Hi to all. I have one push button and then when I push the button in the first time I want that he turn on some leds and when I press the same push button after the first time I want that he turns on one led and turn off the other led
I'am using a Mega model
My project starts with the led at port 53 ON
When I press the button at first time I want that we do the following:
- Turn OFF led of 53 port
- Turn ON led of 52 port
- Turn ON leds from port 34 to port 52
When I press the button after the first time I want that my project do the following:
- Turn OFF led of 52 port
- Turn ON led of 53 port
I don't want to change the state of the other leds (port 34 to 51) because they are commanded by other buttons
My problem is when I run my project and press the button in the first time he runs the code fot the first time and also for the other times
This is My code:
// definition of ports where leds are connected
#define l34 34
#define l35 35
#define l36 36
#define l37 37
#define l38 38
#define l39 39
#define l40 40
#define l41 41
#define l42 42
#define l43 43
#define l44 44
#define l45 45
#define l46 46
#define l47 47
#define l48 48
#define l49 49
#define l50 50
#define l51 51
#define l52 52
#define l53 53
//byte variable for each led
byte l34_estado = LOW;
byte l35_estado = LOW;
byte l36_estado = LOW;
byte l37_estado = LOW;
byte l38_estado = LOW;
byte l39_estado = LOW;
byte l40_estado = LOW;
byte l41_estado = LOW;
byte l42_estado = LOW;
byte l43_estado = LOW;
byte l44_estado = LOW;
byte l45_estado = LOW;
byte l46_estado = LOW;
byte l47_estado = LOW;
byte l48_estado = LOW;
byte l49_estado = LOW;
byte l50_estado = LOW;
byte l51_estado = LOW;
byte l52_estado = LOW;
byte l53_estado = HIGH;
// definition of ports where button are connected
#define b01 A0
//byte variable for last button state
byte last_b01=LOW;
//byte variable to control if is the firs time or not
byte start = HIGH;
void setup(){
pinMode(l34, OUTPUT);
pinMode(l35, OUTPUT);
pinMode(l36, OUTPUT);
pinMode(l37, OUTPUT);
pinMode(l38, OUTPUT);
pinMode(l39, OUTPUT);
pinMode(l40, OUTPUT);
pinMode(l41, OUTPUT);
pinMode(l42, OUTPUT);
pinMode(l43, OUTPUT);
pinMode(l44, OUTPUT);
pinMode(l45, OUTPUT);
pinMode(l46, OUTPUT);
pinMode(l47, OUTPUT);
pinMode(l48, OUTPUT);
pinMode(l49, OUTPUT);
pinMode(l50, OUTPUT);
pinMode(l51, OUTPUT);
pinMode(l52, OUTPUT);
pinMode(l53, OUTPUT);
pinMode(b01, INPUT);
// start the project with led of port 53 ON
digitalWrite(l53,l53_estado);
}
void loop() {
if (start == HIGH){
byte estado_b01 = digitalRead(b01);
if (estado_b01 != last_b01) {
last_b01 = estado_b01;
if (estado_b01==LOW){
//code when I press the button at first time
l52_estado = HIGH;
l53_estado = LOW;
l51_estado = HIGH;
l50_estado = HIGH;
l49_estado = HIGH;
l48_estado = HIGH;
l47_estado = HIGH;
l46_estado = HIGH;
l45_estado = HIGH;
l44_estado = HIGH;
l43_estado = HIGH;
l42_estado = HIGH;
l41_estado = HIGH;
l40_estado = HIGH;
l39_estado = HIGH;
l38_estado = HIGH;
l37_estado = HIGH;
l36_estado = HIGH;
l35_estado = HIGH;
l34_estado = HIGH;
digitalWrite(l53,l53_estado);
digitalWrite(l51,l51_estado);
digitalWrite(l50,l50_estado);
digitalWrite(l49,l49_estado);
digitalWrite(l48,l48_estado);
digitalWrite(l47,l47_estado);
digitalWrite(l46,l46_estado);
digitalWrite(l45,l45_estado);
digitalWrite(l44,l44_estado);
digitalWrite(l43,l43_estado);
digitalWrite(l42,l42_estado);
digitalWrite(l41,l41_estado);
digitalWrite(l40,l40_estado);
digitalWrite(l39,l39_estado);
digitalWrite(l38,l38_estado);
digitalWrite(l37,l37_estado);
digitalWrite(l36,l36_estado);
digitalWrite(l35,l35_estado);
digitalWrite(l34,l34_estado);
digitalWrite(l52,l52_estado);
start=LOW;
}
}
} else {
// code for after the first press
l53_estado = HIGH;
digitalWrite(l53,l53_estado);
l52_estado = LOW;
digitalWrite(l52,l52_estado);
}
}
What is wrong with the code?
Can I do what I want with a push button
Thany you for your help