Error Code, "No if before else"

Hello, so I'm creating a little puzzle using 8 fuse switches (RCD).

The correct pattern of switches need to be set to activate a lock release (in this case (Down, Up,Down,Up,Down, Down, Down, Up)

Having issue with the error mesagge
"Arduino: 1.8.12 (Mac OS X), Board: "Arduino Uno"

Buttontest23:61:1: error: expected unqualified-id before 'else'
else {
^~~~
Buttontest23:67:1: error: expected declaration before '}' token
}
^
exit status 1
expected unqualified-id before 'else'
"

Below is my code;

const int greenLED =  10;        
const int redLED = 11;
   
void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);


  
 pinMode(greenLED, OUTPUT);


  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, HIGH);

  
}

void loop() {
  //read the pushbutton value into a variable
  int fuse1 = digitalRead(2);
  int fuse2 = digitalRead(3);
  int fuse3 = digitalRead(4);
  int fuse4 = digitalRead(5);
  int fuse5 = digitalRead(6);
  int fuse6 = digitalRead(7);
  int fuse7 = digitalRead(13);
  int fuse8 = digitalRead(12);
  //print out the value of the fuse
  Serial.print(fuse1);
  Serial.print(fuse2);
  Serial.print(fuse3);
  Serial.print(fuse4);
  Serial.print(fuse5);
  Serial.print(fuse6);
  Serial.print(fuse7);
  Serial.print(fuse8);
 Serial.println(F(" CODE ENTERED"));
  delay (1000);
  

   if ((digitalRead(fuse1) == 1  && digitalRead(fuse2) == 0 && digitalRead(fuse3) == 1 && digitalRead(fuse4) == 0 && digitalRead(fuse5) == 1 && digitalRead(fuse6) == 1 && digitalRead(fuse7) == 1  && digitalRead(fuse8) == 0));



digitalWrite(greenLED, HIGH); // turn the Green LED ON
digitalWrite(redLED, LOW); // turn the RED LED OFF
Serial.println(F("PUZZLE COMPLETE")); 
 delay (5000);
 }
else { 
digitalWrite(greenLED, LOW); // turn the Green LED OFF
digitalWrite(redLED, HIGH); // turn the RED LED ON
  Serial.println(F("PUZZLE INCOMPLETE"));
  delay (1000);
}
}

 

  
}

Any help is appreciated.

At a guess, bad breadboard connection to the LED.

Edit: As it seems the OP has modified their post (and completely changed context and the problem - I do wish people wouldn't do that) after I posted my reply, an update is in order...

The original posted problem was that ONE of the LEDs was working correctly, the other was turning on only intermittently and apparent randomly.
As the code changed (and still does) both LEDs in lock step, the most likely explanation (at that time) was faulty wiring.

I don't know what a fuse switch is but I know that this does not look correct:

   if ((digitalRead(fuse1) == 1 ... && digitalRead(fuse8) == 0));

Uh oh! Common mistake! That semicolon was probably supposed to be a curly bracket "{".

Also, when you are tempted to use numbers as the suffix to a name, it is time to learn about arrays.

Also, although 1 and 0 work, digitalRead(...) is documented to return HIGH and LOW.

Can you explain which semi colon you mean?

Labelled fuse as that's the switches I'm using, (as in the type that takes power into your home)

I know a bit about arrays but made this super quick.

This one (at the end of the line):

   if ((digitalRead(fuse1) == 1  && digitalRead(fuse2) == 0 && digitalRead(fuse3) == 1 && digitalRead(fuse4) == 0 && digitalRead(fuse5) == 1 && digitalRead(fuse6) == 1 && digitalRead(fuse7) == 1  && digitalRead(fuse8) == 0));

As written, that is a legal "if" statement but it won't do anything useful. If you do not understand what I mean, it is time to go back over your "C" lessons.

. . . && digitalRead(fuse8) == 0)); <———<<<<< remove the ;


Read Tip #13

Yes, but one must do more than remove the semicolon. There must be a "{" so that the "else" works.

Was in the midst of finding the url

Read Tip #13

:slight_smile:

If you properly indent your code (IDE tools -> autoformat), you get the below

const int greenLED =  10;
const int redLED = 11;

void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);



  pinMode(greenLED, OUTPUT);


  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, HIGH);


}

void loop() {
  //read the pushbutton value into a variable
  int fuse1 = digitalRead(2);
  int fuse2 = digitalRead(3);
  int fuse3 = digitalRead(4);
  int fuse4 = digitalRead(5);
  int fuse5 = digitalRead(6);
  int fuse6 = digitalRead(7);
  int fuse7 = digitalRead(13);
  int fuse8 = digitalRead(12);
  //print out the value of the fuse
  Serial.print(fuse1);
  Serial.print(fuse2);
  Serial.print(fuse3);
  Serial.print(fuse4);
  Serial.print(fuse5);
  Serial.print(fuse6);
  Serial.print(fuse7);
  Serial.print(fuse8);
  Serial.println(F(" CODE ENTERED"));
  delay (1000);


  if ((digitalRead(fuse1) == 1  && digitalRead(fuse2) == 0 && digitalRead(fuse3) == 1 && digitalRead(fuse4) == 0 && digitalRead(fuse5) == 1 && digitalRead(fuse6) == 1 && digitalRead(fuse7) == 1  && digitalRead(fuse8) == 0));



  digitalWrite(greenLED, HIGH); // turn the Green LED ON
  digitalWrite(redLED, LOW); // turn the RED LED OFF
  Serial.println(F("PUZZLE COMPLETE"));
  delay (5000);
}
else {
  digitalWrite(greenLED, LOW); // turn the Green LED OFF
  digitalWrite(redLED, HIGH); // turn the RED LED ON
  Serial.println(F("PUZZLE INCOMPLETE"));
  delay (1000);
}
}




}

