Back to Parent

#include <neopixel.h>

#define PIXEL_PIN D2
#define PIXEL_COUNT 50
#define INC_BRIGHT 25
#define TIMER_MAX 4
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

bool flash = false;
int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;

uint32_t c;
uint32_t white = strip.Color(255,255,255);
uint32_t red = strip.Color(255,0,0);
uint32_t yellow = strip.Color(255,255,0);
uint32_t green = strip.Color(0,255,0);
uint32_t black = strip.Color(0,0,0);

int IR = D3;
int detectionIR = HIGH;
int countIR = 0;
int timerIR = TIMER_MAX;

int accel = A2;
int detectionAC;
int countAC = 0;
int timerAC = TIMER_MAX;
int rest = 0;

int stage = 1;
int light = 1;

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

void detectIR() {
  detectionIR = digitalRead(IR);

  if (detectionIR == LOW && countIR <= 25) countIR++;
  else if (timerIR > 0) timerIR--;
  else if (countAC > 0) {
    timerIR = TIMER_MAX;
    countIR--;
  }
}

void detectMotion() {
  detectionAC = analogRead(accel) - rest;

  if(detectionAC > 102 && countAC <= 15) countAC++;
  else if (timerAC > 0) timerAC--;
  else if (countAC > 0) {
    timerAC = TIMER_MAX;
    countAC--;
  }
}

void setStage() {
  if (countIR >= 20 && countAC >= 10) stage = 3;
  else if (countIR > 10 && countAC >= 5) stage = 2;
  else stage = 1;

  light = (flash) ? 2*stage : 2*stage-1;
}

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

void changePixels1() {
    for(int i = 0; i < strip.numPixels(); i++) {
        if (stage == 1) {
            c = strip.Color(brightness1, 0, 0);
            brightness1 = brightness1 + INC_BRIGHT;
        } else if (stage == 2) {
            c = strip.Color(0, 0, brightness2);
            brightness2 = brightness2 + INC_BRIGHT;
        } else {
            c = strip.Color(0, brightness3, 0);
            brightness3 = brightness3 + INC_BRIGHT;
        }
       
        strip.setPixelColor(i, c); // set a color
    }

    strip.show();
}

void changePixels2() {
    for(int i = 0 ; i < strip.numPixels(); i++){
        if (light == 1 && i < 5) strip.setPixelColor(i, red);
        else if (light == 2 && i > 7  && i < 13) strip.setPixelColor(i, red);
        else if (light == 3 && i > 14 && i < 21) strip.setPixelColor(i, yellow);
        else if (light == 4 && i > 23 && i < 29) strip.setPixelColor(i, yellow);
        else if (light == 5 && i > 30 && i < 37) strip.setPixelColor(i, green);
        else if (light == 6 && i > 39 && i < 45) strip.setPixelColor(i, green);
        else strip.setPixelColor(i, black);
        
        displayState();
    }

    strip.show();
}

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

void setup() {
  strip.begin();
  strip.show();

  pinMode(IR, INPUT);
  pinMode(accel, INPUT);
  
  rest = analogRead(accel);
}

void loop() {
    detectIR();
    detectMotion();
    setStage();
    changePixels2();

    flash = !flash;
    delay(500);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0