Hello,
I am having trouble receiving variables from Processing to Arduino.
My code aims to tell the Arduino to stop/start/set the system.
assuming I want to set the system processing will execute the following:
s="<2"+t_min_icp.getText()+","+t_max_icp.getText()+","+ t_max_drain.getText()+","+t_drain_rate.getText()+">";
myPort.write(s);
the texts are taken from text fields.
My Arduino code receives the first char, but does not seem to receive the others..
for the following Arduino code
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
bool st = false;
bool set = false;
bool overide = false;
char pre_Data[80];
char *post_Data[80];
float final_Data[80];
char *var = NULL;
byte index;
char inChar;
int stat;
int lower_Pressure;
char s[30];
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
while (Serial.available() > 0) {
Serial.read();
}
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while (Serial.available() > 0)
{
inChar = Serial.read();
if (inChar == SOP)
{
inChar = Serial.read();
index = 0;
pre_Data[index] = '\0';
started = true;
ended = false;
if (inChar == '0') {
digitalWrite(LED_BUILTIN, LOW);
st = false;
}
else if (inChar == '1') {
digitalWrite(LED_BUILTIN, HIGH);
st = true;
}
else if (inChar == '2') {
digitalWrite(LED_BUILTIN, HIGH);
set = true;
}
}
else if (inChar == EOP)
{
ended = true;
break;
}
else if (index < 30)
{
if (set == true || overide == true) {
pre_Data[index] = inChar;
pre_Data[index] = '\0';
}
index++;
}
}
if (started && ended)
{
if (set == true)
{
var = strtok(pre_Data, ",");
index = 0;
while (var != NULL)
{
post_Data[index] = var;
index++;
var = strtok(NULL, ",");
}
for (int i = 0; i < index; i++) {
final_Data[index] = atof(post_Data[index]);
}
lower_Pressure = final_Data[0];
set=false;
}
// Reset for the next packet
started = false;
ended = false;
index = 0;
pre_Data[index] = '\0';
}
if (lower_Pressure == 3)
{
digitalWrite(LED_BUILTIN, HIGH);
}
}
this return 255 (space in ASCII) instead of '2'.
What am I doing wrong?