Error Message, 'else' without a previous 'if'

Good day, I’m new to programming and I am currently working on a traffic light signal to be controlled by 2 switches for different functions, however, I am getting an error message stating i have an else without a previous if. Any advice?

//buttons

int button1 = 2;

int button2 = 3;

// light one

int red1 = 11;

int yellow1 = 12;

int green1 = 7;

// light two

int red2 = 8;

int yellow2 = 9;

int green2 = 10;

void setup() {

// light one

pinMode(red1, OUTPUT);

pinMode(yellow1, OUTPUT);

pinMode(green1, OUTPUT);

// light two

pinMode(red2, OUTPUT);

pinMode(yellow2, OUTPUT);

pinMode(green2, OUTPUT);

//buttons
pinMode (button1, INPUT);
pinMode (button2, INPUT);

}

void loop() {
if (digitalRead(button1) == HIGH)
(digitalRead(button2) == LOW);

for (int thisPin = 7; thisPin < 12; thisPin++)
{

digitalWrite(green1, HIGH);
digitalWrite(yellow1, LOW);
digitalWrite(red2, HIGH);
delay(15000);
digitalWrite(red2, HIGH);
digitalWrite(yellow1, HIGH);
digitalWrite(green1, LOW);
delay(3000);
digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
digitalWrite(green2, HIGH);
digitalWrite(red2, LOW);
delay(10000);
digitalWrite(green2, LOW);
digitalWrite(yellow2, HIGH);
digitalWrite(red1, HIGH);
delay(3000);

}

if (digitalRead(button1) == LOW)
(digitalRead(button2) == HIGH);

for (int thisPin = 11; thisPin = 8; thisPin++)
{
digitalWrite(red1, HIGH);
digitalWrite(red2, HIGH);
digitalWrite(yellow1, LOW);
digitalWrite(yellow2, LOW);
digitalWrite(green1, LOW);
digitalWrite(green2, LOW);
delay(3000);
}
else
{
return
}

}

just guessing here but are your switch conditions:

switch1=HIGH and switch2=LOW → execute function 1

AND

switch1=LOW and switch2=HIGH → execute function 2

if yes, then I think this is what you may have intended to code (code compiles, not tested!):

//buttons

int button1 = 2;

int button2 = 3;


// light one

int red1 = 11;

int yellow1 = 12;

int green1 = 7;



// light two

int red2 = 8;

int yellow2 = 9;

int green2 = 10;



void setup() {



  // light one

  pinMode(red1, OUTPUT);

  pinMode(yellow1, OUTPUT);

  pinMode(green1, OUTPUT);




  // light two

  pinMode(red2, OUTPUT);

  pinMode(yellow2, OUTPUT);

  pinMode(green2, OUTPUT);

  //buttons
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);


}

void loop() {
  if (digitalRead(button1) == HIGH && digitalRead(button2) == LOW) {


    for (int thisPin = 7; thisPin < 12; thisPin++)
    {

      digitalWrite(green1, HIGH);
      digitalWrite(yellow1, LOW);
      digitalWrite(red2, HIGH);
      delay(15000);
      digitalWrite(red2, HIGH);
      digitalWrite(yellow1, HIGH);
      digitalWrite(green1, LOW);
      delay(3000);
      digitalWrite(yellow1, LOW);
      digitalWrite(red1, HIGH);
      digitalWrite(green2, HIGH);
      digitalWrite(red2, LOW);
      delay(10000);
      digitalWrite(green2, LOW);
      digitalWrite(yellow2, HIGH);
      digitalWrite(red1, HIGH);
      delay(3000);

    }
  }
  else if (digitalRead(button1) == LOW && digitalRead(button2) == HIGH) {

    for (int thisPin = 11; thisPin > 8; thisPin--)
    {
      digitalWrite(red1, HIGH);
      digitalWrite(red2, HIGH);
      digitalWrite(yellow1, LOW);
      digitalWrite(yellow2, LOW);
      digitalWrite(green1, LOW);
      digitalWrite(green2, LOW);
      delay(3000);
    }
  }
  else
  {
    return;
  }

}

hope that helps…

Please enclose your code in a code block.

Each of your ‘if’ statements does nothing because there is a semicolon after them which results in executing nothing if they are true. You should remove those semicolons. Also you can remove the ‘else return’ as it accomplishes nothing, because the loop will return anyway.

What is the purpose of using ‘thisPin’ in your for loops? The first for loop iterates 5 times so there is no reason to iterate from 7 to 11. The second for loop will iterate forever because ‘thisPin=8’ is an assignment and not a conditional and will always be true.

// buttons

int button1 = 2;

int button2 = 3;


// light one

int red1 = 11;

int yellow1 = 12;

int green1 = 7;



// light two

int red2 = 8;

int yellow2 = 9;

int green2 = 10;



void setup() {



  // light one

  pinMode(red1, OUTPUT);

  pinMode(yellow1, OUTPUT);

  pinMode(green1, OUTPUT);




  // light two

  pinMode(red2, OUTPUT);

  pinMode(yellow2, OUTPUT);

  pinMode(green2, OUTPUT);

  //buttons
  pinMode (button1, INPUT);
  pinMode (button2, INPUT);


}

void loop() {
  if (digitalRead(button1) == HIGH)
    (digitalRead(button2) == LOW); 

  for (int thisPin = 7; thisPin < 12; thisPin++)
  {

    digitalWrite(green1, HIGH);
    digitalWrite(yellow1, LOW);
    digitalWrite(red2, HIGH);
    delay(15000);
    digitalWrite(red2, HIGH);
    digitalWrite(yellow1, HIGH);
    digitalWrite(green1, LOW);
    delay(3000);
    digitalWrite(yellow1, LOW);
    digitalWrite(red1, HIGH);
    digitalWrite(green2, HIGH);
    digitalWrite(red2, LOW);
    delay(10000);
    digitalWrite(green2, LOW);
    digitalWrite(yellow2, HIGH);
    digitalWrite(red1, HIGH);
    delay(3000);

  }
  
  if (digitalRead(button1) == LOW)
     (digitalRead(button2) == HIGH);

  for (int thisPin = 11; thisPin = 8; thisPin++)
  {
    digitalWrite(red1, HIGH);
    digitalWrite(red2, HIGH);
    digitalWrite(yellow1, LOW);
    digitalWrite(yellow2, LOW);
    digitalWrite(green1, LOW);
    digitalWrite(green2, LOW);
    delay(3000);
  }
  else
  {
    return
  }

}