Pages: [1]   Go Down
Author Topic: Arduino <-> Processing via Serial, text only reading as ASCII codes  (Read 4393 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
String 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:
    incomingByte = Serial.read();
    Serial.println(incomingByte);
    // if it's "High", turn on the LED:
    if (incomingByte == "High") {
      digitalWrite(ledPin, HIGH);
    }
    // if it's "Low" turn off the LED:
    if (incomingByte == "Low") {
      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 smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Pages: [1]   Go Up
Jump to: