Spaceship Interface

So, I wrote the code and hammered it to remove errors. The circuit did not behave as expected. After futzing around, I just uploaded the prewritten code, same apparent result. Here it is anyways:

/*
  Arduino Starter Kit example
  Project 2 - Spaceship Interface

  This sketch is written to accompany Project 2 in the Arduino Starter Kit

  Parts required:
  - one green LED
  - two red LEDs
  - pushbutton
  - 10 kilohm resistor
  - three 220 ohm resistors

  created 13 Sep 2012
  by Scott Fitzgerald

  http://www.arduino.cc/starterKit

  This example code is part of the public domain.
*/

// Create a global variable to hold the state of the switch. This variable is
// persistent throughout the program. Whenever you refer to switchState, you’re
// talking about the number it holds
int switchstate = 0;

void setup() {
  // declare the LED pins as outputs
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // declare the switch pin as an input
  pinMode(2, INPUT);
}

void loop() {

  // read the value of the switch
  // digitalRead() checks to see if there is voltage on the pin or not
  switchstate = digitalRead(2);

  // if the button is not pressed turn on the green LED and off the red LEDs
  if (switchstate == LOW) {
    digitalWrite(3, HIGH); // turn the green LED on pin 3 on
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
  }
  // this else is part of the above if() statement.
  // if the switch is not LOW (the button is pressed) turn off the green LED and
  // blink alternatively the red LEDs
  else {
    digitalWrite(3, LOW);  // turn the green LED on pin 3 off
    digitalWrite(4, LOW);  // turn the red LED on pin 4 off
    digitalWrite(5, HIGH); // turn the red LED on pin 5 on
    // wait for a quarter second before changing the light
    delay(250);
    digitalWrite(4, HIGH); // turn the red LED on pin 4 on
    digitalWrite(5, LOW);  // turn the red LED on pin 5 off
    // wait for a quarter second before changing the light
    delay(250);
  }
}

So …
State 1, nothing plugged in = no lights
State 2, USB plugged in = no lights
State 3, USB/GND plugged in = green light on with switch on/off
State 4, USB/GND/5V plugged in = green light on // [button pressed] all LED off // [button unpressed] delay then green light on
State 5, same as 4 but using jumper to bypass switch = similar to 4 but most times (not all) no delay

When I rewrite the code from: if (switchstate == LOW) to HIGH, the red lights will blink in stead of a steady green, same otherwise to 4 and 5

Also, I get a warning on my mac soon after telling my Arduino is using too much power
EDIT: More importantly, the message (I later notice) pops up exactly when I press the button, here it is:
USB ACESSORIES DISABLED
Unplug the accessory using too much power to reenable USB devices.
A bit more information, ya.
So the wiring is correct, and the code. (I think)
Why does the Mac have problems with power draw when I press the button, that explains why the LED are dark.
Typing this I notice the LED on the UNO go out too. releasing the button brings it online again.
This now is certainly not a projects question but an Arduino question. How do I move it?
Guess I can try reporting myself …



Using code tags in first post, Karma++.

Post a wiring diagram of the button.

What does "State" say? A button has 2 states...

USB plugged... Explain. It makes no sense.

A still trying to get an image up, I think the upload timed out B State = system state C ... " I then plugged in the soinso cable or wire... " if that makes any sense

So the wiring is correct

I believe not. The switch is wired incorrectly. Pull it off and try again. Meanwhile, you should see the LEDs come back on, if the Arduino hasn't been damaged from +5V being shorted to ground.

Your button is wired incorrectly. Please carefully compare it against the diagram in the Arduino Project Book:

Clipboard01.png

hehe ty! I'm on the case! OK, fixed the placement of the 10k O resistor and same effect. Shall I assume I broke my Uno on my second project?

sean_clough: A still trying to get an image up, I think the upload timed out B State = system state C ... " I then plugged in the soinso cable or wire... " if that makes any sense

"State" is Your private nomenclature, not understandable. Please don't use any private dictionary.

Bring up the Arduino reference: https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/ I use quite often. Look at "INPUT_PULLUP. That saves components and wiring.

What is "soinso"?

sean_clough: Shall I assume I broke my Uno on my second project?

Not necessarily. Go back to some earlier project and try that again!

I would suggest this: Wire the switch to connect the pin to Gnd when pressed. Remove any connection to 5V. In setup(), use this

pinMode (pinName, INPUT_PULLUP);  // turns on internal pullup resistor

In loop(), look for a low:

pinNameState = digitalRead (pinName);
if (pinNameState == LOW){ // button is pressed
// perform the button pressed action
}

or

if (digitalRead (pinName) == LOW){
// perform the button pressed action
}

Pulldown to Gnd and then yanking it high is prone to errors. Especially when buttons are miswired and 5V is shorted to Gnd. We've seen it lot. Miswired button to Gnd, no big deal - the pin may read high all the time, or it may read low all the time.

CrossRoads: Miswired button to Gnd, no big deal - the pin may read high all the time, or it may read low all the time.

This absolutely is the preferred way to wire switches, but would still be a problem if you accidentally coded it to an output. :astonished: