arduino spaceship interface from arduino projects book

I have made the circuit on a breadboard for the arduino spaceship interface project from arduino projects book .

I have also written the code exactly copied from the book from pages 37 and 39.

When I verify the code this error message comes up at the bottom of the screen:

spaceship_interfearence_code.ino: In function 'void setup()': spaceship_interfearence_code:7: error: redefinition of 'void setup()' spaceship_interfearence_code:1: error: 'void setup()' previously defined here spaceship_interfearence_code.ino: In function 'void loop()': spaceship_interfearence_code:13: error: redefinition of 'void loop()' spaceship_interfearence_code:4: error: 'void loop()' previously defined here

I would like some help with the code errors so that I can make the project work. Thank you Cameron

Post YOUR code.

Please post the code.

This is the correct code for the Spaceship Interface Project:

int switchState = 0;

void setup () {
  pinMode (3, OUTPUT) ;
  pinMode (4, OUTPUT) ;
  pinMode (5, OUTPUT) ;
  pinMode (2, INPUT) ;
  // Pinmode - Right bar on Uno 
  // Green wire is input, yellow wire is output
  }

void loop () {
  switchState = digitalRead(2) ;
  

if (switchState == LOW) {
    //button is not pressed
    digitalWrite(3, HIGH) ; // green LED 
    digitalWrite(4, LOW) ; // red LED
    digitalWrite(5, LOW) ; // red LED
  
  }
  
  else  { //button is pressed
    digitalWrite(3, LOW) ;
    digitalWrite(4, HIGH) ;
    digitalWrite(5, HIGH) ;
  
  delay(250); //quarter second
    digitalWrite(4, HIGH) ;
    digitalWrite(5, LOW) ;
    delay(250) ;

  }

}

Hope this helps.

chum

Is that how it is published in the book?

AWOL: Is that how it is published in the book?

Yes, only with a couple modified notes to help me understand more.

chummer1010:

AWOL: Is that how it is published in the book?

Yes, only with a couple modified notes to help me understand more.

digitalWrite(3, HIGH) ; // green LED

I truly do not understand this style of coding - call pin 3 something sensible, like "greeLEDpin", and then the comment is superfluous, and the code is easier to read and maintain.

(I'm not having a go at you chummer1010 - I'm shocked that a book aimed at beginners proliferates bad practice)

Someone else has already done a video about this

see http://forum.codecall.net/blog/1824/entry-1957-arduino-project-02/

www.youtube.com/watch?v=stB8rllg-R0

AWOL:

chummer1010:

AWOL: Is that how it is published in the book?

Yes, only with a couple modified notes to help me understand more.

digitalWrite(3, HIGH) ; // green LED

I truly do not understand this style of coding - call pin 3 something sensible, like "greeLEDpin", and then the comment is superfluous, and the code is easier to read and maintain.

(I'm not having a go at you chummer1010 - I'm shocked that a book aimed at beginners proliferates bad practice)

Edit: I totally read you wrong.

const byte greenLEDpin = 3;
const byte redLED0pin = 4;
const byte redLED1pin = 5;
const byte switchPin = 2;

void setup () 
{
  pinMode (greenLEDpin, OUTPUT) ;
  pinMode (redLED0pin, OUTPUT) ;
  pinMode (redLED1pin, OUTPUT) ;
  pinMode (switchPin, INPUT) ;
  // Pinmode - Right bar on Uno 
  // Green wire is input, yellow wire is output
}

void loop () 
{
  int switchState = digitalRead(switchPin);  

  if (switchState == LOW) 
  {
    //button is not pressed
    digitalWrite(greenLEDpin, HIGH) ;
    digitalWrite(redLED0pin, LOW) ;
    digitalWrite(redLED1pin, LOW) ;
  }
  else  
  { //button is pressed
    digitalWrite(greenLEDpin, LOW) ;
    digitalWrite(redLED0pin, HIGH) ;
    digitalWrite(redLED1pin, HIGH) ;
  
    delay(250); //quarter second
    digitalWrite(redLED0pin, HIGH) ;
    digitalWrite(redLED1pin, LOW) ;
    delay(250) ;
  }
}
  }

AWOL:

const byte greenLEDpin = 3;

const byte redLED0pin = 4;
const byte redLED1pin = 5;
const byte switchPin = 2;

void setup ()
{
  pinMode (greenLEDpin, OUTPUT) ;
  pinMode (redLED0pin, OUTPUT) ;
  pinMode (redLED1pin, OUTPUT) ;
  pinMode (switchPin, INPUT) ;
  // Pinmode - Right bar on Uno
  // Green wire is input, yellow wire is output
}

void loop ()
{
  int switchState = digitalRead(switchPin);

if (switchState == LOW)
  {
    //button is not pressed
    digitalWrite(greenLEDpin, HIGH) ;
    digitalWrite(redLED0pin, LOW) ;
    digitalWrite(redLED1pin, LOW) ;
  }
  else 
  { //button is pressed
    digitalWrite(greenLEDpin, LOW) ;
    digitalWrite(redLED0pin, HIGH) ;
    digitalWrite(redLED1pin, HIGH) ;
 
    delay(250); //quarter second
    digitalWrite(redLED0pin, HIGH) ;
    digitalWrite(redLED1pin, LOW) ;
    delay(250) ;
  }
}
  }

That is so different, I’ve always used the commands by pinMode…

Oh haha. Now in project 4 they use the phrase 'greenLEDPin', 'blueLEDPin', ect...

Phew!