Help w/ Project 2 - Spaceship Interface Code

I'm starting the second project and get the response "void setup()" as an error. I've written all the code as it is (or as I think it is) in the book, but something is wrong. "void setup()" is the error I get when I include the text from the top shaded box on page 37 of book:

void setup () {
}

void loop () {
}

When I delete the text from the top shaded box on page 37, I get this error instead: "switchState" was not declared in this scope.
I considered starting with code in the second shaded box because it has a "1" next to the first line... my thinking is this should be the first line of code. Still, no-worky.

With this error, "switchState" was not declared in this scope, the following line is highlighted in yellow: if (switchState == LOW) {

How do I get this code to work? The on-line videos of the code for this project at RS look different than what's in the book, but I've got the book and want to use it for instruction. Thanks.

Problem is solved.

If anyone has the same problem, I found code for this project by clicking A) Arduino icon, B) File, C) Examples, D) 10.StarterKit, and then E) p02_SpaceshipInterface

Uploading this sketch made made project #2 work. It looks like the codes for all projects are listed here, and don't have to be typed by the user.

Good luck with your projects!

I had the same problem. When i changed the name of the variable from "switchState" to "switchstate" everything worked fine.

it gave me a different issue i wrote the code from the book like this:
int switchState = 0;
void setup() {
pinMode (3,OUTPUT);
pinMode (4,OUTPUT);
pinMode (5,OUTPUT);
pinMode (2,INPUT);

}

void loop(){
switchState = digitalRead(2);
// this is a comment
if (switchState == LOW);{
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(250);//wait for a quarter second
//toggle the LEDs
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
delay(250);
}
}

and it gave error compiling stating "else without previous if" ???????

Hi rcarver445,

If you look closely, I believe your problem is with a misplaced semicolon on the line with your if statement:
if (switchState == LOW);{
should be
if (switchState == LOW) {

I hope this helps!

can someone tell me whats wrong here please because when I click to verify its giving me the void setup() { error.....thanks

int switchstate = 0;
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT):
pinMode(5,OUTPUT);
pinMode(2,INPUT);
}
void loop() {
switchstate = digitalRead(2);
// hi im awesome
if (switchstate == LOW) {
// botton is not pushed
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, LOW);
digitalWrite(5, HIGH);
delay(250); // wait quarter second
// toggle leds
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250);
}
} // go back to loop

@Tacoguy347

As I answered in your other posting:

Don't add your question to someone else's thread - that's called Thread Hijacking.

Post your code in [code][/code] tags so it formats correctly.

What error do you get?

All of this is in the post 'How to use this forum - please read.' at the top of the section.

If you had done this then I'd probably be addressing your issues rather than having to ask for this.

You have a colon instead of a semicolon.

  pinMode(4,OUTPUT):

should be

  pinMode(4,OUTPUT);

I am having the same problem. 'Switchstate' was not declared in this scope. I have read these tips posted and still have not figured it out. I know you can load the preprogrammed version. But I am learning the commands and wanted to do it all myself. I even tried to copy and paste the correct code over and still error. Has anyone figured this out yet?

I can't see your code from here so would you care to post it? And please read the stickie on forum etiquette by Nick Gammon at the top of the board before doing so.

hello soo i tripple checked it and it doesn't work
can somebody please tell me what is wrong with it int switchState = 0;
void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
switchState = digitalRead(2);
// this is comment
if (switchsState == LOW) {
//button is not pressed
digitalWrite(3, HIGH); // green LED
digitalWrite(4, LOW); // RED LED
digitalWrite(5, LOW); // red led
}
else { // button pressed
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250); //wait for a quarter second
//toggle the leds
digitalWrite(4. HIGH);
digitalWrite(5, LOW);
delay(250); //wait for a quarter second
}
}

there is double ss because i tried lower case

int switchState = 0;

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

void loop() {

switchState = digitalRead(2);

if (switchState == LOW) {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}

else {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(250);
digitalWrite(4. HIGH);
digitalWrite(5, LOW);
delay(250);
}
}

i stripped sample from all comments as well and sample works but mine is not, mind bobbling

tried wipe a screen already as well ;))

If you fix the period in the next to last digitalWrite, change it to a comma, it compiles ok.

Sir
you absolutely genius
guess there is still hope
thank you thank you