SOLVED Newbie help, code missing a ;

Hello,

I am getting an error message that my code is missing a ;

Sorry for the simple question but I can't find the spot.

void loop(){        
           
    if (digitalRead(switchPin) == LOW) { // button is on

            // spike 1
            spike(3, 0.3, 0.2, 0.4, 0.4);
            noTone(8);
            delay(600);
          
            // spike 2
            spike(3, 0.3, 0.4, 0.5, 0.5);
            noTone(8);
            delay(400);
           
        }
       
      else (digitalRead(switchPin) == HIGH) { // button is off
      
    noTone(8);
    
    }  
       
   } 

What line does the complier tell you where the error is ?

Dont post code fragments that dont compile, very often the error is in the code you do not post.

Maybe the problem is in the code that you did not post

Please post the full sketch and the full error message

An unrelated question for you

  else (digitalRead(switchPin) == HIGH)   // button is off

what is this line of code supposed to do ?

Thank you taking the time to reply. The compiler says it is in the line:
else (digitalRead(switchPin) == HIGH) { // button is off

Thank you for helping. It is supposed to turn off the sound.

Sorry, I'm new to coding, the compiler says it is in this part of the code so I just posted this part.

Then maybe there is an error there ?

Do a Google search on;

Arduino Reference else

To see hints on what might be wrong ...................

Hi, I have searched and looked at samples, I can't find it so I thought I would ask for help.

post all the code, please.

Delete everything between the else and opening brace.

This is the error:

In function 'void loop()':
error: expected ';' before '{' token
else (digitalRead(switchPin) == HIGH) { // button is off
^
exit status 1
expected ';' before '{' token

I was just thinking something like that, wondering if the code was copied from an internet page and pasted into the IDE.

You don't need a condition for the else

First item in the list for the above search points to;

and;

Syntax

if (condition1) {
  // do Thing A
}
else if (condition2) {
  // do Thing B
}

else {
  // do Thing C
}

Note the syntax should be else {

Try:
else if(digitalRead(switchPin) == HIGH) // button is off
Or:

else
  noTone(8);

that’s where the compiler expects a semicolon, but if you put one there it won’t fix it other than making it syntactically correct.

autoformat your code using the IDE tool, review the syntax for if/else statements and you will probably spot your real error.

a7

Thank you for taking the time to help. It did solve the issue. I will try the autoformat.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.