Hope this post finds you all in good health.
I'm looking for some help on how to process data gathered from the serial port.
I created this code to receive ASCI coded data ("Hello World") from a serial port and store it into an array. Since Each ASCI character is two bytes long, I have a 22 position array.
if (Serial.available()) {
for (i = 0; i < 22; i ++) {
SCALE_DATA[i] = Serial.read();
}
Serial.flush();
At a later stage I am able to use the stored data and print it on another serial port using a similar loop, just this time:
for (i = 0; i < 22; i ++) {
Serial.print (SCALE_DATA[i]);
}
What I cant do is use this data, for example to print on an LCD. Say for example I would like to print only the 'world' word. I would need asci characters w+o+r+l+d, but each asci character is actually 2 array positions. How could I select print this?
if (Serial.available()) {
for (i = 0; i < 22; i ++) {
SCALE_DATA[i] = Serial.read();
}
Serial.flush();
1)When you are checking if(Serial.available()) all you are doing is asking if there is AT LEAST 1 byte in the buffer. So, when you continue to read 22 bytes of data when there is most likely only 1 byte in there, what is going to happen? Maybe try if (Serial.available() >= 22) so you dont end up with garbage.
2)Serial.flush() only waits for you to finish writing data, it does not do what you expect and clear the buffer. To do this, you will want to do
Ps991:
1)When you are checking if(Serial.available()) all you are doing is asking if there is AT LEAST 1 byte in the buffer. So, when you continue to read 22 bytes of data when there is most likely only 1 byte in there, what is going to happen? Maybe try if (Serial.available() >= 22) so you dont end up with garbage.
Great idea, but that would assume the buffer is at least 22 bytes long, which may or may not
be the case.
If you want a blocking version of read, its easy to code one up:
MarkT:
Great idea, but that would assume the buffer is at least 22 bytes long, which may or may not
be the case.
I didnt assume it was 22 bytes, in the OP code, he is looking for 22 bytes, that is where I got the number.
Although, 1 glitch where a byte is not received will cause problems, so you need more sophisticated code to check for errors or at least the entire transfer was completed.
I'm looking for some help on how to process data gathered from the serial port.
Code that captures servo position commands from the serial port and processes the data to position the sesired servo.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}