Basic Arduino programming help

Hey there, I’m making a basic Arduino game for a school project, the idea is to have 3 lights, 2 red and 1 green. It constantly cycles through the three with the green in the middle, the user needs to click the button while the light is green to win, if the light is red he loses, it’s kind of like a rythm / reaction game. I’ve been trying to program it but nothing seems to work. I am a fairly new user so I don’t quite know everything there is to know, can anyone see an obvious error my my code that would cause it not to work?

Code:
#include “pitches.h”

int led1 = 13;
int led2 = 12;
int led3 = 11;
const int threshold = 10;
int i = 1;
int notes = {
NOTE_A4, NOTE_B4,NOTE_C4,NOTE_AS4,NOTE_D4 };

void setup() {

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}

void loop() {
for(i=0; i<3; i++) {
if (i=0) {
digitalWrite(led1, HIGH);
delay(200);
digitalWrite(led2, LOW);
delay(0);
digitalWrite(led3, LOW);
delay(0);
int sensorReading = analogRead(0);
if (sensorReading > threshold) {
tone(8, NOTE_C4, 20);
tone(8, NOTE_D4, 20);
}
}
if (i=1) {
digitalWrite(led2, HIGH);
delay(200);
digitalWrite(led1, LOW);
delay(0);
digitalWrite(led3, LOW);
delay(0);
int sensorReading = analogRead(0);
if (sensorReading > threshold) {
tone(8, NOTE_A4, 20);
tone(8, NOTE_AS4, 20);
tone(8, NOTE_B4, 20);
tone(8, NOTE_C4, 20);
}
}
if (i=2) {
digitalWrite(led3, HIGH);
delay(200);
digitalWrite(led1, LOW);
delay(0);
digitalWrite(led2, LOW);
delay(0);
int sensorReading = analogRead(0);
if (sensorReading > threshold) {
tone(8, NOTE_C4, 20);
tone(8, NOTE_D4, 20);

}
}
}
}

You are saying “if (i=0)” when you mean “if (i == 0)”.

Since things happen in sequence anyway you can get rid of the loop since each time through the loop a different block of code is executed:

  for(i=0; i<3; i++) {   
    if (i=0) {
      digitalWrite(led1, HIGH);
      delay(200);
      digitalWrite(led2, LOW);
      delay(0);
      digitalWrite(led3, LOW);
      delay(0);
      int sensorReading = analogRead(0);
       if (sensorReading > threshold) {      
        tone(8, NOTE_C4, 20);
        tone(8, NOTE_D4, 20);
       }
    }
     if (i=1) {
      digitalWrite(led2, HIGH);
      delay(200);
      digitalWrite(led1, LOW);
      delay(0);
      digitalWrite(led3, LOW);
      delay(0);
      int sensorReading = analogRead(0);
       if (sensorReading > threshold) {      
        tone(8, NOTE_A4, 20);
        tone(8, NOTE_AS4, 20);
        tone(8, NOTE_B4, 20);
        tone(8, NOTE_C4, 20);
       }
    }
     if (i=2) {
      digitalWrite(led3, HIGH);
      delay(200);
      digitalWrite(led1, LOW);
      delay(0);
      digitalWrite(led2, LOW);
      delay(0);
      int sensorReading = analogRead(0);
       if (sensorReading > threshold) {      
        tone(8, NOTE_C4, 20);
        tone(8, NOTE_D4, 20);
       
      }
    }

does the same thing as:

      digitalWrite(led1, HIGH);
      delay(200);
      digitalWrite(led2, LOW);
      delay(0);
      digitalWrite(led3, LOW);
      delay(0);
      int sensorReading = analogRead(0);
       if (sensorReading > threshold) {      
        tone(8, NOTE_C4, 20);
        tone(8, NOTE_D4, 20);
       }


      digitalWrite(led2, HIGH);
      delay(200);
      digitalWrite(led1, LOW);
      delay(0);
      digitalWrite(led3, LOW);
      delay(0);
      int sensorReading = analogRead(0);
       if (sensorReading > threshold) {      
        tone(8, NOTE_A4, 20);
        tone(8, NOTE_AS4, 20);
        tone(8, NOTE_B4, 20);
        tone(8, NOTE_C4, 20);
       }



      digitalWrite(led3, HIGH);
      delay(200);
      digitalWrite(led1, LOW);
      delay(0);
      digitalWrite(led2, LOW);
      delay(0);
      int sensorReading = analogRead(0);
       if (sensorReading > threshold) {      
        tone(8, NOTE_C4, 20);
        tone(8, NOTE_D4, 20);
      }

Of course,

delay(0);

does nothing but look silly.

Names like lftLedPin, ctrLedPin, and rgtLedPin would really help to know which LED pin you are referring to.

Notice, also that while delay() is happening, your are NOT reading the switch state. So, there is no way for a use to press a switch while a LED is on.

Look at the blink without delay example. Delete all your code, and start over. You can get there, but you aren't even heading in vaguely the correct direction.

weded:
Hey there, I’m making a basic Arduino game for a school project …

Read this before posting a programming question