light sensing scarf

For the second exercise, I attached a blue LED, a green LED, and a light sensor to a scarf to alert the wearer when the light in the area has been low too often.  The green LED grows brighter and the blue LED becomes dimmer when there is more light, and the opposite happens as there is less light.  If the light sensor detects a very low level of light 3 times, the blue and green LEDs will flash 10 times as a warning.

Code:

/*
Analog Input and Output
Justine
*/

int sensorIn = 3;   // input pin for light sensor
int bluePin = 11;   // blue LED pin
int greenPin = 10;  // green LED pin
int inval = 0;      // variable to store value coming from the sensor
int val = 0;        // variable to store value after dividing
int counter = 0;    // variable to store counter for light level
boolean lowLight = false;  //boolean to store whether there is low light

void setup() {
pinMode(bluePin, OUTPUT);  // declare bluePin as an OUTPUT
pinMode(greenPin, OUTPUT); // declare greenPin as an OUTPUT
Serial.begin(9600);       // start comunication with the computer
}

void loop() {
inval = analogRead(sensorIn);    // read the value from the sensor range 0 – 1024
val = inval/4;                  // divide the value by 4 so it is between 0 and 255
//Serial.println(val);         // Send val to the pc, can be seen using Serial Monitor

//blue light gets dimmer and green light gets brighter as light gets brighter
analogWrite(bluePin, val);        //make the blue light shine at the brightness of val
analogWrite(greenPin, 255-val);  //make the green light shine at the opposite of the blue

if (lowLight == false && val > 225) {  //if light has just become low
counter = counter + 1;                //update counter
lowLight = true;                      //update boolean to show low light
//Serial.println(counter);            //send counter to the pc for testing purposes
}

if (val < 215) {                  //if light becomes high again
lowLight = false;              //update boolean
}

// if light has been low 3 times, flash a warning
if (counter >= 3) {
for (int i=0; i<10; i++) {
analogWrite(bluePin, 255);
delay(200);
analogWrite(bluePin, 0);
analogWrite(greenPin, 255);
delay(200);
analogWrite(greenPin, 0);
}
counter = 0;
}

}

Another idea I was considering (or maybe an accessory?) with the same code, light sensing earrings:

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s