I am new to Arduino and have tried all kinds of code for attempting to achieve this. None with any luck, however. I feel that it should be a relatively easy task and therefore, figured that someone out there with more proficiency with an Arduino than me, would be able to help.
Essentially, I have two commands sent to a device via the Leonardo serial port. One is the single character "R", and the other is the single character "O". The device then sends back 8 binary bytes (non ASCII) in floating point format per command. So if I send O, it will return 8 binary bytes, and then the same goes for if I sent R.
The commands O and R need to be simultaneously sent via the serial every 250ms and wait for their respective responses before sending the next command.
I also need to store the device's response to O and the device's response to R in respective variables that will have a new value every 250ms. I am not familiar with single point format and therefore am unaware of what variable system to use.
Here is my best, yet unsuccessful attempt:
unsigned long previousMillis = 0;
const long interval = 250;
int responseO;
int responseR;
void setup() {
Serial.begin(9600);
while (!Serial);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) //timer for every 250ms
{
previousMillis = currentMillis;
Serial.println("R"); //Figured that "println" with a string for the character R should be ok
if(Serial.available() >0) //Don't know if I am correct here, I was hoping that the serial would only be read if the device responded to the command R
{responseR = Serial.read();} //Read serial for device's response to command R and save it in a variable
Serial.println("O"); //Do the same thing for the command O
if(Serial.available() >0)
{responseO = Serial.read()}
}
}
However, whenever I test this, it appears that the program is unable to differentiate between which responses are due to one command, and treats one response as if both commands requested it when only one did. Also is a floating point format variable just a float?
Thank you so so much, I really need some help this has been burning away at me for days........
Cheers, Harry