Tuesday, December 4, 2012

Erin | led and light sensor code evolution



basic code to make the led come on when light sensor senses above a certain range and off when not. 


//Final Project Light Sensor to turn on led - working to turn light on or off
//To Do
// 1. make the light glowy (PWM)
// 2. make more (groups of) lights glowy at intervals (preferably at random)
// 3. (if time) make the  if (ldr) sensitivity connected to a potentiometer for ease in installation
// 4. (if time) make the led brightnesses on potentiometer as well.

int ledPin = 9;  // led pin on 9
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR
float sinVal;
int ledVal;

#define DEBOUNCE 5  // button debouncer


void setup() {
  // nothing to do here
  pinMode(ledPin,OUTPUT);
  pinMode(ldrPin,INPUT);
Serial.begin(9600);
Serial.flush();

}

void loop() {
   ldrValue = analogRead(ldrPin); // read the value from the LDR
        Serial.println(ldrValue);
        // tone(ledPin,1000); // play a 1000Hz tone from the piezo
        delay(500);  // wait a bit
        //digitalWrite(ledPin, LOW);
        if (ldrValue <= 250) {
          digitalWrite(ledPin, HIGH); //turn led on
        }
        else {
          digitalWrite(ledPin, LOW);//turn led off
        delay(25);
      }
}

________
now trying to achieve some of the goals listed above including fade. 
So far I've tried two kinds, but can't seem to make it fade in and hold when sensor is triggered and fade out and hold when sensor is not getting enough light. 

//Final Project Light Sensor to fade in led - this code fades in and out until no light reaches the lightsensor

int ledPin = 9;  // led on Pin 9
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR
float sinVal;
int ledVal;

#define DEBOUNCE 5  // button debouncer


void setup() {
  // nothing to do here
  pinMode(ledPin,OUTPUT);
  pinMode(ldrPin,INPUT);
Serial.begin(9600);
Serial.flush();

}

void loop() {
   ldrValue = analogRead(ldrPin); // read the value from the LDR
        Serial.println(ldrValue);
        // wait a bit
        
        if (ldrValue <= 250) {
          for (int x=0; x<180; x++){
        // convert degrees to radians then obtain sin value
        sinVal = (sin(x*(3.1412/180)));
        ledVal = int(sinVal*255);
        analogWrite(ledPin, ledVal);
        delay(25);
       
        //digitalWrite(ledPin, HIGH); //turn led on
        }}
        else {
          digitalWrite(ledPin, LOW);//turn led off
        // delay(25);
      }
}





_________________________
Another approach

//Final Project Light Sensor to turn on led - working to fade light on or off
// using arduino fade tutorial http://arduino.cc/en/Tutorial/Fade
// it's 
int ledPin = 9;  // led on Pin 9
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR

int brightness = 0;     // how bright the LED is
int dimness = 0;
int fadeAmount = 1;    // how many points to fade the LED by

#define DEBOUNCE 5  // button debouncer


void setup() {
  pinMode(ledPin,OUTPUT);
  pinMode(ldrPin,INPUT);
Serial.begin(9600);
Serial.flush();
}

void loop() {
   ldrValue = analogRead(ldrPin); // read the value from the LDR
        Serial.println(ldrValue);
                    
        if (ldrValue <= 250) {
          analogWrite(ledPin, brightness);
            brightness = brightness + fadeAmount;

              
        }
        else {
          digitalWrite(ledPin, LOW); //turn off light immediately
         // this bit of code i've cut out with //s is weird 
         // because if the sensor recieves too little light 
         // the led stays where it was the moment the sensor was covered
         
         // analogWrite(ledPin, brightness); 
         // brightness = brightness - fadeAmount;

         //(brightness == 0 || brightness == 255);{
        
        //fadeAmount = -fadeAmount ; 
       //  }
        
        delay(30);
      
   }}

__________
This is my back up code. It is not ideal. 

//Final Project Light Sensor  
// working to turn lights on or off in sequence when ldr is triggered and off after the sequence completes if the ldr is not triggered
// 3. (if time) make the  if ldr sensitivity connected to a potentiometer for ease in installation
// 4. (if time) make the led brightnesses on potentiometer as well.

int ledPin1 = 11;  // led Pin 9
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 6;
int ledPin5 = 5;
int ledPin6 = 3;
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR
float sinVal;
int ledVal;

