Error Message: Expected unidentified -id before "if"

Hi! I'm working on this code and it was working fine until I started messing with it, then I deleted the things I added and tried uploading it but I keep getting "Expected Unidentified -id before "if".
I'm in desperate need of help, so any advice will be much appreciated thank you!

*/ 
// declare what constants won't change. They are used here to set pin numbers.

const int buttonPin1 = 10; // first pushbutton on pin 10 
const int buttonPin2 = 11; // second pushbutton on pin 11
const int buttonPin3 = 12; // third pushbutton on pin 12 
const int buttonPin4 = 13; // fourth pushbutton on pin 13

const int ledPin1 = 5; // ledPin on pin 5
const int ledPin2 = 6;
const int speaker = 7;


// declare what variables will change: 

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0; 
int buttonState4 = 0; 


void setup () { // run once at sketch startup 
Serial.begin(9600); // begin serial 

pinMode (buttonPin1, INPUT); // buttonPin1 as input 
pinMode (buttonPin2, INPUT); // buttonPin2 as input 
pinMode (buttonPin3, INPUT); // buttonPin3 as input 
pinMode (buttonPin4, INPUT); // buttonPin4 as input 

pinMode (ledPin1, OUTPUT); // ledPin1 as output  
pinMode (ledPin2, OUTPUT);
pinMode (speaker, OUTPUT);
}

void loop () { 
  digitalWrite (ledPin1, LOW);
  buttonState1 = digitalRead (buttonPin1);
 // Serial.println (buttonState1);
   buttonState2 = digitalRead (buttonPin2);
    //Serial.println (buttonState2); 
     buttonState3 = digitalRead (buttonPin3);
   //Serial.println (buttonState3);
     buttonState4 = digitalRead (buttonPin4);
   //Serial.println (buttonState4);
}

if(digitalRead (buttonPin4) ==LOW) {
  Serial.println ("4");
  delay (1000);
if ((digitalRead (buttonPin1) ==LOW) && (digitalRead (buttonPin2)== LOW)) {
    Serial.println ("1 and 2");
    delay (1000);
if (digitalRead (buttonPin3) ==LOW) {
      Serial.println ("3");
      delay (1000);
    digitalWrite (ledPin1, HIGH);
    tone (speaker, 125);
    delay (2000);
    noTone (speaker);
} else {
    digitalWrite( ledPin1, HIGH);
    tone (speaker, 400);
    delay (1000);
    noTone (speaker);
    digitalWrite (ledPin2, LOW);
  } 
}

You have code that is required to be in a function but is not. Count the curly brackets or look at the indentation used by the IDE autoformat feature.

first thing to do is
pressing ctrl-t in the adrduino-ide to autoformat indentions

then do a right-click with the mouse and choose copy for forum
then use ctrl-v to paste clipboard content into a posting.
best regards Stefan

get used to the habit to save code wirg save as and an added version number. Then it will be very easy to roll back to a codeversion that is working
brs

sketch_apr17a:46:1: error: expected unqualified-id before 'if'
 if (digitalRead (buttonPin4) == LOW)
 ^~

The full error message (in the box below your sketch) tells you what line it found the mistake at. In this case, Line 46. That will often tell you that an extra or missing curly-bracket ('{' to '}') is extra or missing just above the error.

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