This is the relevant part of my code I'm trying to improve. In the pumping loop, there is always an analogRead, and only if there is a change greater than a tolerance, then it recalculates the delay and refreshes the LCD. Else, it goes into the pumping function (it is 8 high low switching steps, but I only included 1 for space).
What I'm thinking is reducing the sample frequency (I gave it a time change tolerance of 500 ms, anyway), but I'd like the analogRead to be faster, if possible.
Code: Select all
case 0:
potValueRead = analogRead(potInput);
diffPot = abs(potValueRead - potValueOld);
diffTimeChange = timeCurrent - timeChange;
if(switchValueA != switchValueALast) {
modeString = "Pumping";
delayCalc(potValueRead, potValueLow, potValueHigh, potFlowValue, potDelayValue);
potValueOld = potValueRead;
LCDFlowStart(potFlowValue);
digitalWrite(enableState, LOW);
switchValueALast = 0;
}
if(diffPot < changeTolerance) {
timeChange = millis();
}
if(diffPot >= changeTolerance) {
delayCalc(potValueRead, potValueLow, potValueHigh, potFlowValue, potDelayValue);
}
if(diffPot >= changeTolerance && diffTimeChange >= 500) {
LCDFlowUpdate(potFlowValue);
potValueOld = potValueRead;
timeChange = millis();
}
else {
pump(potDelayValue);
}
break;
.
.
.
inline void pump() __attribute__((always_inline));
void pump(int _DelayValue) {
digitalWriteFast(PA_9, HIGH);
delayMicroseconds(_DelayValue);
digitalWriteFast(PA_9, LOW);
delayMicroseconds(_DelayValue);
}