#define DEBOUNCE 5  // button debouncer ?? I don't know if this is doing anything. 


void setup() {
  pinMode(ledPin1,OUTPUT);
  pinMode(ledPin2,OUTPUT);
  pinMode(ledPin3,OUTPUT);
  pinMode(ledPin4,OUTPUT);
  pinMode(ledPin5,OUTPUT);
  pinMode(ledPin6,OUTPUT);

  pinMode(ldrPin,INPUT);
Serial.begin(9600);   // helpful to see how much light the sensor is reading
Serial.flush();

}

void loop() {
   ldrValue = analogRead(ldrPin); // read the value from the LDR
        Serial.println(ldrValue);
        delay(25);  // wait a bit to slow down the reading
        
        if (ldrValue <= 250) {  // if time is left at the end, I would like this sensitivity to be determined by a potentiometer
          // * I would prefer to have the lights glow on and off in sequence. 
          // ** and would like the sequence to only run once. 
          // *** and stop if the ldr sensor is not getting enough light sensitivity
          digitalWrite(ledPin1, HIGH); //turn led on
          delay(500);
          digitalWrite(ledPin1, LOW); //turn led on
          delay(100);
          digitalWrite(ledPin2, HIGH); //turn led on
          delay(400);
          digitalWrite(ledPin3, HIGH); //turn led on
          delay(100);
          digitalWrite(ledPin2, LOW); //turn led on
          digitalWrite(ledPin3, LOW); //turn led on
           digitalWrite(ledPin4, HIGH); //turn led on
          delay(500);
          digitalWrite(ledPin4, LOW); //turn led on
          delay(100);
          digitalWrite(ledPin5, HIGH); //turn led on
          delay(1000);
          digitalWrite(ledPin5, LOW); //turn led on
          delay(100);

        }
        else {
          digitalWrite(ledPin1, LOW);//turn led off
        delay(25);
      }
}






________________________
This is using an array and fade. 
/*
  This Code was written to sequence the PWM pins
  on an Arduino. In this was the LEDs will fade on
  and off one after the next.
  
  There should be an LED attached with resistor form
  each PWM pin to Ground
  
  Code written by:
  Dan Joyce and Lauren Stec
  
  addition of ldr sensor by Erin Curry
*/

//Declares PMW ports in an array named woot
int woot[] = {3,5,6,9,10,11};                                            
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR

void setup()                                                        
{
pinMode(ldrPin,INPUT);
Serial.begin(9600);
Serial.flush();
 

}

void loop ()
{
     ldrValue = analogRead(ldrPin); // read the value from the LDR
     Serial.println(ldrValue);
     delay(10); 
   {if (ldrValue <= 250) 
       {
  //for loop. "x" declares the position of the integers listed in the array (6 positions).
  for(int x = 0; x <= 5; x++)    
  {
    //for loop. declares fade-in setting (0-255)
    for(int value = 0; value <= 255; value+=5)                   
    {
      //Calls for the PWM pin as listed in the array and performs an analogWrite.
      analogWrite(woot[x], value);                                
     delay(30);
       //   
    }
    for (int value = 255; value>= 0; value-=5)        
    {
      analogWrite(woot[x], value);        
      delay(30); 
    }
  }}}}

   // else {
     // for(int x = 0; x <= 5; x++)   
      //analogWrite(woot[x],LOW);
 


This is modified to allow more direct fade adjustment. 
/*
  This Code was written to sequence the PWM pins
  on an Arduino. In this was the LEDs will fade on
  and off one after the next.
  
  There should be an LED attached with resistor form
  each PWM pin to Ground
  
  initial Code written by:
  Dan Joyce and Lauren Stec
  
  addition of ldr sensor by Erin Curry
  I've also removed the array to allow each pin to be called one by one. 
  (I know it's not efficent this way, but I'm able to see exactly which light is called AND 
  change the rate of fade to be specific for each led by changing the delay 
  which allows me to have some drawings lit longer than others and would allow change the tempo of reading.)
  I've also added a call to read the light sensor before moving onto the next light
  this code interrupts the light, but not the sequence, for example if the "candlelight" 
  source goes out and then back on the led sequence picks up where it would have been without any interruption. 
  [i.e. skipping led2 and 3 and picking up at 4.]
 This is not what I'm looking for. 
 I want the sensor to be read continually (or even between each led lighting up) and
 if the ldr doesn't sense enough light, the sequence should stop.
 When enough light is sensed again the sequence should start from the beginning. 
 
  
*/

