I have the following part of a big code which sends a hex code to a torque sensor thought serial1 and receives a response then it converts the hex value to decimal, converts the decimal to float, then sends it to serial0 (pc).
This should happen while a stepper motor is running using Accelstepper library.
The motor is running fast and smoothly without the MeasureTorque(), function but while using the MeasureTorque() function the stepper is so slow.
As I understood, the Serial1.readbytes is blocking my code between steps, so what is another way to do the same without blocking the code?
I'm using arduino mega2560 if that matters.
Here is the relevant part of the code:
void MeasureTorque(){
Serial1.write(getvalue, sizeof(getvalue));
Serial1.readBytes(response, sizeof(response));
decvalue = (response[3]<<24) | (response[4]<<16) | (response[5]<<8) | response[6];
if(decvalue>>9999){
decvalue = -(4294967296-decvalue);
fltvalue = (float)decvalue/100.0;
}
else{
fltvalue = (float)decvalue/100.0;
}
Serial.println(fltvalue);
memset(response, 0, sizeof(response));
decvalue=0;
fltvalue=0;
}
void loop() {
CheckButtons();
Motor.run();
CheckQueuedTest();
MeasureTorque();
}
I tried to make the code receive one byte a time during one loop cycle but it didn't work, seems I didn't understand well how that should be done.