[HELP] Start LED blink by buttonclick, stop by buttonclick

Hello, i m programming a traffic light and i need some help.
I want my yellow traffic light to blink after button click until the button gets clicked again.

Version - Start blink by buttonclick for 10 seconds and stop afterwards.
This version works, but its not the way i want it to be.

int carred = 8, caryellow = 9, cargreen = 10, pedred = 12, pedgreen = 13, pedbutton = 4, nightbutton = 5;
const int buzzer = 3;


void setup()
{  
	//LED init
  pinMode(carred, OUTPUT);
  pinMode(caryellow, OUTPUT);
  pinMode(cargreen, OUTPUT);
  pinMode(pedred, OUTPUT);
  pinMode(pedgreen, OUTPUT);
	//Button init
  pinMode(pedbutton, INPUT); 
   //Buzzer init
  pinMode(buzzer, OUTPUT);
  
	//LED default 
	//(car=green;pet=red)
  digitalWrite(carred, LOW);
  digitalWrite(caryellow, LOW);
  digitalWrite(cargreen, HIGH);
  digitalWrite(pedred, HIGH); 
  digitalWrite(pedgreen, LOW);  
}
 
 
//Buzzer Sound when Ped is green and car red 
void Pedgreentone(){
int i;

for(i=0;i<10;i++){ 
	tone(buzzer, 1000); 
	delay(500);         
	noTone(buzzer);     
	delay(500);      
}
} 
 
 
void loop()
{
int i;

	//Night-Button = 1 (on_click)
  if(digitalRead(nightbutton) == HIGH)
  { 
	delay(2000);
	digitalWrite(carred, LOW);  
	digitalWrite(cargreen, LOW); 
	digitalWrite(pedred, LOW); 
	digitalWrite(pedgreen, LOW); 
	
	//Yellow light blink
	for(i=0;i<10;i++){
	digitalWrite(caryellow, HIGH);    
	delay(1000);                        
	digitalWrite(caryellow, LOW);    
	delay(1000);   
	} 
  }


	//Ped-Button = 1 (on_click)
  if(digitalRead(pedbutton) == HIGH)
  {
	//(car=yellow;pet=red)
	delay(5000);
	digitalWrite(caryellow, HIGH);  
	digitalWrite(cargreen, LOW);
	
	//(car=red;pet=red)
	delay(2000);
	digitalWrite(caryellow, LOW);
	digitalWrite(carred, HIGH);
	
	//(car=red;pet=green)
	delay(2000);
	digitalWrite(pedgreen, HIGH);
	digitalWrite(pedred, LOW);
	Pedgreentone(); 
	
	//(car=red;pet=red) 
	digitalWrite(pedgreen, LOW);
	digitalWrite(pedred, HIGH);
	
	//(car=red+yellow;pet=red)
	delay(2000);
	digitalWrite(caryellow, HIGH); 
	 
	//(car=green;pet=red)
	delay(2000);
	digitalWrite(carred, LOW); 
	digitalWrite(caryellow, LOW); 
	digitalWrite(cargreen, HIGH); 
  }
  
  
  //Ped-Button = 0
  else
  { 
    digitalWrite(pedred, HIGH); 
    digitalWrite(pedgreen, LOW);  
	digitalWrite(carred, LOW); 
	digitalWrite(caryellow, LOW); 
	digitalWrite(cargreen, HIGH); 
  }
}

Version - Start blink by buttonclick, ask for another buttonclick and stop afterwards.
This version doesn´t work. The problem is the delay(); the button clicks cant be read anymore. I tried to fix this with interupt already but i m not able to get it working. This is the way i need it to be.

int carred = 8, caryellow = 9, cargreen = 10, pedred = 12, pedgreen = 13, pedbutton = 4, nightbutton = 5, nightstatus = 0;
const int buzzer = 3;


