Go Down

Topic: Project 02 - button does nothing (Read 2028 times) previous topic - next topic

yattix

Sep 01, 2017, 10:30 pm Last Edit: Sep 01, 2017, 10:36 pm by yattix
Hi, brand new to electronics and Arduino but come from a programming background.

I'm only on project 2 and the wheels have already come off!

When I press the button, the green LED remains lit and the other two don't flash. I've swapped the conditional around in the program and the red lights do flash (so if the switchstate == HIGH they flash). I've uploaded the sample code from the web app to rule out any code issues as well, no dice.

I've also followed the advice on here about straightening the legs of the button and moving it to another part of the breadboard but still no luck.

I've tried all the buttons in the pack and none of them work. Am I missing something?

The weird thing is, if I replace the switch with a piece of jumper wire (so the circuit is complete) input pin 2 is still getting no voltage. I just don't understand - if that piece of the circuit is in series, then surely the pull-down resistor will always give it 0V? Regardless of whether the button is open or closed?

pert

I believe the code in question is the File > Examples > p02_SpaceshipInterface:
Code: [Select]
/*
  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);
  }
}


but I'm a bit confused. You say:
I've swapped the conditional around in the program and the red lights do flash (so if the switchstate == HIGH they flash).
but the code above already is written so that when switchstate == HIGH the red LEDs flash.

Please post your modified sketch using code tags (</> button on the toolbar).

yattix

If I modify the code thus:

Code: [Select]
void loop() {
    switchstate = digitalRead(2);
   
    if (switchstate == HIGH){
      // The button is not pressed
     
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
    }
   
    else { // the button is pressed
   
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
   
    delay(250);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    delay(250);
   
    }
}


the red LEDs flash. Otherwise, they don't. It's a bit moot because in both cases pressing the button has no effect.

yattix

I should point out though, that I'm using the example code and still not getting the expected result.

So, because the green LED is consistently lit (and won't change) this must mean that pin 2 is getting zero volts (is LOW). This means then that the circuit with the button in it must be open, right? And closing it by pressing the button isn't working.

But what puzzles me, as I said before, is that if I replace the button with a jumper, so it's constantly closed, the red lights don't flash then either.

yattix

OK - update. It's now working. I took the button out and rotated it 180 degrees and it now works.

Pretty frustrating as I don't know if it was a dodgy connection or if the buttons only work one way round?

pert

No, rotating it 180 degrees would make no difference. Rotating it 90 degrees on the other hand will make a difference. Two pairs of the legs are always connected to each other. The connection between the pairs is only made when the button is pressed. So if you put it in the breadboard at the wrong orientation it will always conduct whether the button is pressed or not.

yattix

I get what you're saying, it must have just been a dodgy connection. I can't actually fit it to the board at 90 degrees as the legs don't reach across the divide in the breadboard.

Go Up