int ledPin1 = 11;  // led Pin 9
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 6;
int ledPin5 = 5;
int ledPin6 = 3;                                          
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR

void setup()                                                        
{
pinMode(ldrPin,INPUT);
Serial.begin(9600);
Serial.flush();
 

}

void loop ()
{
     ldrValue = analogRead(ldrPin); // read the value from the LDR
     Serial.println(ldrValue);
     delay(10); 
   {if (ldrValue <= 250) //condition to be met can be altered here.
       {
 
  {
    //for loop. declares fade-in setting (0-255)
    for(int value = 0; value <= 255; value+=5)                   
    {
      //Calls for the PWM pin as listed in the array and performs an analogWrite.
      analogWrite(ledPin1, value);                                
     delay(30);
       //   
    }
    for (int value = 255; value>= 0; value-=5)        
    {
      analogWrite(ledPin1, value);        
      delay(30); 
    }
  }}}

 ldrValue = analogRead(ldrPin); // read the value from the LDR
     Serial.println(ldrValue);
     delay(10); 
   {if (ldrValue <= 250) 
       {
  //for loop. "x" declares the position of the integers listed in the array (6 positions).
  //for(int x = 0; x <= 5; x++)    
  {
    //for loop. declares fade-in setting (0-255)
    for(int value = 0; value <= 255; value+=5)                   
    {
      //Calls for the PWM pin as listed in the array and performs an analogWrite.
      analogWrite(ledPin2, value);                                
     delay(30);
       //   
    }
    for (int value = 255; value>= 0; value-=5)        
    {
      analogWrite(ledPin2, value);        
      delay(30); 
    }
  }}}
 ldrValue = analogRead(ldrPin); // read the value from the LDR
     Serial.println(ldrValue);
     delay(10); 
   {if (ldrValue <= 250) 
       {
  //for loop. "x" declares the position of the integers listed in the array (6 positions).
  //for(int x = 0; x <= 5; x++)    
  {
    //for loop. declares fade-in setting (0-255)
    for(int value = 0; value <= 255; value+=5)                   
    {
      //Calls for the PWM pin as listed in the array and performs an analogWrite.
      analogWrite(ledPin3, value);                                
     delay(30);
       //   
    }
    for (int value = 255; value>= 0; value-=5)        
    {
      analogWrite(ledPin3, value);        
      delay(30); 
    }
  }}}
   ldrValue = analogRead(ldrPin); // read the value from the LDR
     Serial.println(ldrValue);
     delay(10); 
   {if (ldrValue <= 250) 
       {
  //for loop. "x" declares the position of the integers listed in the array (6 positions).
  //for(int x = 0; x <= 5; x++)    
  {
    //for loop. declares fade-in setting (0-255)
    for(int value = 0; value <= 255; value+=5)                   
    {
      //Calls for the PWM pin as listed in the array and performs an analogWrite.
      analogWrite(ledPin4, value);                                
     delay(30);
       //   
    }
    for (int value = 255; value>= 0; value-=5)        
    {
      analogWrite(ledPin4, value);        
      delay(30); 
    }
  }}}
}

   // else {
     // for(int x = 0; x <= 5; x++)   
      //analogWrite(woot[x],LOW);
 

Nisa helped with my code in exchange for some help with her final project and worked out a brillant solution. 
int ledPin1 = 11;  // led Pin 9
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 6;
int ledPin5 = 5;
int ledPin6 = 3;                                          
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR

int a = 0; //variable to check if led 1 has already been triggered
int b = 0; //variable to check if led 2 has already been triggered
int c = 0; //variable to check if led 3 has already been triggered


void setup() {
  pinMode(ldrPin,INPUT);
  Serial.begin(9600);
  Serial.flush();
  analogWrite(ledPin1, 0);
}

