Hi All
I have knocked together a £3 ardiuno UNO on a bread board and sending 2 packets of data from a 28x2 to it.
The uno is doing a bit of floating maths then sending back the result after converting it into intreger for the picaxe
to deal with.
Have knocked up a simple test code for the picaxe to test the sending and returning result.
How ever I can not figure out how to handle the data on the picaxe side, capturing it in a word variable but the results are wrong
Picaxe code
[]
symbol tempC= w5
symbol RH= w6
symbol VPD=w7
tempC=10
RH=15
pause 1000
do
serout C.0,T9600,("<",#tempC,",",#RH,">")
serin C.1,T9600,VPD
'VPD = b3-"0"*10 + b2-"0" * 10 + b1-"0" * 10 + b0-"0"
if RH >= 86 then
RH=15
inc tempC
endif
sertxd(" VPD= ",#vpd,cr,lf)
inc RH
pause 500
loop
For those with a nervious dispersition the Ardiuno code
[]
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
//char messageFromPC[numChars] = {0};
int area ={0};
float RH = 0.0;
// varables for calData
int SVP = 0;
float temP = 0.0;
float VPD = 0.0;
int back = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
//strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
// strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
area = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
RH = atof(strtokIndx); // convert this part to integer
Serial.print("AREA ");
Serial.println(area);// for testing only
Serial.print("RH ");
Serial.println(RH);//for testing only
switch (area) {
case 0: SVP = 611; break; case 1: SVP = 657; break; case 2: SVP = 706; break; case 3: SVP = 758; break; case 4: SVP = 813; break; case 5: SVP = 872; break; case 6: SVP = 935; break;
case 7: SVP = 1102; break; case 8: SVP = 1073; break; case 9: SVP = 1148; break; case 10: SVP = 1228; break; case 11: SVP = 1312; break; case 12: SVP = 1402; break; case 13: SVP = 1497; break;
case 14: SVP = 1598; break; case 15: SVP = 1705; break; case 16: SVP = 1818; break; case 17: SVP = 1937; break; case 18: SVP = 2064; break; case 20: SVP = 2197; break; case 21: SVP = 2338; break;
case 22: SVP = 2486; break; case 23: SVP = 2809; break; case 24: SVP = 2983; break; case 25: SVP = 3167; break; case 26: SVP = 3361; break; case 27: SVP = 3565; break; case 28: SVP = 3779; break;
case 29: SVP = 4005; break; case 30: SVP = 4242; break; case 31: SVP = 4492; break; case 32: SVP = 4754; break; case 33: SVP = 5029; break; case 34: SVP = 5318; break;
case 35: SVP = 5621; break; case 36: SVP = 5940; break; case 37: SVP = 6273; break; case 38: SVP = 6623; break; case 39: SVP = 6990; break; case 40: SVP = 7374; break;
case 41: SVP = 7776; break;
}
// Serial.print("SVP ");
// Serial.println(SVP);
float temP = 100 - RH;
temP = temP/100;
// Serial.print("temP ");
// Serial.println(temP);
VPD = (float) temP * (int) SVP;
// Serial.print("VPD ");
// Serial.println(VPD);
VPD = round(VPD);
back = int (VPD);
}
//============
void showParsedData() {
// Serial.print("Picaxe ");
Serial.println(back);//result back to Picaxe
}
[/]
[/]
The Uno is recieving the data correctly and its results are right, just baffled on the Picaxe side
regards john
I have knocked together a £3 ardiuno UNO on a bread board and sending 2 packets of data from a 28x2 to it.
The uno is doing a bit of floating maths then sending back the result after converting it into intreger for the picaxe
to deal with.
Have knocked up a simple test code for the picaxe to test the sending and returning result.
How ever I can not figure out how to handle the data on the picaxe side, capturing it in a word variable but the results are wrong
Picaxe code
[]
symbol tempC= w5
symbol RH= w6
symbol VPD=w7
tempC=10
RH=15
pause 1000
do
serout C.0,T9600,("<",#tempC,",",#RH,">")
serin C.1,T9600,VPD
'VPD = b3-"0"*10 + b2-"0" * 10 + b1-"0" * 10 + b0-"0"
if RH >= 86 then
RH=15
inc tempC
endif
sertxd(" VPD= ",#vpd,cr,lf)
inc RH
pause 500
loop
For those with a nervious dispersition the Ardiuno code
[]
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
//char messageFromPC[numChars] = {0};
int area ={0};
float RH = 0.0;
// varables for calData
int SVP = 0;
float temP = 0.0;
float VPD = 0.0;
int back = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
//strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
// strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
area = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
RH = atof(strtokIndx); // convert this part to integer
Serial.print("AREA ");
Serial.println(area);// for testing only
Serial.print("RH ");
Serial.println(RH);//for testing only
switch (area) {
case 0: SVP = 611; break; case 1: SVP = 657; break; case 2: SVP = 706; break; case 3: SVP = 758; break; case 4: SVP = 813; break; case 5: SVP = 872; break; case 6: SVP = 935; break;
case 7: SVP = 1102; break; case 8: SVP = 1073; break; case 9: SVP = 1148; break; case 10: SVP = 1228; break; case 11: SVP = 1312; break; case 12: SVP = 1402; break; case 13: SVP = 1497; break;
case 14: SVP = 1598; break; case 15: SVP = 1705; break; case 16: SVP = 1818; break; case 17: SVP = 1937; break; case 18: SVP = 2064; break; case 20: SVP = 2197; break; case 21: SVP = 2338; break;
case 22: SVP = 2486; break; case 23: SVP = 2809; break; case 24: SVP = 2983; break; case 25: SVP = 3167; break; case 26: SVP = 3361; break; case 27: SVP = 3565; break; case 28: SVP = 3779; break;
case 29: SVP = 4005; break; case 30: SVP = 4242; break; case 31: SVP = 4492; break; case 32: SVP = 4754; break; case 33: SVP = 5029; break; case 34: SVP = 5318; break;
case 35: SVP = 5621; break; case 36: SVP = 5940; break; case 37: SVP = 6273; break; case 38: SVP = 6623; break; case 39: SVP = 6990; break; case 40: SVP = 7374; break;
case 41: SVP = 7776; break;
}
// Serial.print("SVP ");
// Serial.println(SVP);
float temP = 100 - RH;
temP = temP/100;
// Serial.print("temP ");
// Serial.println(temP);
VPD = (float) temP * (int) SVP;
// Serial.print("VPD ");
// Serial.println(VPD);
VPD = round(VPD);
back = int (VPD);
}
//============
void showParsedData() {
// Serial.print("Picaxe ");
Serial.println(back);//result back to Picaxe
}
[/]
[/]
The Uno is recieving the data correctly and its results are right, just baffled on the Picaxe side
regards john
Attachments
-
28.5 KB Views: 2