Go Down

Topic: NeoPixels and States (Read 1 time) previous topic - next topic

thatfrostyguy

Hi Everyone,
Pretty new to Arduino, having a bit of an issue with my code.

My current project is a cylinder with a product in it. Every time someone in the store walks by, LEDs will blink in a Red LED sequence, then turn off after x amount of seconds and switch to an idle sequence, where no LEDs will be going. When the next person walks by, it should switch to a blue LED sequence.

 
Basically I need to switch between different LED states every time the PIR sensor sees something. I am most likely missing something very simple in this, the code is also a mess, I apologize in advance. I have never needed to use states before and poking around the forum hasn't helped much.

There are currently 3 states that need to be executed
0 being IDLE
1 being RED
2 being BLUE

I have it sort of working, State goes IDLE when there is no motion, and when there is motion, it flicks to RED state, however I am unable to get it to switch to blue after red, then loop the entire thing over again
(Idle > Red >Idle > Blue > Idle > Repeat

Can anyone help out?
Code Below:

Code: [Select]
enum LedStates {IDLE,RED,BLUE,OFF} ledState;
//LedStates ledState = 0;
//unsigned long lastMovementTime = 0;


#include <Adafruit_NeoPixel.h>
#define PIN 2
#define NUM_LEDS 18
const int SensorPin = 3; //For The Sensor


Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
  strip.begin();
  strip.setBrightness(200);
 // strip.show(); // Initialize all pixels to 'off'
 pinMode(SensorPin, INPUT_PULLUP); //pull-up resistor is required for the sensor
LedStates ledState = IDLE;
}

void loop() {

Serial.println (ledState);

  int proximity = digitalRead(SensorPin);
  if (proximity == HIGH) //Sensor detects movement
 
 delay (50);
 
  if (digitalRead(SensorPin)){
    Serial.println("I sense something");
  switch (ledState == ledState) {
  case RED:
  ledState = RED;
  break;
    }
  }
 
//int proximity = digitalRead(SensorPin);
  if (proximity == LOW) //Sensor detects movement
    switch (ledState == 0) {
  case 0:
  ledState = 0;
    }

    //int proximity = digitalRead(SensorPin);
  if (proximity == LOW) //Sensor detects movement
    switch (ledState == 2) {
  case 2:
  ledState = 2;
    }
 
switch (ledState) {

  case IDLE:
    strip.show(); //switch LEDs off
    Serial.println("I am IDLE");
    break;

  case RED:
      Strobe(0xff, 0x00, 0x00, 1, 75, 1000);
  Strobe(0xff, 0x00, 0x00, 4, 45, 10);
 
  RunningLights(0xff,0,0, 20, 100, 30000);
 
  Strobe(0xff, 0x00, 0x00, 1, 75, 1000);
  Serial.println("I am RED");
    break;

  case BLUE:
      Strobe(0x00, 0x00, 0xff, 1, 75, 1000);
  Strobe(0x00, 0x00, 0xff, 4, 45, 10);
 
  RunningLights(0,0,0xff, 20, 100, 30000);
 
  Strobe(0x00, 0x00, 0xff, 1, 75, 1000);
  Serial.println("I am BLUE");
    break;

  case OFF:
    strip.show(); //switch LEDs off
    Serial.println("I am turned OFF");
    break;
}



}

//LED Patterns for reference above.

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

//This is the fuction for the Strobe Feature effect
void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
  for(int j = 0; j < StrobeCount; j++) {
    setAll(red,green,blue);
    showStrip();
    delay(FlashDelay);
    setAll(0,0,0);
    showStrip();
    delay(FlashDelay);
  }
  //Serial.println ("PauseNow");
  delay (EndPause);
}


//function for Running Light effect
void RunningLights(byte red, byte green, byte blue, int WaveDelay, int EndPause, int TimeToPlay) {
  int Position=0;
 
  for(int j=0; j<NUM_LEDS*2; j++)
  {
      Position++; // = 0; //Position + Rate;
      for(int i=0; i<NUM_LEDS; i++) {
        // sine wave, 3 offset waves make a rainbow!
        //float level = sin(i+Position) * 127 + 128;
        //setPixel(i,level,0,0);
        //float level = sin(i+Position) * 127 + 128;
        setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
                   ((sin(i+Position) * 127 + 128)/255)*green,
                   ((sin(i+Position) * 127 + 128)/255)*blue);
      }
     
      showStrip();
      delay(WaveDelay);
      delay(EndPause);
  }
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

hzrnbgy

Code: [Select]
switch (ledState == 0)

This doesn't look right. This would either result to true or false

You normally don't do conditionals in the switch statement

switch(ledstate)

case 1
case 2
etc...

Go Up