ettu
May 5, 2020, 5:57am
1
Hello!
I have a little problem.
How to..
The red LEDs flash alternately indefinitely.
When the button is pressed, the red LEDs will stop flashing and the green LED will light up.
As long as the button is held down, the green LED will light up and the red LEDs will not flash.
When the button is released, the green LED will turn off and the red LEDs will flash again.
The whole cycle repeats indefinitely.
Can somebody help me? :))
ettu
May 5, 2020, 6:29am
3
im so new at this but could this one work? And sorry i really don
t understand why one line is red - what`s the problem?
Please take a look at Read this before posting a programming question then follow the instructions to post your code and any error messages rather than a picture of it
pcbbc
May 5, 2020, 6:46am
5
Semi colon at the end maybe?
Do any other if statements examples you’ve seen have a semicolon there?
Edit: Also, as you are reading from pin 2 (presumably your button), do you think pinMode(2, OUTPUT) is the correct choice?
This code can help. Just put your button pin, redled pin and greenled pin name in the proper place.
int redled = ?;
int greenled = ?;
int button = ?;
int reddelay;
int greendelay;
bool switchpos = 0;
bool first = 0;
void setup(){
pinMode(button, INPUT);
}
void loop(){
check();
}
void check(){
switchpos = digitalRead(button);
if(switchpos == 0 && first == 1) blinkred();
if(switchpos == 1 && first == 0) blinkgreen();
}
void blinkred(){
check();
digitalWrite(greenled, LOW);
digitalWrite(redled, HIGH);
reddelay = 0;
delayred();
digitalWrite(redled, LOW);
reddelay = 0;
delayred();
void blinkgreen(){
check();
digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
greendelay = 0;
delayrgreen();
digitalWrite(greenled, LOW);
greendelay = 0;
delaygreen();
void delayred(){
while(reddelay < 200){
reddelay++;
delay(1);
check();
}
}
void delaygreen(){
while(greendelay < 200){
greendelay++;
delay(1);
check();
}
}
}
Please check the code again and try it. Reply if it works. Just put your led pin numbers in the int redled and int greenled and tg button pin number in the int button. Reply if it works.