@Nick Gammon
Thank for the link and tips. I will look into it and study the code.
I was doing my best to code it. I did test my code, using the serial monitor, and it work "fine" <-- so far... but not to well with the PC side. Processing code.
I was trying to send the data the "wrong" way, maybe right, but it did not work to well in Processing code. I have a "sychronazation" issue. I fix the initial problem, ( keep sending data, no waiting for the data ) using the state/flag methode. I was thinking about CrossRoads idea...so I decide the re-send the data one byte at the time, just I received one byte at a time. So I extract the numbers of an integer like : 1023 - take the BCD out, convert them in ASCII and send it. AT the receiving end, one byte at a time, convert ASCII to integer, and calculate the full integer data. And do the calculation ( find the voltage ) and convert to an integer and set the decimal point to 0.000. After that, extract the BCD numbers and send the data one byte at a time.
During testing, it work but, sometime, it have a few issues with a possible synchronization problem ?
Any second opinion will be nice. I will look into Nick Gammon link's.
Here the Arduino Code :
/*
Size : 3566 bytes
Serial data out and data in.
Version 2.0
Setup: a LCD 16 X 2 connect the the Arduino
Author setup : Using a DIY LCD Sheild
Analog pin 2 connected to nothing.
By Serge J Desjardins
aka techone / tech37
Toronto, Ontario, Canada
Compile and Tested
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,10,9,8,7);
const byte anapin = 2;
unsigned int my_value;
unsigned char incom[8];
int outdata[4];
int j;
boolean state;
void setup()
{
analogReference(DEFAULT);
Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
lcd.print("V : ");
lcd.setCursor(11,0);
lcd.print(" Volt");
for (int i=0;i<8;i++)
{
incom[i]=0x20;
}
state=0;
}
void loop()
{
if (state==0)
{
my_value = analogRead( anapin );
outdata[0] = my_value / 1000;
my_value = my_value -( outdata[0] * 1000 );
outdata[1] = my_value / 100;
my_value = my_value -( outdata[1] * 100 );
outdata[2] = my_value / 10;
outdata[3]= my_value % 10;
for (int i=0;i<4;i++)
{
Serial.write((outdata[i]+48));
}
Serial.print(">");
state=1;
}
delay(100);
j=0;
while ( Serial.available() )
{
incom[j] = Serial.read();
if (incom[j]=='>' )
{
state=0;
break;
}
j++;
if (j==8)
{
state=0;
break;
}
}
lcd.setCursor(4,0);
lcd.print(" ");
for (int i=0;i<7;i++)
{
if (incom[i]=='>') break;
lcd.setCursor((4+i),0);
lcd.write(incom[i]);
}
delay(1000);
}
Here the Processing code.
/*
The PC section. It received data , process it and send data
*/
import processing.serial.*;
Serial myPort;
int[] inbyte = new int[5];
int[] outbyte = new int[4];
int j;
float voltage;
int answer;
int sendinfo;
boolean state;
void setup()
{
println(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
//state=0;
}
void draw()
{
if ( myPort.available() > 0 )
{
j=0;
while ( myPort.available() >0 )
{
inbyte[j] = myPort.read();
if (inbyte[j]=='>') break;
j++;
if (j==4) break;
}
answer=(inbyte[0]-48)*1000;
answer=((inbyte[1]-48)*100)+answer;
answer=((inbyte[3]-48)*10)+answer;
answer=(inbyte[3]-48)+answer;
voltage= float(answer)*(5.0/1023.0);
voltage = voltage * 1000.0;
sendinfo = int(voltage);
outbyte[0] = sendinfo / 1000;
sendinfo = sendinfo -( outbyte[0] * 1000 );
outbyte[1] = sendinfo / 100;
sendinfo = sendinfo -( outbyte[1] * 100 );
outbyte[2] = sendinfo / 10;
outbyte[3]= sendinfo % 10;
myPort.write((outbyte[0]+48));
myPort.write(".");
myPort.write((outbyte[1]+48));
myPort.write((outbyte[2]+48));
myPort.write((outbyte[3]+48));
myPort.write(">");
}
}