what function can be used to restart a game ?

in the code : can we use the function loop(); in the loop in order to restart game ?

code is attached below

& this game idea is that ... when the third LED is on , the player should press the button . Otherwise he will lose the game !
each round the speed becomes much faster

codew.ino (10.7 KB)

Thank you for the helpful answer :slight_smile:

do you have any clue to what should i use instead ?

here is what i am trying to do :
if (digitalRead(button)==HIGH && digitalRead(pin1)==HIGH){
off();
loop(); // << wrong
}

i am making a game , and if this condition occurs ^^^^ the game should come to an end and to restart game ...
any other function to use ??

You could set a variable, then use it to bypass the reset of the code that follows but, as Delta_G has said, we need to see your code if you want the help you are looking for.

the code is long it can't be posted here :frowning:

void gameround1(){
  digitalWrite(pin1,HIGH);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin1)==HIGH){
    off();
    loop();
  }
  digitalWrite(pin1,LOW);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin1)==LOW){
    off();
    loop();
  }

    //-
  digitalWrite(pin2,HIGH);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin2)==HIGH){
    off();
    loop();
  }
  digitalWrite(pin2,LOW);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin2)==LOW){
    off();
    loop(); 
  }
  
 //-
   digitalWrite(pin3,HIGH);
   delay(1000);
   if(digitalRead(button)==HIGH&& digitalRead(pin3)==HIGH){
     digitalWrite(pin3,LOW);
     delay(1000);
     
   }
   if(digitalRead(button)==LOW&&digitalRead(pin3)==HIGH){
     off();
     loop();
   }
   //-
  digitalWrite(pin4,HIGH);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin4)==HIGH){
    off();
    loop();
  }
  digitalWrite(pin4,LOW);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin4)==LOW){
    off();
    loop(); 
  }

   //-
  digitalWrite(pin5,HIGH);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin5)==HIGH){
    off();
    loop();
  }
  digitalWrite(pin5,LOW);
  delay(1000);
  if (digitalRead(button)==HIGH && digitalRead(pin5)==LOW){
    off();
    loop(); 
  }
}
//---------------------------

this code then will go under the void loop !

the idea is that if the button is pressed while a particular led is on the game will restart !

the idea is that if the button is pressed while a particular led is on the game will restart

You can not implement that idea with that method.

the code is long it can't be posted here

It can be attached.

You need to implement a state machine:-
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

For a full project that uses a state machine of this sort see:-
http://www.thebox.myzen.co.uk/Hardware/Dice_Game.html

NihadAbuHummos:
the code is long it can't be posted here :frowning:

void gameround1(){

digitalWrite(pin1,HIGH);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin1)==HIGH){
   off();
   loop();
 }
 digitalWrite(pin1,LOW);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin1)==LOW){
   off();
   loop();
 }

//-
 digitalWrite(pin2,HIGH);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin2)==HIGH){
   off();
   loop();
 }
 digitalWrite(pin2,LOW);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin2)==LOW){
   off();
   loop();
 }
 
//-
  digitalWrite(pin3,HIGH);
  delay(1000);
  if(digitalRead(button)==HIGH&& digitalRead(pin3)==HIGH){
    digitalWrite(pin3,LOW);
    delay(1000);
   
  }
  if(digitalRead(button)==LOW&&digitalRead(pin3)==HIGH){
    off();
    loop();
  }
  //-
 digitalWrite(pin4,HIGH);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin4)==HIGH){
   off();
   loop();
 }
 digitalWrite(pin4,LOW);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin4)==LOW){
   off();
   loop();
 }

//-
 digitalWrite(pin5,HIGH);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin5)==HIGH){
   off();
   loop();
 }
 digitalWrite(pin5,LOW);
 delay(1000);
 if (digitalRead(button)==HIGH && digitalRead(pin5)==LOW){
   off();
   loop();
 }
}
//---------------------------




this code then will go under the void loop ! 

the idea is that if the button is pressed while a particular led is on the game will restart !

Note : Putting pinX instead of just X is not really required - 8 is not going to be assigned to the 5th pin for example, you don't need to assign that. It's just wasting a couple bits of memory (not sure if the compiler is optimizing it though).
Though you may want to assign a const named "delay" instead of changing your 1000s all time.

setup() is called on the beginning.
loop() should never be called by yourself, as Delta_G told you.

Without the full code we hardly can understand what do you precisely want to do. Please post your code to pastebin (also take the C syntax highlighting so it's easier for us to read) and give the link there - We can give you tips to improve your game structure instead so you don't face similar problems later.

A way to do it would be : Let's say the LED you're talking about is indicating game over.
Define a variable named "is_game_over".
Make a function named set_gameover(). There write the light state to the game.
Then in the loop() (as said previously, don't call it manually), check if is_game_over true and then if the button is pressed. If those are true, call a function resetting your game.

Saturos:
Please post your code to pastebin (also take the C syntax highlighting so it's easier for us to read) and give the link there.

Please do not use pastbin just attach it to your post.

Please read the how to use this forum sticky.

Please edit your first post to give this thread a sensible title. Are not virtually all the posts in this forum questions?