arduino 'currentState' dose not name a type

Hi i'm new to the arduino and have play with some of the easier projects and wanted to try something bigger so i triedto make a toggle switch panel for a game. now since i have add more switches and now i get this

test2:42: error: 'currentState' does not name a type
test2:43: error: expected unqualified-id before 'if'
test2:46: error: expected unqualified-id before 'else'
test2:50: error: 'lastState' does not name a type
test2:51: error: expected declaration before '}' token
'currentState' does not name a type

and here is apart of the sketch

// the pin that the pushbutton is attached to
const int buttonPin_1 = 13;
const int buttonPin_2 = 12;
const int buttonPin_3 = 11;
const int buttonPin_4 = 10;
const int buttonPin_5 = 9;
const int buttonPin_6 = 8;
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
void setup() {
** // initialize the button pin as a input:**
** pinMode(buttonPin_1, INPUT);**
** pinMode(buttonPin_2, INPUT);**
** pinMode(buttonPin_3, INPUT);**
** pinMode(buttonPin_4, INPUT);**
** pinMode(buttonPin_5, INPUT);**
** pinMode(buttonPin_6, INPUT);**
** // initialize serial communication:**
** Serial.begin(9600);**
}
void loop(){

** currentState = digitalRead(buttonPin_1);**
** if (currentState == HIGH && lastState == LOW){//if button has just been pressed**
** Serial.println("Headlights");**
** delay(2);//crude form of button debouncing**
** } else if(currentState == LOW && lastState == HIGH){**
** Serial.println("Headlights");**
** delay(1);//crude form of button debouncing**
** }**
** lastState = currentState;**
}
** currentState = digitalRead(buttonPin_2); <---this is where i get 'currentState' dose not name a type **
** if (currentState == HIGH && lastState == LOW){**
** Serial.println("Windshield Wipers");**
** delay(2);**
** } else if(currentState == LOW && lastState == HIGH){**
** Serial.println("Windshield Wipers");**
** delay(1);**
** }**
** lastState = currentState;**
}

and i have add the sketch to if you want to see the full code. if any one can help that would be great and sorry if i have put this in the wrong place

test2.ino (2.56 KB)

Hi can you post your code using the code tags its the first option before bold

Helps everyone look at your code easier. Also please post your entire code not just a little bit, can also make things easier for people to help you.

I think that you issue might be that your currentState is a boolean and so it can only be true or false not currentState = digitalRead(buttonPin_2);

No, the reason is you have a closing } which ends the loop() function block half way through:

  lastState = currentState;
} // this is not needed

  currentState = digitalRead(buttonPin_2);

And you do this again few times. 5 times in total, remove those 5 brackets and it compiles.

thanks for posting ACremers and how would I go about fixing the problem I have

thank you tammytam. now i have a new problem. when i turn the switch on it keeps trigger the pin and the next one to it. all im trying to do is when i flip a toggle switch on and it sends and input to the arduino and it send out one pales and vise versa

Yea, I'd hazard a guess that its because you store the buttons states, but you only use 1 current and previous state set for all the buttons. Let me shorten the code to show what I mean:

currentState = digitalRead(buttonPin_1);
// Removed logic that goes here
lastState = currentState;


currentState = digitalRead(buttonPin_2);
// Removed logic that goes here
lastState = currentState;


currentState = digitalRead(buttonPin_3);
// Removed logic that goes here
lastState = currentState;

You have 6 buttons, Im using your first 3 as an example. As you can see, it doesnt matter what buttonPin1 is, because buttonPin2 will overwrites its lastState, likewise with buttonPin3 and so on.

Big thanks to you tammytam for helping me that help alot. All i have to do is add a lastState and currentState for each switch and it worked thanks again tammytam

Can you help?

No. You can have ONE function named loop(). How many do you try to define?