Is there a way to create a delay without useing Delay()?
I have Data reporting every second. I also have a Digi pin go HIGH for 5 seconds every 10 min. However, when this pin is high the data reporting stops until that function returns 5 seconds later.
So I’m either missing something simple or that is the way it is.
void loop()
{
// Listen for Incoming data
if(Serial.available() > 0)
{
// First byte is Command type
c = Serial.read();
switch(c)
{
case 'P': Purge(2500);break;
// case 'R': iR_State = SetRelay();break;
case 'S': SetTankSpan(); break;
default: break;
}
Serial.flush();
}
delay(100);
// Data Start
if(millis() - PreMillis > REPORT_TIME)
{
PreMillis = millis();
ReportData();
}
Without the delay(100); in there, the SetTankSpan() function call gets inturupted by the ReportData() function call. The SetTankSpan() call reads more incoming data where ReportData() sends data out the com port.
case 'P' still functions with or without the delay but case 'S' is followed by more data.
EDIT
It must be looping back before the SetTankSpan() function can finish. only thing I can think of.
The first thing I see when I look at this code is this:
Serial.flush();
Please tell me you know what this function does, and why you need to use it.
We need to see the code for the SetTankSpan() function. Your surmise that SetTankSpan is returning before you expect it to is probably correct. There is either a problem with the code or a problem with your expectations. Need to see the code to know which is correct.
The most likely cause is that serial data transmission is relatively slow, while reading serial data is not. It is likely that SetTankSpan runs out of data to read before it reads all the data that it needs to.
Is there some marker in the data stream that tells SetTankSpan when it has read enough data?
You have attached something to the Arduino, and arranged for it to send serial data.
There is no handshaking going on. That is, the device sending serial data is not sending it specifically in a request for data. It is sending it at its own pace. Periodically, you dump random amounts of data that the device has sent.
I say random amounts because you have no idea, when you call Serial.flush() whether there are 0 bytes or 100 bytes. You don't read and ignore complete packets. You just pull the plug and drain the serial buffer of whatever data it held.
Unless you understand that this is what you are doing, and there is good reason for doing this, it is generally a bad idea to do this.