We are using an Arduino Due to send serial data via usb to a piece of software produced in visual studio.
The problem we are having lies with the Due. We have a section of code that is time critical
PumpCycleTime = millis();
// Loop this until the pumpHIGH duration has expired
do
{
digitalWrite(Pump, HIGH);
SendSerialData();
}
while (millis() - PumpCycleTime < PumpHIGH);
// Record the pump cycle LOW start time
PumpCycleTime = millis();
// Loop this until the pumpLOW duration has expired
do
{
digitalWrite(Pump, LOW);
SendSerialData();
}
while (millis() - PumpCycleTime < 1);
Initially we were controlling the frequency of the pump using delays. Which gave us reliable frequencies when measure on an oscilloscope. However this of course compromised the plotting of the graphs as there was gaps in the data.
With millis() we are unable to produce the same frequency. Obviously we could compensate to achieve a single frequency however we need to be able to use a range frequencies reliably.
It seems the SendSerialData(), which is a function with around 15 serial.print's, is effecting the timings as without it the frequency is as expected.
Does anyone know of any solutions? Data logging and plotting to a graph in real time is essential and cannot be compromised.
Thanks in advance
If the time is so critical DON'T use delay()
, use Finite State Machine.
More then this, what is your baud rate? if it's 9600 there is a big chance that is your slowing down because the println();
, try to use a bigger baud (like 115200).
Hope it's help.
Yoav