Project 2 on the starter kit

Hey guys new coder here. This seems to be a common issue here so here I go with mine. I keep getting the "switchState was not declared in this scope" message when I try and verify my code. are yall seeing anything wrong with it? here's the error code

C:\Users\truth\Documents\Arduino\consol\consol.ino: In function 'void loop()':
consol:9:3: error: 'switchState' was not declared in this scope
switchState = digitalRead(2);
^~~~~~~~~~~
consol:25:3: error: expected ';' before 'digitalWrite'
digitalWrite(4, HIGH);
^~~~~~~~~~~~
consol:28:1: error: expected ';' before '}' token
}
^
exit status 1
'switchState' was not declared in this scope
1

[code]
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}

void loop() {
switchState = digitalRead(2);
if (switchState == LOW) {
// the button is not pressed
digitalWrite(3, HIGH);
// green LED
digitalWrite(4, LOW);
// red LED
digitalWrite(5, LOW);
// red LED
}
else {
// the button is pressed
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
delay(250)
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250)
}
}
[/code]111

You forgot a semicolon on the line before it. But you seem to have cut and pasted the error. So you have to fix it in two places.

All the sketches from the Starter Kit Project Book are available in the Arduino IDE under the File > Examples > 10.StarterKit_BasicKit menu in the Arduino IDE.

Now, some may argue that the process of transcribing the text from the book to the Arduino IDE is an important learning opportunity that will be missed if you just open the pre-written sketch in the Arduino IDE. My opinion is that the time required for tedious mechanical copying would be better spent studying the code, researching parts you don't understand, and then running experiments to verify your understanding. But everyone has their own learning styles, so if you think the transcription process is beneficial or enjoyable, then certainly go for it.

turns out id did that on top of not putting the " int switchState = 0;". figured it out by comparing it to the example code in the IDE thanks for the help though!

But, do you know why that was important ?

I do, I didn't understand at first that I needed to define variables. once I figured that out and went back and corrected it the rest of program just had a few typos to correct then it ran great. Thanks for verifying that I understood it I really do appreciate yall looking out like that.

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