I am an absolute beginner at all things arduino and I am attempting to practice reading the state of a button each time I press it....but I keep getting an IOException.

My code is as follows:

int ledPin;
int button;
int buttonVal;

void setup(){
  button = 7;
  ledPin = 6;

void loop(){
  buttonVal =  digitalRead(button);
    Serial.println("...CHECKING : " + buttonVal);

The code only partly works. It prints ..CHECKING: until I actually press the button and the crash occurs with the following message: Error inside Serial.serialEvent() java.io.IOException: Input/output error in nativeavailable

Please help, I'm very stuck

  • \$\begingroup\$ Make sure you've connected the button to ground through a pull up resistor, otherwise the values will be random \$\endgroup\$ – user37463 Feb 19 '14 at 18:55

The problem is that you can't simply "add" to the string as you're doing. I don't quite have the time to write a full answer right now, but try this instead of your single Serial.println line:

Serial.print("...CHECKING: ");
Serial.println(buttonVal, DEC);

This should print a whole ton of "...CHECKING: 0" and "...CHECKING: 1" lines depending on the state of the button.

If you want it to only print "checking" and then wait until the button is pressed, you can try this:

void loop() {
    Serial.print("Waiting for button press... ");
    do {
       buttonVal = digitalRead(button);
    } while (buttonVal == 0);
  Serial.println("button pressed!");
  • \$\begingroup\$ Thanks for your suggestions. I tried both scenarios but the same exception occurs in both cases ... \$\endgroup\$ – sisko Apr 9 '12 at 14:41
  • 1
    \$\begingroup\$ The second example as well? Using your code with the loop function I provided works for me (at least when I put a 4.7k ohm pulldown between the button pin and ground; otherwise digitalRead gives random values). \$\endgroup\$ – exscape Apr 9 '12 at 15:19

buttonVal won't be a printable character - it's probably 0x00 when not pressed and maybe something like 0x01 when it is. This can't be displayed as text so Java will throw a fit trying. Try this:

Serial.println("...CHECKING : " + (buttonVal)?"True":"False")

The bit on the end there with the ()?():() format is called the ternary operator. It works like this: if buttonVal is evaluated as a boolean - if it's zero, that equates to FALSE, if at least one bit is '1' it will be TRUE. If false, it returns the "False" text (or whatever is after the :) and it will be displayed. If it's anything else, True will be displayed (or whatever is before the :).

Edit: Hmm, well if that's not working let's start whittling away at things I know are wrong with your code. The first one I can see is that I wouldn't just loop over the serial print over and over again: put a delay after the serial print. There's no need to print it every loop and it will really tear up the processor while basically doing nothing. Add a 10ms wait or so after the serial print statement. It may not be the cause of the problem but it's certainly not right. Sometimes multiple issues can gang up and cause multiple problems that look like one big problem instead of several little ones. Best to start removing sources of error and see where that gets you.

  • 2
    \$\begingroup\$ Arduino is written in C++, not Java! Thus the strings are char arrays, and adding to them won't work in your case, either. The simple solution for Arduino is to use two print/println statements. Edit: Oh, I just realized the cause of the confusion. The IDE is in Java, but the code is written in C++ and compiled with avr-gcc. \$\endgroup\$ – exscape Apr 9 '12 at 13:35
  • \$\begingroup\$ Using a ternary operator was a good idea. I used digitalRead to get the button value and your suggested code to print it. However, once I push the button the same exception occurred and the code stops working \$\endgroup\$ – sisko Apr 9 '12 at 14:33
  • \$\begingroup\$ Why does the + work for him? I agree it doesn't look like standard C++ but you can do amazing things with operator overloading. \$\endgroup\$ – AngryEE Apr 9 '12 at 15:24
  • \$\begingroup\$ @AngryEE I think the + works for him because he's adding a string ("...Checking") to another string ("True" or "False, depending on the result of the Ternary operator.) \$\endgroup\$ – ducksauz Apr 10 '12 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.