Hello All, I have just put together my first project. I thought I followed this very well - http://www.sparkfun.com/tutorials/181
BUT, when I hooked JC1 to D2 and JC3 to D13, and load up the Button Example (and made sure I am not rubbing against the USB cover), I get the reverse. i.e. the LED is on constantly, and turns off when I press the button.
If I change the Button code to IF LOW from, IF HIGH, I can make it work, but from what I read IF HIGH means when button is pressed.
Any ideas on what I have done wrong? Everything looks ok to my untrained eye.
thanks
By Button Example do you mean this ?
http://arduino.cc/en/Tutorial/ButtonStateChange
If so how do you have the button switch wired? The switch in the example is supposed to be a normally open type and so provides a logic high to the input pin (D2) when pressed. Presumably the LED and resistor at JC2 provide a pull down to ground. When you press the button does the LED at JC2 go on ? Or is it on and then goes off, which would indicate the button is a normally closed type, which might explain your problem.
Also the code above cycles the LED on/off only after 4 presses. Are you saying the output LED at JC3 goes on/off with every press ?
Thanks for the reply Mee.
Mee_n_Mac:
By Button Example do you mean this ?
http://arduino.cc/en/Tutorial/ButtonStateChange
No, I mean the example in the Arduino ERW software:
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Mee_n_Mac:
If so how do you have the button switch wired? The switch in the example is supposed to be a normally open type and so provides a logic high to the input pin (D2) when pressed. Presumably the LED and resistor at JC2 provide a pull down to ground. When you press the button does the LED at JC2 go on ? Or is it on and then goes off, which would indicate the button is a normally closed type, which might explain your problem.
Sorry, I am so new to this I am not sure, I just followed the tutorial on how to put it together. What I have is:
JC3 ↔ D13 and JC1 ↔ D2
The light is on all the time, and when I press the button at S2, the JC3 LED turns off. I was under the impression it should be off all the time, and on when button pressed.
The reset button seems to be working ok.
OK, my bad. I kept reading JC2 when you said JC1. Looking at the code and at the schematic it’s obvious what’s happening. You’ve put it together properly and the code is also working as it should. The reversed behavior you see is because the Protoboard’s wiring to the SW2 is the opposite of what the code was expecting. That is, the schematic shows JC1 is wired via a pull-up resistor to 5V. SW2 is a NO type and so when NOT pushed, 5V, a logic HIGH is applied to pin D2. When SW2 is pushed, it applies a ground, a logic LOW, to D2. The rest you’ve already figured out.
http://www.sparkfun.com/datasheets/DevT … ld-v25.pdf
The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
Thanks for your help Mee.
Looks like I need to start to learn the hardware side of things more, rather than just the software.
have a nice day