Some methods for receiving Serial commands have built-in timeouts. There might be other things to consider, as well. I use this sketch to turn on an LED listening to the Serial interface (via USB):
#define LED 13
#define WIRE_OUT 10
#define SENSOR 5
int sensorValue = 0;
void setup()
{
Serial.begin(115200); // set serial speed
pinMode(LED, OUTPUT); // set LED as output
pinMode(WIRE_OUT, OUTPUT); // set as output
pinMode(SENSOR, INPUT); // set as output
}
void loop()
{
// get byte from serial interface
int val = Serial.read() - '0';
if (val == 1)
{
// set wire_out active
digitalWrite(WIRE_OUT, HIGH);
// check wire_out
while (sensorValue == 0)
{
sensorValue = digitalRead(SENSOR);
}
// send reply to phone and light led
Serial.write(53); // send back "5"
digitalWrite(LED, HIGH); // signal sensor reading via LED
delay(100); // LED on for 0.1 s
// re-set
digitalWrite(LED, LOW);
digitalWrite(WIRE_OUT, LOW);
sensorValue = 0;
}
}
I am issueing the serial command from an Android phone and see round-trip times (serial command sent -> voltage set to high -> voltage sensed on other pin -> serial reply received on Android) of usually around 2-2.5 ms (with spikes up to 4 ms possible, although rare). This isn't bad and variance should be mostly due to the Android device.
Is there an even faster and more reliable (timing-wise) way?
digitalRead and digitalWrite are not very fast because they have to be compatible with all Arduinos. You can write directly to ports if you are willing to forsake compatibility.
Spending milliseconds to light an LED for a human is not usually an issue. What are you REALLY trying to do?
Also, it is usually poor form to do a Serial.read() without first doing a Serial.available(). In this program it does not matter but I had to read the entire program to verify that, and it may matter when the program gets bigger.
vaj4088:
digitalRead and digitalWrite are not very fast because they have to be compatible with all Arduinos. You can write directly to ports if you are willing to forsake compatibility.
Spending milliseconds to light an LED for a human is not usually an issue. What are you REALLY trying to do?
@vaj4088
Sorry for the long delay in my response, other projects kept me very busy. Thank you very much for the reply!
I am not sure how to directly write to ports, but I will investigate this. In case you have any tipps, I would appreciate them highly!
I am testing the delay of various visual presentation methods on an Android phone. I switch an LED on and wait for the graphics update to happen on the screen. I "wait" by means of a high-speed camera filming both the LED and the screen at the same time.
Currently (with a similar sketch) I observe a "round trip time" of a serial command (Phone->Arduino->Phone) of ~2.5 ms:
Phone sends byte via serial interface
Arduino sets pin to HIGH
Arduino senses the voltage on second pin and replies to phone
Phone performs time measurement and displays the result on screen
This is not bad as it means that a conservative estimation of an LED response would be <2 ms.
I was just wondering whether I could achieve an even better/more responsive "now" marker with different code. I usually find a small, but not insignificant, delay between the RX LED on the Arduino lighting up, and an LED reponse on Pin 13. If possible, I would like to achive an overall LED response time of well below 1 ms.
vaj4088:
Also, it is usually poor form to do a Serial.read() without first doing a Serial.available(). In this program it does not matter but I had to read the entire program to verify that, and it may matter when the program gets bigger.
I can imagine that. Could it be that omitting Serial.available() is faster in this case?
Tbh, this was more or less copy and paste from another web source.