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);
}
}
srnet
February 18, 2022, 3:45pm
2
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.
srnet
February 18, 2022, 3:49pm
7
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.
arduinokov:
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 { // button is off
noTone(8);
}
}
You don't need a condition for the else
srnet
February 18, 2022, 3:55pm
14
srnet:
Arduino Reference else
First item in the list for the above search points to;
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
and;
Syntax
if (condition1) {
// do Thing A
}
else if (condition2) {
// do Thing B
}
else {
// do Thing C
}
Note the syntax should be else {
JCA34F
February 18, 2022, 4:35pm
15
Try:
else if(digitalRead(switchPin) == HIGH) // button is off
Or:
else
noTone(8);
alto777
February 18, 2022, 4:39pm
16
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.
system
Closed
August 17, 2022, 4:58pm
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.