If, after an if, a line or block is not indented, it means that the if-block is completed. Usually because of a semicolon at the end of the if.

  if ((digitalRead(fuse1) == 1 ... digitalRead(fuse8) == 0))[b][color=red];[/color][/b]

After removing that semicolon and applying the autoformat

  if ((digitalRead(fuse1) == 1  && digitalRead(fuse2) == 0 && digitalRead(fuse3) == 1 && digitalRead(fuse4) == 0 && digitalRead(fuse5) == 1 && digitalRead(fuse6) == 1 && digitalRead(fuse7) == 1  && digitalRead(fuse8) == 0))



    digitalWrite(greenLED, HIGH); // turn the Green LED ON
  digitalWrite(redLED, LOW); // turn the RED LED OFF

Now only one line is indented; that indicates that only that line is depending on the if. Looking at your code, you're missing a { as already indicated.
After adding that and applying autoformat again, it will look like below

  if ((digitalRead(fuse1) == 1  && digitalRead(fuse2) == 0 && digitalRead(fuse3) == 1 && digitalRead(fuse4) == 0 && digitalRead(fuse5) == 1 && digitalRead(fuse6) == 1 && digitalRead(fuse7) == 1  && digitalRead(fuse8) == 0))
  {
    ...
    ...
  }
  else {
    ...
    ...
  }
}


}

Now you have one problem left. After an autoformat, you should never have more than 1 } at the beginning of your last lines. The } on the last line should be removed.

Autoformat is your friend !

Use my code. I have edited your code and corrected your errors.

const int greenLED =  10;        
const int redLED = 11;
  
void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin 2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);


  
 pinMode(greenLED, OUTPUT);


  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, HIGH);

  
}

void loop() {
  //read the pushbutton value into a variable
  int fuse1 = digitalRead(2);
  int fuse2 = digitalRead(3);
  int fuse3 = digitalRead(4);
  int fuse4 = digitalRead(5);
  int fuse5 = digitalRead(6);
  int fuse6 = digitalRead(7);
  int fuse7 = digitalRead(13);
  int fuse8 = digitalRead(12);
  //print out the value of the fuse
  Serial.print(fuse1);
  Serial.print(fuse2);
  Serial.print(fuse3);
  Serial.print(fuse4);
  Serial.print(fuse5);
  Serial.print(fuse6);
  Serial.print(fuse7);
  Serial.print(fuse8);
 Serial.println(F(" CODE ENTERED"));
  delay (1000);
  

   if (digitalRead(fuse1) == 1  && digitalRead(fuse2) == 0 && digitalRead(fuse3) == 1 && digitalRead(fuse4) == 0 && digitalRead(fuse5) == 1 && digitalRead(fuse6) == 1 && digitalRead(fuse7) == 1  && digitalRead(fuse8) == 0)
{
digitalWrite(greenLED, HIGH); // turn the Green LED ON
digitalWrite(redLED, LOW); // turn the RED LED OFF
Serial.println(F("PUZZLE COMPLETE"));
 delay (5000);
 }


else 
{
digitalWrite(greenLED, LOW); // turn the Green LED OFF
digitalWrite(redLED, HIGH); // turn the RED LED ON
  Serial.println(F("PUZZLE INCOMPLETE"));
  delay (1000);
}

}

Sorry if some errors still exists in the syntax, because my PC is having some problems and I am posting this on my phone, but I believe that this code will work fine.

..Arnav

ArnavPawarAA:
I have edited your code and corrected your errors.

  digitalWrite(redLED, HIGH);

Best set redLED to be an OUTPUT first, otherwise you're just waggling the pullup.
It may "work", but it'll be dim.

Note that it is OK to put line breaks in an expression to make your lines more readable. The line break is treated like a space character.

  if (digitalRead(fuse1) == 1  &&
      digitalRead(fuse2) == 0 &&
      digitalRead(fuse3) == 1 &&
      digitalRead(fuse4) == 0 &&
      digitalRead(fuse5) == 1 &&
      digitalRead(fuse6) == 1 &&
      digitalRead(fuse7) == 1  &&
      digitalRead(fuse8) == 0)
  {
    digitalWrite(greenLED, HIGH); // turn the Green LED ON
    digitalWrite(redLED, LOW); // turn the RED LED OFF
    Serial.println(F("PUZZLE COMPLETE"));
    delay (5000);
  }
  else