and ive wired it like this. in my broadboard i have a pushbutton. one lead is connected to "gnd" on the arduino. the other lead is connected to "13" on the arduino. i figured when i run this, and press the button, OINK should print in the serial log. but nothing happens.
ah yes! ive set that pin to be an input! silly. so do i need a resistor inline here when i do this? i guess the resistance is 0. how do i calculate the amps if resistance = 0?
OK try it on a pin other than 13, this is because pin 13 has a resistor and LED connected to it and might be too much for the internal pull up resistor.
indeed that helped. if you can help me a little further id appreciate it.
now it prints a few oinks on init. thats tolerable i guess. but when i press the button, it prints many many oinks. then it stopped. so i pressed it again. then it kept printing oinks. any ideas? how can i get one oink per print? (ideally)
Your code simply continually checks whether the button is pressed.
While it is pressed, it will keep printing oinks as fast as it can, because every time it loops, it is still pressed.
There's a tutorial for state change detection:
That may help.
[edit]Edit: Contact bounce may also be an issue. The contacts in the switch actually bounce and chatter for a short time, seeming like multiple presses. There is also a tutorial on debouncing, and a library, too. http://arduino.cc/en/Tutorial/Debounce[/edit]
It does that because that is what you asked the code to do. To only get it to print once you have to remember what the button state was and then only print if it is now different from the last time you read it.
Use a variable called lastTime, the last thing in your loop() do:-
lastTime = BUTTON1;
then in the if statement :-
if ( ( buttonValue1 == HIGH) && (latsTime==LOW ) ) {
Note when ever you post code use the # button in the reply box.
still getting strange behaviour. this is my test program. at the beginning, one OINK gets printed. if i push that button again. nothing happens. if i press the other button, OI gets printed once. then soon after it keeps printing. plz help!
//declare constants
#define BUTTON1 8
#define BUTTON2 12
#define POWER 7
#define LED 10
//declare variables
int LEDState = 0;
int LEDBrightness = 63;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonValue1 = 0;
int buttonValue2 = 0;
void setup(){
//set pins
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(POWER, HIGH);
Serial.begin(9600);
}
void loop(){
//read button pins
buttonValue1 = digitalRead(BUTTON1);
buttonValue2 = digitalRead(BUTTON2);
//handle button1 behaviour - turn LED off and on
if((buttonValue1 == HIGH) && (buttonState1 != buttonValue1)){
Serial.println("OINK");
if(LEDState == 0){
analogWrite(LED, LEDBrightness);
LEDState = 1;
}
if(LEDState == 1){
analogWrite(LED, 0);
LEDState = 0;
}
delay(20);
}
//handle button2 behaviour - increase brightness of LED
if((buttonValue2 == HIGH) && (buttonState2 != buttonValue2)){
Serial.println("OI!");
LEDBrightness += 63;
if (LEDBrightness > 255)
LEDBrightness = 63;
analogWrite(LED, LEDBrightness);
delay(20);
}
buttonState1 = buttonValue1;
buttonState2 = buttonValue2;
}
Your forgetting to do what you did earlier. That is initialise BOTH push buttons as inputs and do a digital write to BOTH of them to enable the internal pull up resistors.
i think my pushbutton switches are bad or something.
i reduced the problem back to the pushbutton test with the following code. even when i hold the two wires (while not touching the bare metal parts), it continually prints oink. if i wire it up to a switch, on one of mine, it prints oink upon press and once upon release. the other switch it just continues printing oinks. will run to the store and grab more switches. but why would it print oink if im holding the two wires apart!