# arduino IOException

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;
Serial.begin(9600);
}

void loop(){
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

-

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 {
} while (buttonVal == 0);
Serial.println("button pressed!");

-
Thanks for your suggestions. I tried both scenarios but the same exception occurs in both cases ... –  sisko Apr 9 '12 at 14:41
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). –  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.

-
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. –  exscape Apr 9 '12 at 13:35
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 –  sisko Apr 9 '12 at 14:33
Why does the + work for him? I agree it doesn't look like standard C++ but you can do amazing things with operator overloading. –  AngryEE Apr 9 '12 at 15:24
@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.) –  ducksauz Apr 10 '12 at 4:11