Hi all! I've been playing around with the use of buttons. I used the example code on buttons and modified it to print on the serial monitor if I have pressed the button or not here is the code i used.
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);
Serial.begin(9600);
Serial.println("READY");
}
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);
Serial.println("ON");
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.println("OFF");
}
}
Whenever I press the button it keeps showing this message and the Serial Monitor stops responding. =(
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
at gnu.io.RXTXPort.nativeavailable(Native Method)
at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
at processing.app.Serial.serialEvent(Serial.java:215)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
At first sight I would say your problem isn't related to your code. You didn't post your circuit, but is there any chance you connected something to pin 0 or pin 1?
Those two pins are physically connected to the Arduino hardware serial port. Maybe you connected the button to pin 1 instead of pin 2. If not, you have a serious problem in your USB or the Arduino board itself.
Oh no! I hope my Arduino isnt broken.. I'm using Arduino Mega by the way and it works just fine before I modified it to display on Serial Monitor. I only copied the circuit connection here http://arduino.cc/en/tutorial/button
Maybe post a photo of your circuit so we can double-check it matches the schematic you linked?
Are you sure your resistor is the right value- did you read it with a meter?- if you misread the markings and it's too low, or if it's faulty and shorted, then you'll be joining 5v to gnd when the button is pressed through too low a resistor and drawing a high current.
Since your code will send continiously and you have no denouncing etc there is a possibility that the monitor will reveive some gibberish it cant handle.
Try this:
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);
Serial.begin(115200);
Serial.println("READY");
}
void loop(){
buttonState = ((buttonState<<1)|digitalRead(buttonPin))&15;//4 reads for debouncing
switch(buttonState){
case 7://Rasing edge
digitalWrite(ledPin, HIGH);//turn LED on:
Serial.println("ON");
break;
case 8://falling edge
digitalWrite(ledPin, LOW);//turn LED off:
Serial.println("OFF");
};//switch(buttonState)
}
You likely have a problem with serial monitor but that code you wrote will definitely overload the serial queue very quickly. Every time through loop() it sends ON or OFF at pretty much the speed it can send ON or OFF, which is incredible.
Here is a possible fix. It only prints when the button state changes.
If serial monitor keeps giving you problems then worst case; uninstall the IDE, get a new download and install that.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
byte lastButtonState = 0;
// 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 );
Serial.begin( 9600 );
Serial.println( "READY" );
}
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 )
{
if ( lastButtonState == LOW )
{
// turn LED on:
digitalWrite( ledPin, HIGH );
Serial.println( "ON" );
}
}
else
{
if ( lastButtonState == HIGH )
{
// turn LED off:
digitalWrite( ledPin, LOW );
Serial.println( "OFF" );
}
}
lastButtonState == buttonState;
}
Thanks for the reply guys I tried both codes when I tried nilton61's code when I press the button it doesnt display anyting on the serial monitor.
Then when I tried GoForSmoke's code the error appears again whenever I press a button.
Here is a picture of how I connected the button on the Arduino:
By the way, I did not use any resistor and the blue square is the button I'm using I just removed it from its case.
And if it matters with the button I'm using I can't do long presses and when I turn on a LED using this button it only turns on after I have released the button
This is a picture of the button it only has 2 connections to it.
The push button has 2 connections only. 1 connection is mainly connected to 5V supply on the Arduino while the other connection to the button is connected to the gnd and pin 2.
How do I troubleshoot the serial monitor problem? Because it works fine until I press a button it stops and the error appears on the Arduino IDE :~
put a resistor between 5V and the pin. A 1k to 10k resistor would be good but 200 ohms to 10k ohms will do.
put another wire on pin 2 or where the pin 2 wire connects to the button (on the same breadboard row should connect). That wire should go through a twice or more bigger resistor than the first and then to ground. This is to drain the charge on the wire to pin 2 once the button is let go.
I don't know what is happening with your serial monitor. It could be many things. Does it usually print okay?
Jimboza
Hang on.... if that's true, then when the button is "on", you're shorting 5v to ground...
Not quite. Arduino INPUT is megohms high impedance. But read it fast enough, it's still has some draw.
@GFS, he says one side of the switch is to 5v, the other is to ground AND pin 2. That's a dead short when the switch is on.... it's like the tutorial, but with no resistor. Admittedly, his photo doesn't show that, but that's what his words say.
1 connection is mainly connected to 5V supply on the Arduino while the other connection to the button is connected to the gnd and pin 2.
Yes jimboza that's what I mean sorry if it isn't clearly shown in the picture..
GoForSMoke I'm trying what you suggest I'll let you know what happens. Just to be sure I'll add a resistor on the wire that connects to 5V then I'll add a higher value resistor (maybe just 2 of both? ) on the other side of the switch that connects to Pin 2 and GND?
GoForSmoke:
I don't see any connect to ground in the picture.
Me neither.... but it's impossible to help when the pic and the words are out of synch. Also his early post said he copied the tutorial, a later one said no resistor; that's in line with the later statement.
Yes jimboza that's what I mean sorry if it isn't clearly shown in the picture..
Well there's the problem: you presumably didn't understand the significance of the resistor. Without it, every time the switch is on, you have a dead short 5v to ground.
Put the resistor in, like the tutorial said in the first place....