Friday 29 October 2010

Logic to cycle up and down a row of LEDS,could add a delay that reflects the number of the button counter, or flas them on and off that many times, i.e delay() and digitalWrite(HIGH) or LOW how ever many tmes..same circuit as image below

int brightness = 0; // how bright the LED isint fadeAmount = 5; // how many points to fade the LED byconst int buttonPin = 7;int buttonState = 0; const int ledPin = 8; int counter =0;
//turns one after the other LED on and off in sequencevoid setup() { // declare pin 9 to be an output: pinMode(9, OUTPUT); pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); //last led}
void loop() {
// set the brightness of pin 9:pwm /* analogWrite(9, brightness); analogWrite(10, brightness); analogWrite(11, brightness); */ buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { counter ++;
}
if(counter ==1){ digitalWrite(ledPin, HIGH); delay(1000); }
if(counter ==2){ digitalWrite(ledPin, LOW); analogWrite(9, brightness); delay(1000); } if(counter ==3){ digitalWrite(ledPin, LOW); analogWrite(9, 0); analogWrite(10, brightness); delay(1000); } if(counter ==4){ digitalWrite(ledPin, LOW); analogWrite(9, 0); analogWrite(10, 0); analogWrite(11, brightness); delay(1000); }
if(counter==5){ analogWrite(11, 0); analogWrite(10, brightness); delay(1000); }
if(counter==6){ analogWrite(10, 0); analogWrite(9, brightness);
delay(1000); }
if(counter==7){ analogWrite(10, 0); analogWrite(9, 0); analogWrite(8, brightness); delay(1000); counter=0; }
// change the brightness for next time through the loop: brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade: if (brightness == 0 brightness == 255) { fadeAmount = -fadeAmount ; } else { // turn LED off: digitalWrite(ledPin, LOW); } // wait for 30 milliseconds to see the dimming effect delay(30);

}

No comments:

Post a Comment