I have been trying to send values from a text file on my computer to an Arduino Mega 2560. I have a python code on my computer than sends a series of floats to the serial port as an array of bytes delimited by commas (','). The arduino reads these values from the serial port as Strings up to the delimiter ('.'), converts the string into a float, and plugs it into a float array (deg[]). This is done for every index of the array until the array is full. I then call values form the array to actuate a stepper motor. This is working well... well it works well when I call the values of deg[] inside of the if(serial.available>0){} brackets. However, when I try to use the values from deg[] outside of this, it is as though they disappear. I was curious so I had the arduino board print the value inside and outside the brackets, and I got b'' outside of the brackets.
Does anyone know why this happens? Is the value stored in deg[] only stored in a temporary memory bank?
well, it seems that the values are of Global scope so something else may be wrong.
try this:
void loop ()
{
if (Serial.available() > 0)
{
for (int i = 0; i < 5; i++)
{
deg[i] = Serial.readStringUntil(',').toFloat();
}
//stpmv(deg[2], motor);
Serial.println(deg[2]);
delay (1000);
//stpmv(deg[4], motor);
Serial.println(deg[4]);
delay (1000);
}
for(auto i : deg){ . // this is outside that block...
Serial.println(i);
}
delay(1000); // not to spam your Serial monitor
}
Thank you both. I will give these a try and see what works best.
I am still very new to serial communications (and Arduino in general... programming for that matter...), and I had hoped that using strings would simplify the problem enough to get me through this project. However, Whandall's example appears much more robust than my original plan of attack. Thank you once again.