We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone,
I have an Arduino Mega which is sending some predefined data to an Arduino Uno. Namely "Air temperature", "Oil temperature" and "Battery voltage". Consider also that I have checked the incoming serial data into the Uno and confirmed that the correct data is received. Now my problem is that I can not get these data displayed properly using processing. I am trying to display each of the three data items in a separate line. "Air temperature" should read a value of "2266". "Oil temperature" should read a value of "2159" and "Battery voltage" should read a value "531". So I am looking for an output on the screen that looks something like:
Air temp is: 2266 Oil temp is: 2159 Battey voltage is: 531
I would appreciate it if you have a look at my code and let me know where I am going wrong.
Here's my Code on Arduino IDE which is being compiled on Uno:
unsigned char buff[14];
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600); //talk to laptop
}
void loop() // run over and over
{
if (Serial.available() == 14) // I'm sending 14 bytes and want to check that some bytes are read, before filling the buff
{
if (Serial.read() == 0x82)
{
if (Serial.read() == 0x81)
{
if (Serial.read() == 0x80)
{
buff[0] = 0x82;
buff[1] = 0x81;
buff[2] = 0x80;
for (int i = 3; i<14; i++)
{
buff[i] = Serial.read();
}
/*for(int k = 0; k<14;k++) // supposed to empty the buffer or reset it
{
Serial.read();
}*/
}
}
}
}
if (buff[0] == 0x82 && buff[1] == 0x81 && buff[2] == 0x80)
{
int c = ((buff[4] << 8) + buff[5])/10;
Serial.print("Air temp is: ");
Serial.println(c);
Serial.print('F');
int d = ((buff[6] << 8) + buff[7])/10;
Serial.print("Oil temp is: ");
Serial.println(d);
Serial.print('J');
int e = ((buff[8] << 8) + buff[9])/10;
Serial.print("Battey voltage is: ");
Serial.println(e);
Serial.print('N');
//Serial.println(freeRam());
}
}
Here's the code that I run on processing for Uno:
import processing.serial.*;
Serial myPort;
String Air_temp="";
String Oil_temp="";
String Battey_voltage="";
PFont font;
int lf = 10;
void setup() {
size(500, 500);
myPort = new Serial(this, "COM24", 9600);
myPort.bufferUntil('N');
font = createFont(PFont.list()[2], 32);
textFont(font);
}
void draw() {
//The serialEvent controls the display
}
void serialEvent (Serial myPort) {
Air_temp = myPort.readStringUntil('F');
//Oil_temp = myPort.readStringUntil('J');
//Battey_voltage = myPort.readStringUntil('N');
/* if(Air_temp != null){
Air_temp=trim(Air_temp);
}*/
text(Air_temp, 1, 60);
//text(Oil_temp,width/10,height/20);
// text(Battey_voltage,10,40);
}
void writeText(String textToWrite) {
background(255);
fill(0);
text(textToWrite, width/20, height/2);
}
Here's the result that I get from processing:
Answers
I am curious why you would want to send the presentation layer stuff ("Air temp is: ") through the serial port. I would think it would be more efficient to just pass the data and allow the Processing sketch to label it. I have seen some serialEvent() code that may do this better for you:
You can use bufferUntil() to detect a character like newline to fire off the serialEvent. In the code above, vars with "g_" in the name are the global vars to be set.