I am new to Arduino programming and electronics in general.
I am designing a vacuum system that uses a diaphragm pump. I want to implement a timer in my system to detect it the pump has been running for more than 45secs and thus cause an led to indicate this 45secs runtime status. Please I am lost on how to implement this function in an Arduino program. Thank you for your help
const int GLED = 13;
const int PRESS = A5;
const int POT = A3;
const int PUMP = 4;
bool gledVal = LOW;
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 45000; // interval at which to blink (milliseconds)
int PRESS_INIT = 0;
float PRESS_INIT_VOLT = 0.0;
float PRESS_INIT_BAR = 0.0;
float PRESS_INIT_MMHG = 0.0;
int pressVal = 0;
float pressVal_Volts = 0.0;
float pressVal_Bar = 0.0;
float pressVal_mmHg = 0.0;
float pressVal_Gauge = 0.0;
int potVal = 0;
int pumpVal = 0;
void setup() {
Serial.begin(9600);
pinMode(GLED,OUTPUT);
digitalWrite(GLED,gledVal);
pinMode(PRESS,INPUT);
PRESS_INIT = analogRead(PRESS);
PRESS_INIT_VOLT = (PRESS_INIT*5.0)/1023.0;
PRESS_INIT_BAR = (PRESS_INIT_VOLT - (3.3*0.1))/(0.8*3.3);
PRESS_INIT_MMHG = PRESS_INIT_BAR * 750;
pinMode(POT,INPUT);
pinMode(PUMP,OUTPUT);
}
void loop() {
pressVal = analogRead(PRESS);
pressVal_Volts = (pressVal*5.0)/1023.0;
pressVal_Bar = (pressVal_Volts - (3.3*0.1))/(0.8*3.3);
pressVal_mmHg = pressVal_Bar * 750;
pressVal_Gauge = PRESS_INIT_MMHG - pressVal_mmHg;
//Portion that implements the timer
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (gledVal == LOW) {
gledVal = HIGH;
} else {
gledVal = LOW;
}
digitalWrite(GLED, gledVal);
}
potVal = analogRead(POT);
pumpVal = map(potVal,30,688,50,175);
analogWrite(PUMP,pumpVal);
}
Save the value of millis() when the pump is started. Then, each time through loop(), if the pump is running, test whether the current value of millis() minus the saved value is greater than the required period. If so, the pump has been running that long
Using a boolean set to true when the pump is running and false when it is not makes the process easier
One obvious problem is that you unconditionally check whether the period has passed whether or not the pump is running
You need to do something like this
Save the value of millis() when the pump starts and set a boolean to true to indicate that timing is taking place.
Each time through loop(), if the boolean is true then test whether the required period has elapsed by subtracting the start value from the current value of millis(). If so, act accordingly.
If the pump stops before the period elapses then set the boolean to false to stop timing
My millisDelay class has that boolean internally to keep track of if the timer is running. But good coding practice to handle all those details yourself.
I see the problem may not have to do with my timer, but the map function I have used to control the speed of the pump.
The mapped values I have do not actually control the speed or power delivered to the pump, instead it just control the ON/OFF state, which isn't what I wrote the program for anyway.
Hi @orhema
in this line: pumpVal = map (potVal, 30, 688, 50, 175);
portVal receives the value of an anlog port and they can be from 0 to 1023.
and in this line:
analogWrite (PUMP, pumpVal);
and dutty can be from 0 to 255.
Correct to use like this:
pumpVal = map (potVal, 0, 1023, 0, 255);
Thank you for your reply. However, I tried that and it still doesn't change the speed for me. I am using g a 12V/2A pump driven by a MOSFET connected to the Arduino, and all it does is switch the pump on/off despite the cnage in POT value.