Back to Parent

//--------NEOPIXEL SETUP --------
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
static const int PIN = 2;
static const int NUMPIXELS = 3;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//------------------------------


//------- TIMING --------
unsigned long lastSampleTime = 0;
unsigned long sampleInterval = 500; // in ms
//------------------------------


//------- PHOTO CELLS --------
const int photoOne = A0;
const int photoTwo = A1;

int oneReading;
int twoReading;

int oneMap;
int twoMap;

int lightVal = 400;

//------------------------------


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(photoOne, INPUT);
  pinMode(photoTwo, INPUT);

  pixels.begin();
  pixels.show();
}

void loop() {

  checkLight();

}

void checkLight() {
  unsigned long now = millis();
  if (lastSampleTime + sampleInterval < now) {
    lastSampleTime = now;

    oneReading = analogRead(photoOne); // hostess
    twoReading = analogRead(photoTwo); // table
    

    oneMap = map(oneReading, 0, 1023, 0, 500);
    twoMap = map(twoReading, 0, 1023, 0, 500);
  

    Serial.print("Photo One = ");
    Serial.println(oneMap);     // the raw analog reading

    Serial.print("Photo Two = ");
    Serial.println(twoMap);     // the raw analog reading



    if (oneMap <= lightVal) {
      pixelOn(0);
      pixelOff(1);
      pixelOff(2);
    } else if (twoMap <= lightVal) {
      pixelOn(1);
      pixelOff(0);
      pixelOff(2);
    } else if (oneMap > lightVal || twoMap > lightVal ){
      pixelOff(0);
      pixelOff(1);
      pixelOff(2);
    }


  }
}

void pixelOn(int n) {
  pixels.setPixelColor(n, (134, 235, 40));
  pixels.show();
}

void pixelOff(int n) {
  pixels.setPixelColor(n, (0, 0, 0));
  pixels.show();
}
Click to Expand

Content Rating

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

0