Flash 4 leds from right to left and left to light.
Difficulty / Time to complete:
Beginner / 5 minutes
Tools:
- Arduino board (tested on duemilanove)
- 4 LEDs
- 1 switch
- 1 10K/.25W resistor
- Workstation with Arduino 0.017 or higher
int leds[]={10,11,12,13}; //pins with leds
const int dl = 75; //delay between moving
const int btn = 9; //button pin
int btnS = 0; //button state
int i=0; //general purpose counter
void setup(){
for(i=0;ipinMode(leds[i],OUTPUT);
}
pinMode(btn,INPUT);
myflash(leds,sizeof(leds),dl,false);
myflash(leds,sizeof(leds),dl,true);
}
void loop(){
btnS = digitalRead(btn);
if(btnS == HIGH){
myflash(leds,sizeof(leds),dl,false);
myflash(leds,sizeof(leds),dl,true);
delay(250);
}
else{
for(i=0;idigitalWrite(leds[i],LOW);
}
}
//myflash function: flash an array of pins to the R or L
void myflash(int arr[],int size,int delay1,boolean reverse){
int j = 0;
if(reverse==true){
for(j=(size-1);j>=0;j--){
digitalWrite(arr[j],HIGH);
delay(delay1);
digitalWrite(arr[j],LOW);
}
}
else {
for(j=0;jdigitalWrite(arr[j],HIGH);
delay(delay1);
digitalWrite(arr[j],LOW);
}
}
}
Note: After copying-pasting code to the Arduino IDE, press Ctrl+T to fix indents.
No comments:
Post a Comment