void loop () {
  ldrValue = analogRead(ldrPin); // read the value from the LDR
  Serial.println(ldrValue);
  delay(10);
  
  if (ldrValue>=700) {
    analogWrite(ledPin1, LOW);
    analogWrite(ledPin2, LOW);
    analogWrite(ledPin3, LOW);
    analogWrite(ledPin4, LOW);
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);\
               a = 0;
               b = 0;
               c = 0;
  }
  
      if (ldrValue <=699 && a == 0 && b == 0 && c == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin1, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin1, value);        
               }
             a = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

      if (ldrValue <=699 && a == 1 && b == 0 && c == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin2, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin2, value);        
               }
             a = 1;
             b = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

      if (ldrValue <=699 && a == 1 && b == 1 && c == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin3, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin3, value);        
               }
             a = 1;
             b = 1;
             c = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

      if (ldrValue <=699 && a == 1 && b == 1 && c == 1) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin4, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin4, value);        
               }
             a = 0;
             b = 0;
             c = 0;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

}

And here is the final code modified for the "story".
int ledPin1 = 11;  // led Pin 9
int ledPin2 = 10;
int ledPin3 = 9;
int ledPin4 = 6;
int ledPin5 = 5;
int ledPin6 = 3;                                          
int ldrPin = 0;   // LDR on Analog Pin 0
int ldrValue = 0;  // Value read from the LDR

int a = 0; //variable to check if led 1 has already been triggered
int b = 0; //variable to check if led 2 has already been triggered
int c = 0; //variable to check if led 3 has already been triggered
int d = 0; // led 4 triggered?
int e = 0; // led 5 triggered?

void setup() {
  pinMode(ldrPin,INPUT);
  Serial.begin(9600);
  Serial.flush();
  analogWrite(ledPin1, 0);
}

void loop () {
  ldrValue = analogRead(ldrPin); // read the value from the LDR
  Serial.println(ldrValue);
  delay(10);
  
  if (ldrValue>260) {
    analogWrite(ledPin1, LOW);
    analogWrite(ledPin2, LOW);
    analogWrite(ledPin3, LOW);
    analogWrite(ledPin4, LOW);
    analogWrite(ledPin5, LOW);
    analogWrite(ledPin6, LOW);

               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);\
               a = 0;
               b = 0;
               c = 0;
               d = 0;
               e = 0;
  }
  
      if (ldrValue <=260 && a == 0 && b == 0 && c == 0 && d == 0 && e == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin1, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin1, value);        
               }
             a = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

      if (ldrValue <=260 && a == 1 && b == 0 && c == 0 && d == 0 && e == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin2, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin2, value);        
               }
             a = 1;
             b = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

      if (ldrValue <=260 && a == 1 && b == 1 && c == 0 && d == 0 && e == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin3, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(30);
              analogWrite(ledPin3, value);        
               }
             a = 1;
             b = 1;
             c = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

      if (ldrValue <=260 && a == 1 && b == 1 && c == 1 && d == 0 && e == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin4, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(10);
              analogWrite(ledPin4, value);        
               }
             a = 1;
             b = 1;
             c = 1;
             d = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }
             
          
 if (ldrValue <=260 && a == 1 && b == 1 && c == 1 && d == 1 && e == 0) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(20);
                analogWrite(ledPin5, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(5);
              analogWrite(ledPin5, value); 
             }
             for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(10);
                analogWrite(ledPin5, value);                                 
                }
             for (int value = 255; value>= 0; value-=5) { //fade led1 out
                delay(40);
              analogWrite(ledPin5, value);        
               }
             a = 1;
             b = 1;
             c = 1;
             d = 1;
             e = 1;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }
             
              if (ldrValue <=260 && a == 1 && b == 1 && c == 1 && d == 1 && e == 1) {
            for(int value = 0; value <= 255; value+=5) { //fade led1 in
                delay(30);
                analogWrite(ledPin6, value);                                 
                }
             for (int value = 255; value>= 0; value-=2) { //fade led1 out
                delay(120);
              analogWrite(ledPin6, value);        
               }
             a = 0;
             b = 0;
             c = 0;
             d = 0;
             e = 0;
               ldrValue = analogRead(ldrPin); // read the value from the LDR
               Serial.println(ldrValue);
             }

}

It was mentioned during crit that the light should perhaps be less glowy and more flickery and consider non-white leds. I'm not sure I'd like the light to flicker much for story except get that to happen more in the 5th stage. After searching I found there are warm white christmas leds which I could hack to use, they are relatively expensive on line as a pack of bulbs . . .

No comments:

Post a Comment