Wednesday, September 29, 2010

Code solutions

Here is a code similar to one from my last post that fades a line of LEDs in sequence. And good input Mr. Sketch, I needed to set more variables up for this code for the fade and brightnesses otherwise the fade direction will shift constantly and the brightnesses will be static or tied together, something i learned the hard way.....the three hour way.



//LED Pin Variables
int ledPins[] = {3,5,6,9,10,11}; //An array to hold the pin each LED is connected to
                                   //i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
                                   //to address an array use ledPins[0] this would equal 2
                                   //and ledPins[7] would equal 9  
int fadeAmount1 = 1;
int fadeAmount2 = 1;
int fadeAmount3 = 1;
int fadeAmount4 = 1;
int fadeAmount5 = 1;
int fadeAmount6 = 1;
// how many points to fade the LED by
int delaytime = 2;
int maxim = 100;
int bright1 = 0;   // how bright the LED is
int bright2 = 20;
int bright3 = 40;
int bright4 = 60;
int bright5 = 80;
int bright6 = 99;

 /*
 * setup() - this function runs once when you turn your Arduino on
 * We the three control pins to outputs
 */
void setup()
{
 
  //Set each pin connected to an LED to output mode (pulling high (on) or low (off)
  for(int i = 0; i < 6; i++){         //this is a loop and will repeat eight times
      pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
    
  }                                  


}


void loop()                    
{
 
 // oneOnAtATime();          //this will turn one LED on then turn the next one
                             //on turning the
                             //former off (one LED will look like it is scrolling
                             //along the line
analogWrite(ledPins[0], bright1);  

  // change the brightness for next time through the loop:
  bright1 = bright1 + fadeAmount1;

  // reverse the direction of the fading at the ends of the fade:
  if (bright1 == 0 || bright1 == maxim) {
    fadeAmount1 = -fadeAmount1 ;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  delay(delaytime);                                
analogWrite(ledPins[1], bright2);  

  // change the brightness for next time through the loop:
  bright2 = bright2 + fadeAmount2;

  // reverse the direction of the fading at the ends of the fade:
  if (bright2 == 0 || bright2 == maxim) {
    fadeAmount2 = -fadeAmount2 ;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  delay(delaytime);    
analogWrite(ledPins[2], bright3);  

  // change the brightness for next time through the loop:
  bright3 = bright3 + fadeAmount3;

  // reverse the direction of the fading at the ends of the fade:
  if (bright3 == 0 || bright3 == maxim) {
    fadeAmount3 = -fadeAmount3 ;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  delay(delaytime);          
  analogWrite(ledPins[3], bright4);  

  // change the brightness for next time through the loop:
  bright4 = bright4 + fadeAmount4;

  // reverse the direction of the fading at the ends of the fade:
  if (bright4 == 0 || bright4 == maxim) {
    fadeAmount4 = -fadeAmount4 ;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  delay(delaytime);        
  analogWrite(ledPins[4], bright5);  

  // change the brightness for next time through the loop:
  bright5 = bright5 + fadeAmount5;

  // reverse the direction of the fading at the ends of the fade:
  if (bright5 == 0 || bright5 == maxim) {
    fadeAmount5 = -fadeAmount5 ;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  delay(delaytime);  
 analogWrite(ledPins[5], bright6);  

  // change the brightness for next time through the loop:
  bright6 = bright6 + fadeAmount6;

  // reverse the direction of the fading at the ends of the fade:
  if (bright6 == 0 || bright6 == maxim) {
    fadeAmount6 = -fadeAmount6 ;
  }    
  // wait for 30 milliseconds to see the dimming effect  
  delay(delaytime);    
}

No comments:

Post a Comment