0

EMERGENCY HELP! Magnet Sensor Sets Off LED
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

EMERGENCY HELP! Magnet Sensor Sets Off LED

by Zumbadorcito on Mon Apr 25, 2016 4:48 pm

This for a final project, and I need all the help I can get. I'm a student at University of West Florida in an Interactive Design class, and I'm creating a box that has an LED grid that runs through a simple color wipe, but changes to the Rainbow and Rainbow cycle when it is introduced to a magnet. I'm using the simple strandtest_2 code as my base, and tryiong to modify it using a hall sensor code that works for a new hall sensor module for Arduino.

Here's a list of my equipment:

Arduino Uno

Ws2801 non-waterproof addressable LED strips (64 LEDs/8 Strips)

New Hall Sensor Module for Arduino

Magnet: rare Earth Metal Magnet

Here are both of the codes I'm using:

Strandtest_2
Code: Select all | TOGGLE FULL SIZE
`````#include "Adafruit_WS2801.h"
#include "SPI.h" // Comment out this line if using Trinket or Gemma
#ifdef __AVR_ATtiny85__
 #include <avr/power.h>
#endif

/*****************************************************************************
Example sketch for driving Adafruit WS2801 pixels!


  Designed specifically to work with the Adafruit RGB Pixels!
  12mm Bullet shape ----> https://www.adafruit.com/products/322
  12mm Flat shape   ----> https://www.adafruit.com/products/738
  36mm Square shape ----> https://www.adafruit.com/products/683

  These pixels use SPI to transmit the color data, and have built in
  high speed PWM drivers for 24 bit color per pixel
  2 pins are required to interface

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution

*****************************************************************************/

// Choose which 2 pins you will use for output.
// Can be any valid output pins.
// The colors of the wires may be totally different so
// BE SURE TO CHECK YOUR PIXELS TO SEE WHICH WIRES TO USE!
uint8_t dataPin  = 2;    // Yellow wire on Adafruit Pixels
uint8_t clockPin = 3;    // Green wire on Adafruit Pixels

// Don't forget to connect the ground wire to Arduino ground,
// and the +5V wire to a +5V supply

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_WS2801 strip = Adafruit_WS2801(64, dataPin, clockPin);

// Optional: leave off pin numbers to use hardware SPI
// (pinout is then specific to each board and can't be changed)
//Adafruit_WS2801 strip = Adafruit_WS2801(25);

// For 36mm LED pixels: these pixels internally represent color in a
// different format.  Either of the above constructors can accept an
// optional extra parameter: WS2801_RGB is 'conventional' RGB order
// WS2801_GRB is the GRB order required by the 36mm pixels.  Other
// than this parameter, your code does not need to do anything different;
// the library will handle the format change.  Examples:
//Adafruit_WS2801 strip = Adafruit_WS2801(25, dataPin, clockPin, WS2801_GRB);
//Adafruit_WS2801 strip = Adafruit_WS2801(25, WS2801_GRB);

void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
  clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif

  strip.begin();

  // Update LED contents, to start they are all 'off'
  strip.show();
}


void loop() {
  // Some example procedures showing how to display to the pixels
 
  colorWipe(Color(255, 0, 0), 50);
  colorWipe(Color(0, 255, 0), 50);
  colorWipe(Color(0, 0, 255), 50);
  rainbow(20);
  rainbowCycle(20);
}

void rainbow(uint8_t wait) {
  int i, j;
   
  for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 255));
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
 
  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
  int i;
 
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}



Hall Sensor Code

Code: Select all | TOGGLE FULL SIZE
int sensorPin = 2;
int counter = 0;
boolean sensorState = false;

void setup()
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, LOW);
}

void loop()
{
 
  if(!magnetPresent(sensorPin) && !sensorState)
  {
    sensorState = true;
    printMessage("Magnet yo");
  }
  else(magnetPresent(sensorPin) && sensorState);
  {
    sensorState = false;
    printMessage("Magnet bye");
  }
 
}

void printMessage(String message){
  counter++;
  Serial.print(counter);
  Serial.print(" ");
  Serial.println(message);
//  delay(1000);
}

boolean magnetPresent(int pin){
  return digitalRead(pin) == LOW;
}


Most of the work has been done, and I'm only trying to modify what is there. The intent is that when the magnet is not present, it goes through the color wipe code. When the magnet is present, it goes into the rainbow cycle. Thank you for reviewing this, and please respond ASAP!

Zumbadorcito
 
Posts: 4
Joined: Mon Apr 25, 2016 4:34 pm

Re: EMERGENCY HELP! Magnet Sensor Sets Off LED

by adafruit_support_bill on Mon Apr 25, 2016 5:10 pm

Most of the work has been done, and I'm only trying to modify what is there. The intent is that when the magnet is not present, it goes through the color wipe code. When the magnet is present, it goes into the rainbow cycle.

Looks like you have all you need. You just ned to put them together. Where you are printing messages in your hall sensor code now, call the appropriate led functions instead.

adafruit_support_bill
 
Posts: 74618
Joined: Sat Feb 07, 2009 10:11 am

Re: EMERGENCY HELP! Magnet Sensor Sets Off LED

by Zumbadorcito on Mon Apr 25, 2016 10:29 pm

I have the code setting somewhat correct because the power is going through my magnet sensor, but the led code isn't going through. With the magnet present it's supposed to do a color wipe, and when I introduce the magnet, it switches to the rainbow cycle. I may havbe my pins mixed up, but I honestly have no idea what the 'int counter' section is and it's set to zero.

Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_WS2801.h"
#include "SPI.h" // Comment out this line if using Trinket or Gemma
#ifdef __AVR_ATtiny85__
 #include <avr/power.h>
#endif

int sensorPin = 4;
int counter = 0;
boolean sensorState = false;

uint8_t dataPin  = 2; 
uint8_t clockPin = 11;

Adafruit_WS2801 strip = Adafruit_WS2801(64, dataPin, clockPin);

void setup()
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, LOW);
 
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
  clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif

  strip.begin();

  // Update LED contents, to start they are all 'off'
  strip.show();
 
}

void loop()
{
 
  if(!magnetPresent(sensorPin) && !sensorState)
  {
    sensorState = true;
    printMessage("Magnet yo");
   
  rainbow(20);
  rainbowCycle(20);
 
  }
  else if(magnetPresent(sensorPin) && sensorState)
  {
    sensorState = false;
    printMessage("Magnet bye");
   
  colorWipe(Color(255, 0, 0), 50);
  colorWipe(Color(0, 255, 0), 50);
  colorWipe(Color(0, 0, 255), 50);
  }
 
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

void rainbow(uint8_t wait) {
  int i, j;
   
  for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 255));
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
 
  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
  int i;
 
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void printMessage(String message){
  counter++;
  Serial.print(counter);
  Serial.print(" ");
  Serial.println(message);
//  delay(1000);
}

uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

boolean magnetPresent(int pin){
  return digitalRead(pin) == LOW;
}


Zumbadorcito
 
Posts: 4
Joined: Mon Apr 25, 2016 4:34 pm

Re: EMERGENCY HELP! Magnet Sensor Sets Off LED

by Zumbadorcito on Mon Apr 25, 2016 11:07 pm

I just tested it with the magnet, the pole turned out to be wrong, and it went straight into the rainbow cycles instead, then went into the color wipes, then stopped at the color blue, then I reintroduced the magnet, and it reset itself. At least I know my pins are set right, but I need the code to have the color wipes playing through on repeat without the magnet present, then when I introduce the magnet, it goes into the rainbow cycle. My friend said we may need to try nesting 'if' statements.

Zumbadorcito
 
Posts: 4
Joined: Mon Apr 25, 2016 4:34 pm

Re: EMERGENCY HELP! Magnet Sensor Sets Off LED

by Zumbadorcito on Mon Apr 25, 2016 11:17 pm

We solved the problem of getting it to run with the color wipes: The 'else if' statement was tripping up the code, and it does change to the rainbow code when introduced with the magnet, but it does do it instantaneously. The color wipes want to run through the entire cycle first before going into the rainbow code. This is what I have so far:

Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_WS2801.h"
#include "SPI.h" // Comment out this line if using Trinket or Gemma
#ifdef __AVR_ATtiny85__
 #include <avr/power.h>
#endif

int sensorPin = 4;
int counter = 0;
boolean sensorState = false;

uint8_t dataPin  = 2; 
uint8_t clockPin = 11;

Adafruit_WS2801 strip = Adafruit_WS2801(64, dataPin, clockPin);

void setup()
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, LOW);
 
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
  clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif

  strip.begin();

  // Update LED contents, to start they are all 'off'
  strip.show();
 
}

void loop()
{
{
//{
  /*if(!magnetPresent(sensorPin) && !sensorState)
  colorWipe(Color(255, 0, 0), 50);
  colorWipe(Color(0, 255, 0), 50);
  colorWipe(Color(0, 0, 255), 50);}*/
 
  if(!magnetPresent(sensorPin) && !sensorState)

  {
   
    sensorState = true;
    printMessage("Magnet yo");
   
  rainbow(1);
  rainbowCycle(1);
 
  }
  else (magnetPresent(sensorPin) && sensorState);
  {
    sensorState = false;
    printMessage("Magnet bye");
   
  colorWipe(Color(255, 0, 0), 50);
  colorWipe(Color(0, 255, 0), 50);
  colorWipe(Color(0, 0, 255), 50);
  }
 }
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

void rainbow(uint8_t wait) {
  int i, j;
   
  for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 255));
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
 
  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
  int i;
 
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void printMessage(String message){
  counter++;
  Serial.print(counter);
  Serial.print(" ");
  Serial.println(message);
//  delay(1000);
}

uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

boolean magnetPresent(int pin){
  return digitalRead(pin) == LOW;
}


It does the function when introduced to the magnet, but I need it to do the instant the magnet is introduced.

Zumbadorcito
 
Posts: 4
Joined: Mon Apr 25, 2016 4:34 pm

Re: EMERGENCY HELP! Magnet Sensor Sets Off LED

by adafruit_support_bill on Tue Apr 26, 2016 6:27 am

but I need it to do the instant the magnet is introduced.

If you want the magnet to interrupt an LED sequence, you need to test for the magnet inside your LED sequence so you can exit early.

adafruit_support_bill
 
Posts: 74618
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.


cron