I am in the process of building an ROV and would like to add a safety feature to the code. If the the onboard Arduino stops getting commands over serial I would like to shut if off after 1 second or so of no communication with the surface. I have tried a few things but really am lost here at keeeping track of elapsed time. Here is what I have so far, it does not work, it only reduces motor speed to near zero.
if (Serial.available ()){
processInput ();
}
else
{
current = millis();
if (current-last > interval)
{
last = current;
digitalWrite(M1A, LOW);
digitalWrite(M1B, LOW);
}
}
Which means it might be even better to put it in processInput().
So then you could write loop() very cleanly as
void loop()
{
int now=millis();
if (Serial.available()) // Serial Received "Event"
{
processInput();
last=now; // reset serial timer
}
if ( now - last > interval) // Serial Timer Expired "Event"
{
digitalWrite( M1B, LOW); // turn off motors
}
}
Note that "else" is not actually necessary.
Grabbing now=millis() at the start of the loop is a often handy.
Better variable names might include:
last ==> lastSerialProcessed or serialTimerStarted (or comm instead of serial)
interval ==> serialTimeout or serialTimeOutInterval
To be honest, I grabbed that part of the code from somewhere on the interwebs and tweaked it to make it work for me. I do not fully understand it, any simple way to explain to me how that part of the code works?
mech_eng:
To be honest, I grabbed that part of the code from somewhere on the interwebs and tweaked it to make it work for me. I do not fully understand it, any simple way to explain to me how that part of the code works?
It looks like it is reading command data (left, right, speed) from Serial in a format like <123><2><-23><1><345>. What type of device or program is sending to it?