Code:
String cmdData; //Store the complete command on one line to send to sensor board.
String phResponse; //Store the ph sensor response.
boolean startOfLine = false;
boolean endOfLine = false;
boolean stringComplete = false;
boolean s3Trigger = false;
void setup()
{
Serial3.begin(38400);
Serial.begin(38400);
}
void serialEvent()
{
while (Serial.available())
{
char cmd = (char)Serial.read();
if (cmd == '{')
{
startOfLine = true;
}
if (cmd == '}')
{
endOfLine = true;
}
if (startOfLine && cmd != '{' && cmd != '}')
{
cmdData += cmd;
}
}
}
void serialEvent3()
{
while(Serial3.available())
{
char cmd3 = (char)Serial3.read();
phResponse += String(cmd3);
if (cmd3 == '\r') //if Carriage Return has been found then...
{
stringComplete = true;
}
}
}
void loop()
{
if (startOfLine && endOfLine) //both startOfLine and endOfLine must be true to run the command...
{
startOfLine = false;
endOfLine = false;
s3Trigger = true; //set the s3Trigger boolean to true to check if data on Serial3.available() is available.
runCommand();
}
if (stringComplete)
{
Serial.println("Stored Response: " + phResponse); //print stored response from ph sensor.
phResponse = ""; //empty phResponse variable to get ready for the next response
cmdData = ""; //empty phResponse variable to get ready for the next command
stringComplete = false; //set stringComplete to false
s3Trigger = false; //set s3Trigger to false so it doesn't continuously loop.
}
if (s3Trigger) //if true then continue
{
delay(1000); //delay to make sure the Serial3 buffer has all the data
if (!Serial3.available()) //if Serial3 available then execute the runCommand() function
{
runCommand();
}
else
{
s3Trigger = false; //set s3Trigger to false so it doesn't continuously loop.
}
}
}
void runCommand()
{
cmdData.toLowerCase(); //convert cmdData value to lowercase for sanity reasons
if (cmdData == "ph")
{
ph();
}
}
void ph()
{
Serial.println("Calculating PH sensor value in 3 Seconds");
delay(3000);
Serial3.print("r\r");
}
if (s3Trigger) //if true then continue
{
delay(1000); //delay to make sure the Serial3 buffer has all the data
if (!Serial3.available()) //if Serial3 available then execute the runCommand() function
{
runCommand();
}
else
{
s3Trigger = false; //set s3Trigger to false so it doesn't continuously loop.
}
}
serialEvent3() {} will only work on the second, third and etc... time. That if statement will run the previous command sent via serial over the terminal. Once the Serial3 has data in the buffer, any future commands entered will work as expected. Why is my code behaving as such? I'm not sure implementing that if statement is necessary and that my sketch logic is at fault.
Working Code EDIT:
String cmdData; //Store the complete command on one line to send to sensor board.
String phResponse; //Store the ph sensor response.
boolean startOfLine = false;
boolean endOfLine = false;
boolean stringComplete = false;
boolean s3Trigger = false;
void setup()
{
Serial3.begin(38400);
Serial.begin(38400);
pinMode(2, OUTPUT); //used for temperature probe
}
void runCommand()
{
cmdData.toLowerCase(); //convert cmdData value to lowercase for sanity reasons
Serial.println("runCommand was executed! cmdData: " + cmdData);
if (cmdData == "ph")
{
Serial.println("ph was found");
//Serial3.print("r\r");
ph();
}
if (cmdData == "phatc")
{
phATC();
}
}
float getTemp(char tempType)
{
float v_out; //voltage output from temp sensor
float temp; //the final temperature is stored here (this is only for code clarity)
float tempInCelcius; //stores temperature in C
float tempInFarenheit; //store temperature in F
digitalWrite(A0, LOW); //set pull-up on analog pin
digitalWrite(2, HIGH); //set pin 2 high, this will turn on temp sensor
delay(2); //wait 1 ms for temp to stabilize
v_out = analogRead(0); //read the input pin
digitalWrite(2, LOW); //set pin 2 low, this will turn off temp sensor
v_out*=.0048; //convert ADC points to volts (we are using .0048 because this device is running at 5 volts)
v_out*=1000; //convert volts to millivolts
tempInCelcius = 0.0512 * v_out -20.5128; //the equation from millivolts to temperature
tempInFarenheit = (tempInCelcius * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
if (tempType == 'c')
{
return tempInCelcius; //return temp in celcius
}
else if (tempType == 'f')
{
return tempInFarenheit; //return temp in Farenheit
}
}
void ph()
{
Serial.println("ph function executed!");
Serial3.print("r\r");
}
void phATC()
{
Serial.println("PH Auto Temperature Calibration will start in 3 Seconds");
delay(3000);
float temp = getTemp('c');
char tempAr[10];
String tempAsString;
String tempData;
dtostrf(temp,1,2,tempAr);
tempAsString = String(tempAr);
tempData = tempAsString + '\r';
Serial3.print(tempData);
}
void loop()
{
//while(!Serial.available());
//delay(1000);
while(Serial.available())
{
char cmd = (char)Serial.read();
if (cmd == '{')
{
startOfLine = true;
}
if (cmd == '}')
{
endOfLine = true;
}
if (startOfLine && (cmd != '{' && cmd != '}' && cmd != '\r')) // Must add the \r to filter out the CR.
{
cmdData += cmd;
}
}
if (startOfLine && endOfLine) //both startOfLine and endOfLine must be true to run the command...
{
startOfLine = false;
endOfLine = false;
s3Trigger = true; //set the s3Trigger boolean to true to check if data on Serial3.available() is available.
//Serial.println("brackets!");
runCommand();
}
delay(2000);
if(!Serial3.available() && s3Trigger)
{
runCommand();
}else
{
while(Serial3.available())
{
char cmd3 = (char)Serial3.read();
phResponse += String(cmd3);
if (cmd3 == '\r') //if Carriage Return has been found then...
{
stringComplete = true;
}
}
}
if (stringComplete)
{
Serial.println("Stored Response: " + phResponse); //print stored response from ph sensor.
phResponse = ""; //empty phResponse variable to get ready for the next response
cmdData = ""; //empty phResponse variable to get ready for the next command
stringComplete = false; //set stringComplete to false
s3Trigger = false; //set s3Trigger to false so it doesn't continuously loop.
}
}