Arduino <-> Processing via Serial, text only reading as ASCII codes

Hi all,

I have a feeling this is a rather frequent problem, but no amount of searching on these forums or the googles gives me an answer. I'm not a total Arduino or Processing noob, but my limits are showing here.

I'm trying to have bidirection communication between an Arduino and a Processing sketch eventually, sending a compass reading to Processing (eventually over XBees) and Processing sending a PWM value back as well as a couple digital HIGH/LOW settings for motor direction control. I'm trying to get a motor to align things with North.

My code, based on the example "PhysicalPixel" sketch, code I've altered is in red:

const int ledPin = 13; // the pin that the LED is attached to
[color=red]String[/color] incomingByte;   // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the the serial buffer:
    [color=red]incomingByte = Serial.read();[/color]
    Serial.println(incomingByte);
    // if it's "High", turn on the LED:
    if (incomingByte == "H[color=red]igh[/color]") {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's "Low" turn off the LED:
    if (incomingByte == "L[color=red]ow[/color]") {
      digitalWrite(ledPin, LOW);
    }
  }
}

To keep things simple for now I'm just using the Arduino IDE serial monitor to watch the serial coming in, and to manually type "High" or "Low". As you may have guessed by now I'm receiving

72
105
103
104
10

in the serial monitor, which is obviously the ASCII codes for "High" - each letter, plus newline.

Any hints or places anyone can send me to get this solved. I have a feeling it's something simple but I can't track it down.

Thanks in advance :slight_smile:

Serial communication is done one character at a time. You need to read the data the same way.

Changing the type from char to String will not read the whole string for you.

Thanks, PaulS. That makes sense now that you point it out. Assuming I figure out how to read incoming characters one at a time will they automatically then be actual characters, not ASCII codes?

And any chance you or anyone else can point me to a place or search term to learn how to read incoming characters one at a time? I'd very much appreciate it.

Have a great day.

Do a forum search for "started && ended" to find some code that reads all data between start-of-packet and end-of-packet markers, and stores the characters in a suitably sized array.

From there, you can parse the data however is required.

Thanks again PaulS, you've led me in the right direction. I have some sample code up and running now, and I think I can probably figure out the rest on my own. Your "started && ended" code is helpful in more than one way :slight_smile: