giova014:
I've made the code below and tested it, works well.
Don't use a external resistor with the button.
And suggestions:
Don't use Pin1, because it's Serial TX and don't use Pin13, because there's already a LED on it (avoid Current problems)
//Program for Game with LED bouncing
//LEDs are Common Cathode
//Button switches to GND when closed
// PINOUT:
#define BUTTON 1
#define LED0 4
#define LED1 7
#define LED2 8
#define LED3 12
#define LED4 13
// TIME:
#define DELAY_BETWEEN_LEDS 100 //in milliseconds
byte LEDS; //LEDs states
bool Direction; //true = Right, false = Left
unsigned long Timer; //Timer for switch LEDs and debounce
int Delay; //Amount of delay
bool DebounceButton; //For debounce the button
void setup() {
pinMode(LED0,OUTPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);
pinMode(BUTTON,INPUT_PULLUP);
LEDS = B10000;
manageLEDS();
}
void loop() {
blinkLED();
while(1){
if(millis()-Timer<Delay){
if(digitalRead(BUTTON) && DebounceButton)
DebounceButton = false;
if(!digitalRead(BUTTON) && !DebounceButton){
DebounceButton = true;
break;
}
continue;
}
if(LEDS>B00001 && Direction)
LEDS = LEDS >> 1;
else if(LEDS<B10000 && !Direction)
LEDS = LEDS << 1;
if(LEDS==B00001 || LEDS==B10000){
Direction = !Direction;
Delay = 2*DELAY_BETWEEN_LEDS;
}
else
Delay = DELAY_BETWEEN_LEDS;
manageLEDS();
Timer = millis();
}
}
void manageLEDS(){
digitalWrite(LED0,bitRead(LEDS,0));
digitalWrite(LED1,bitRead(LEDS,1));
digitalWrite(LED2,bitRead(LEDS,2));
digitalWrite(LED3,bitRead(LEDS,3));
digitalWrite(LED4,bitRead(LEDS,4));
}
void blinkLED(){
byte LEDS_backup = LEDS;
for(int n = 0; n<16; n++){
if(LEDS)
LEDS = 0;
else
LEDS = LEDS_backup;
manageLEDS();
delay(DELAY_BETWEEN_LEDS);
}
LEDS = LEDS_backup;
}
Please visit the website and order you item. I get back next Sunday, and will take care of your request then.