Programming leds to stop blinking when button is pushed and another to blink

Hello!
I have a little problem.

How to..

  1. The red LEDs flash alternately indefinitely.

  2. When the button is pressed, the red LEDs will stop flashing and the green LED will light up.

  3. As long as the button is held down, the green LED will light up and the red LEDs will not flash.

  4. When the button is released, the green LED will turn off and the red LEDs will flash again.

  5. The whole cycle repeats indefinitely.

Can somebody help me? :))

Post what you have tried

im so new at this but could this one work? And sorry i really dont understand why one line is red - what`s the problem?

problemm.jpg

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

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.