I seem to be having a problem with an int writing at the correct time. In the below code I have a button setup to send a serial command to a device($ABCDEF=?#). The device then returns a serial command($CAMGET=1234#) that is converted to an int(integerFromPC) which needs to print to serial monitor.
I can get this to work using two buttons, one to send the command to the device and a second button to read the return command and then print it to the serial monitor. However, if I try and do it all on one button then I get the incorrect value for integerFromPC the first time the button is pressed and the correct value the second time it's pressed.
Can anyone shed some light as to what I have out of order and how I might fix it? Sorry for the sloppy code.
#define HWSERIAL Serial2
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[32]; // temporary array for use by strtok() function
/*---comparing header types from Camera---*/
int messageFound = -1;
char messageBuffer[2][7] = {"CAMACK", "CAMGET"};
/*---THE NEXT 3 LINES ARE PART OF THE PARSE AND HEX CONVERSION---*/
char messageFromPC[32] = {0};
char dataFromPC[5] = {0};
int integerFromPC = 0;
boolean newData = false;
const int buttonPin = 8;
int buttonState = 0;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
HWSERIAL.begin(38400);
delay(1000);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Serial.print("RED RIGHT "); //debug only
HWSERIAL.println("$ABCDEF=?#");
delay(500);
integerFromPC = map(integerFromPC, 0, 10239, 0 , 1000);
Serial.println(integerFromPC);
}
recvWithStartEndMarkers();
strcpy(tempChars, receivedChars);
parseData();
showNewData();
}
/*---CAPTURING THE SERIAL DATA FROM CAMERA STARTING WITH "$" AND ENDING WITH "#" ---*/
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '
serial monitor returns - where the 0 should read the same value as New Integer
RED RIGHT 0
Camera Returned 1880
New Integer 6272
;
char endMarker = '#';
char rc;
// if (Serial.available() > 0) {
while (HWSERIAL.available() > 0 && newData == false) {
rc = HWSERIAL.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;
}
}
}
/---PRINTING THE CAPTURED DATA TO SERIAL MONITOR FOR DEBUG ONLY---/
void showNewData() {
if (newData == true) {
/* FOR DEBUG ONLY
Serial.print("Input = ");
Serial.println(receivedChars);
Serial.print("Message = ");
Serial.println(messageFromPC);
Serial.print("Data = ");
Serial.println(dataFromPC);
Serial.print("Integer = ");
Serial.println(integerFromPC);
/
/---looking at the camera's return message to see if it's valid and what to do with it---*/
for (int i = 0; i < 2; i++) {
if (strcmp(messageFromPC, messageBuffer[i]) == 0) {
messageFound = i;
break;
}
}
switch (messageFound) {
case 0:
Serial.print("Command Acknowledged ");
Serial.println(dataFromPC);
messageFound = -1;
break;
case 1:
Serial.print("Camera Returned ");
Serial.println(dataFromPC);
Serial.print(" New Integer ");
Serial.println(integerFromPC);
messageFound = -1;
break;
default:
Serial.print("Invalid ");
Serial.println(receivedChars);
break;
}
newData = false;
}
}
/---SPLIT THE DATA INTO ITS TWO PARTS---/
void parseData() {
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
strcpy(dataFromPC, strtokIndx); //copy it to dataFromPC
sscanf(dataFromPC, "%x", &integerFromPC); //converts HEX string to a DEC int
}
serial monitor returns - where the 0 should read the same value as New Integer
RED RIGHT 0
Camera Returned 1880
New Integer 6272