First Arduino Project

Hi,

I have made a Simon Says game with Arduino. The goal was to learn the programming syntax. I am not a programmer but it seemed pretty easy to learn. I posted the code below.
I do have a question. Using for loops, whenever I try to use for(i=0; i==5;i++){} the loop does not work. I have to use i<=5 instead, which doesn’t make sense to me. Why can’t i use ==?

Below is my code for review. Everything works as designed but there are always improvements to make code more efficient. Since I am just starting out, any suggestions are welcome.

/* Arduino based Simon-Says Game
Learning exercise written by Patrick J.
*/

// define variables
int led = {2,3,4,5};
int b = {8,9,10,11};
int sequence[100];
int rounds=0;

void setup(){
for(int i=0; i<=3; i++){
pinMode(led*, OUTPUT); //set LED pins as output*
_ pinMode(b*, INPUT); //set button pins as input*_
_ digitalWrite(b*, HIGH);//enable internal pullups*
* }
randomSeed(analogRead(0));//set random seed from unused analog input*_

}
void loop(){

* waitForInput(); //press any button to start *
* int counter = 0; //initialize counter variable to track game progress*
* boolean mistake = false; //mistake variable for tracking mistakes*

* run(3); //start of game*

* while(mistake == false){ //as long as no mistakes have been made remain in this loop*
* int currentled=random(4); // pick a random LED to light up*
* sequence[counter]=currentled; //assign random LED to sequence array*
* for(int i=0; i<=counter; i++){ //display sequence*
_ lightled(sequence*,1500); //light up each LED for 1.5 seconds*
* } //end display sequence*_

* for(int i=0; i<=counter; i++){ //repeat LED sequence through button presses*
* waitForInput(); //waitForInput function pauses loop() until a button is pressed*
* int x=button_press(); //button_press function tells us which of the four buttons has been pressed*

* lightled(x,250); //lightled function lights up the LED corresponding to the button just pressed for 250 milliseconds*
_ if(x !=sequence*){ // compares the button pressed to the value stored in the sequence array*
* mistake=true; // if it doesn’t match we set mistake to true to end the while loop*
* break; // and break out of the for loop immediately*
* }
} // end for loop*

* counter++; //increment the counter to add and addional LED to the sequence*
* run(0); // signal the beginning of the next turn through running led lights*
* } //end while loop*_

* run(2); //signal to the user that a mistake has been made throug a longer sequence of running lights *

* delay(1000);*
* flash(counter-2); //signal to the user how many correct turns were accomplished*
* delay(1000);*

* //at this point the game starts over*

}//end of loop()
void flash(int times){
* for(int flashes =0; flashes <=times; flashes++){*
* for(int i=0; i<=3; i++){*
_ digitalWrite(led*, HIGH);
}
delay(500);
for(int i=0; i<=3; i++){
digitalWrite(led, LOW);
}
delay(500);
}
}
void run(int times){
for(int runs=0; runs<=times; runs++){*_

* for(int i=0; i<=3; i++){*
_ digitalWrite(led*,HIGH);
delay(50);
digitalWrite(led,LOW);
}
for(int i=3; i>=0; i–){
digitalWrite(led,HIGH);
delay(50);
digitalWrite(led,LOW);
}
}
}*_

int button_press(){

* for(int i=0; i<=3; i++){*
_ switch (digitalRead(b*)){
case HIGH:
//no button was pressed*

* break;
case LOW:
//button was pressed, return the button number*

* return i;
break;
}
}*_

}
void lightled(int x, int duration){
digitalWrite(led[x],HIGH);
delay(duration);
digitalWrite(led[x],LOW);
if(duration > 500){
* delay(duration/2);*
}

}
void waitForInput(){
* while(digitalRead(b[0])==HIGH && digitalRead(b[1])==HIGH && digitalRead(b[2])==HIGH && digitalRead(b[3])==HIGH){*
* //do nothing until a button has been pressed*
* }*

}

Hi,

there are 3 expressions in a for statement, the first one initializes the loop variable with a start value, the second expression is a condition, the loop is executed as long as this expression is true. The third expression is calulated at the end of each loop.

Example: for (i=0; i==5;i++) …
i is set to 0
the i==5 is tested, the result is false, so the loop is not executed

for (i=0: i<= 5; i++) …
i is set to 0
then i<=5 is testetd and gives true as long for i=0,1,2,3,4,5
so the loop is executed, at the end i is increased by 1 (i++).

Hope its a litle bit clearer now
Mike

mike_pa,

thanks, that explained it very well. No it makes perfect sense. I thought the loop runs until the condition is true, but instead it runs while the condition is true.

The while statement is the one that runs until the condition is false :slight_smile:
Like for example

int i=0; while(i!=5){
...
i++;
}

But in this case you for(){} is much better!

Have fun!