Hi everyone. This is my first post. I've started reading a book on Arduino C programming. I'm not a stranger to electronics at all, and have familiarity with serveral scripting languages like PHP, Powershell, Java... I do some work in SmartThings home automation as well which is Groovy based. Anyway, suffice to say I'm no expert programmer but I do have some experience.
What I'm trying to do is read a constant stream of serial data coming from my car's oxygen sensor control box which operates on serial rs232 at 19200 baud, n81. The data coming out of it is what the lambda value is (how much air to fuel in the exhaust) and the readiness state and error state. The stream looks like this.
1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors1.770 Ready No-errors
For now I am just wanting the 1.770 part. It could be anything from 1.770 to 0.700. The text stuff I honestly CARE about, but not right now. In the future (and you can see it somewhat in my code) I would just do something like set the status of an LED just to represent that the data is ok and there aren't any errors.
What I'm seeing right now is if I just paste 1.770 Ready No-errors into the serial monitor, I see it perfect on the quad alphanumeric display. It was a pain to get it to show the decimals right... If I paste a long stream like above, or more importantly plug it in, I see randomly placed zeros, 7's, decimals, ones, etc.... If I paste the above into the serial monitor I see basically the same behavior. I can paste just number in all day long and it seems to be right, but something with the text I guess is causing me grief. Anyway, here's the code. Any help is really appreciated.
// reads serial data and puts it on the adafruit quad alpha display over i2c
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
// setup the variables
char incoming;
char displaybuffer[4];
int i=0;
boolean d[4]={false,false,false,false};
boolean readystatus= false;
void setup() {
pinMode(13, OUTPUT); // turn off
digitalWrite(13,LOW); // that stupid onboard LED
Serial.begin(19200); // set our USB baud rate
Serial1.begin(19200); // set the TTL to serial baud rate for COM1
alpha4.begin(0x70); // pass in the address of the i2c alpha display
alpha4.setBrightness(1); // set its brightness low
alpha4.writeDigitRaw(0, 0x7FFF); // test character 1 full on
alpha4.writeDigitRaw(1, 0x7FFF); // test character 2 full on
alpha4.writeDigitRaw(2, 0x7FFF); // test character 3 full on
alpha4.writeDigitRaw(3, 0x7FFF); // test character 4 full on
alpha4.writeDisplay(); // light em up now
delay(100); // wait a bit
alpha4.clear(); // clear out the display
alpha4.writeDisplay();
}
void loop() {
i = 0; // reset the index to capture new data
readystatus=false;
while (i<=3) {
while (! Serial.available()) return;
incoming = Serial.read();
// first lets deal with o2 values. grab the numbers first
if ((incoming >= 48) and (incoming <= 57)) {
displaybuffer[i]=incoming;
d[i]=false;
Serial.write("INDEX: "); Serial.println(i);
Serial.write("BUFFER: "); Serial.println(displaybuffer[i]);
i=i+1;
}
// do we need to set the decimal place?
if (incoming == 46) {
i=i-1;
d[i]=true;
//Serial.write("INDEX: "); Serial.println(i);
//Serial.write("BUFFER: "); Serial.println(displaybuffer[i]);
Serial.write("DECIMAL BOOL: "); Serial.println(d[i]);
i=i+1;
}
// do something with spaces
if (incoming == 32) {
}
// do something with the s character
if (incoming == 115) {
}
/* // do something with text
if ((incoming >= 65) and (incoming <= 126)) {
Serial.write(incoming);
if (incoming == 'R') {
readystatus=true;
Serial.write("READY STATE: "); Serial.println(readystatus);
} else {
readystatus=false;
Serial.write("READY STATE: "); Serial.println(readystatus);
}
}*/
dispalpha();
}
}
void dispalpha() {
//set every digit to the buffer
alpha4.writeDigitAscii(0, displaybuffer[0],d[0]);
alpha4.writeDigitAscii(1, displaybuffer[1],d[1]);
alpha4.writeDigitAscii(2, displaybuffer[2],d[2]);
alpha4.writeDigitAscii(3, displaybuffer[3],d[3]);
alpha4.writeDisplay();
if (readystatus = true) {
digitalWrite(13,HIGH); // Set arduino status LED on
} else {
digitalWrite(13,LOW); // Set arduino status LED off
}
}
void debugging() {
Serial.println("INDEX: "); Serial.write(i);
Serial.println("BUFFER: "); Serial.write(displaybuffer[i]);
Serial.println("DECIMAL BOOL: "); Serial.write(d[i]);
}