I have cobbled together an RC car that uses serial over bluetooth to control it. All is good and it works perfectly, but...
I need a fail safe.
The serial data comes in as a continuous stream. If for whatever reason this stream gets broken due to loss of bluetooth comms, I need the servos to default to a set pwm.
I have tried a few things, but no luck, so asking for some help with this. How can I detect that the serial stream has gone. Serial.available does not work.
Serial.available will work if you couple it with a timer.
If for whatever reason this stream gets broken due to loss of bluetooth comms ...
You need to define "gets broken" in terms of a time frame. Does lack of communication for 1 second make it "broken"? Does lack of commo for 5 seconds make it broken?
I have a counter target of x. This increments as the loop runs. Each time a serial command comes in, the counter gets reset. So if there is no serial data the counter expires and I get my fail safe
One question left. If I want my counter to equal 1 second, what is the value, ie how many counts per second per iteration of the main loop?
That method will work well enough for your purposes. It sounds like you check for serial input often enough that there won't be long threads of code running that would cause that technique to fail. Tip-o-the-hat to you for coming up with that creative solution.
If I want my counter to equal 1 second, what is the value, ie how many counts per second per iteration of the main loop?
The answer depends entirely on what else your code does, so it can't be answered without seeing the rest of the loop, and will change every time you change your code anyway, not to mention the varying logic paths your code will be taking inside the loop, so don't go there.
A real-world approach to getting the value is to start with your estimated 45000 and see what effect raising or lowering that number has. IOW, use the trial and error method.
Remember that the maximum number that can be held in an int data type is 32767, so define your counter as a long data type to be on the safe side and compare it to 45000L. (If you need to, see the bottom of http://www.arduino.cc/en/Reference/IntegerConstants for an explanation of the 'L' at the end there.)