void setup()
{  
	//LED init
  pinMode(carred, OUTPUT);
  pinMode(caryellow, OUTPUT);
  pinMode(cargreen, OUTPUT);
  pinMode(pedred, OUTPUT);
  pinMode(pedgreen, OUTPUT);
	//Button init
  pinMode(pedbutton, INPUT); 
   //Buzzer init
  pinMode(buzzer, OUTPUT);
  
	//LED default 
	//(car=green;pet=red)
  digitalWrite(carred, LOW);
  digitalWrite(caryellow, LOW);
  digitalWrite(cargreen, HIGH);
  digitalWrite(pedred, HIGH); 
  digitalWrite(pedgreen, LOW);  
}
 
 
//Buzzer Sound when Ped is green and car red 
void Pedgreentone(){
int i;

for(i=0;i<10;i++){ 
	tone(buzzer, 1000); 
	delay(500);         
	noTone(buzzer);     
	delay(500);      
}
} 
 
 
void loop()
{
int i;

if(digitalRead(nightbutton) == HIGH)
  { 
  nightstatus++;
  }


	//Night-Button = 1 (on_click)
  if((nightstatus % 2) == 1)
  { 
	delay(2000);
	digitalWrite(carred, LOW);  
	digitalWrite(cargreen, LOW); 
	digitalWrite(pedred, LOW); 
	digitalWrite(pedgreen, LOW); 
	
	//Yellow light blink
	while((nightstatus % 2) == 1)
	{    
	digitalWrite(caryellow, HIGH);    
	delay(1000);                        
	digitalWrite(caryellow, LOW);    
	delay(1000);  
	if(digitalRead(nightbutton) == HIGH)
	{ 
	nightstatus++;
	}
	}
  }


	//Ped-Button = 1 (on_click)
  if(digitalRead(pedbutton) == HIGH)
  {
	//(car=yellow;pet=red)
	delay(5000);
	digitalWrite(caryellow, HIGH);  
	digitalWrite(cargreen, LOW);
	
	//(car=red;pet=red)
	delay(2000);
	digitalWrite(caryellow, LOW);
	digitalWrite(carred, HIGH);
	
	//(car=red;pet=green)
	delay(2000);
	digitalWrite(pedgreen, HIGH);
	digitalWrite(pedred, LOW);
	Pedgreentone(); 
	
	//(car=red;pet=red) 
	digitalWrite(pedgreen, LOW);
	digitalWrite(pedred, HIGH);
	
	//(car=red+yellow;pet=red)
	delay(2000);
	digitalWrite(caryellow, HIGH); 
	 
	//(car=green;pet=red)
	delay(2000);
	digitalWrite(carred, LOW); 
	digitalWrite(caryellow, LOW); 
	digitalWrite(cargreen, HIGH); 
  }
  
  
  //Ped-Button = 0
  else
  { 
    digitalWrite(pedred, HIGH); 
    digitalWrite(pedgreen, LOW);  
	digitalWrite(carred, LOW); 
	digitalWrite(caryellow, LOW); 
	digitalWrite(cargreen, HIGH); 
  }
}

What happened to your post? Bit of a screaming mess :wink:

But the key problem here is, if you want to do multiple things (like blinking and reading switches) you can't use delay(). In fact, I still think chapter two in every Arduino Howto should be called "Now never use delay() again!". Delay() does exactly what it says on the tin, it waits and does nothing. And a (single core) processor can just do one thing at a time, even if that's waiting. See Blink without delay for how to do it differently. It's like using a clock.

And some quick tips
Watch you indentation! Jump in after a { and jump back after a }. And don't mix and match spaces and tabs (I guess you copied parts from the interwebs? :wink: ) Press Ctrl+T in the IDE to let the IDE help you format it nicely.

Variable names areMoreClearWhenYouCamelCase them :wink: (insteadofwritingthemasonebigmess)

int carred = 8, caryellow = 9, cargreen = 10, pedred = 12, pedgreen = 13, pedbutton = 4, nightbutton = 5, nightstatus = 0;

Yeah, it saves you from writing the variable type again, but does it really make things more readable?

Talking about variables, use the smallest size that fits. So for pins that's a byte (or uint8_t). Also, because they never change you can make them const. And I would make the names more clear:

//NOT
const byte CarRed = 8;
//but
const byte CarRedPin = 8;
int i;

for(i=0;i<10;i++){

You can just do that at the same time (and again, use variable that's just big enough). Also, spaces are free :wink:

for(byte i = 0; i < 10; i++){

And you check buttons to be HIGH so I assume external pull up resistors? You can save the hassle of the resistors by switching to the internal pull up resistors. Just use pinMode(pin, INPUT_PULLUP). Only note, now a closed switch will read LOW and an open switch HIGH. But that's just a matter of switching the logic in the code.

Well thanks for the quick tips. Yes i know my format is not nice, thats because i coded it in the basic windows editor. I got no IDE here its just a small testing project(my first one with aduino).

I know the problem is the delay function but i dont know any other way to realise this. I tried interupt but i probably did it wrong i guess.

arduinonub:
I got no IDE here its just a small testing project(my first one with aduino).

Why not? Just download it. There's even a non-install version that can run when you're not allowed to install anything on the machine.

arduinonub:
I know the problem is the delay function but i dont know any other way to realise this.

I just told you, the answer is in Blink without delay, go look that up :wink: Hint, millis() :wink:

arduinonub:
I tried interupt but i probably did it wrong i guess.

An interrupt is absolutely the wrong answer! That just drags you down into another rabbit hole :wink: