hi everyone, im developing a code where it has two pumps, PUMP 1 and PUMP 2...PUMP 1 will only triggered when a button in pressed, but PUMP2 will triggered when the arduino is powered up and it will run for first 15mins then 1min once after that (ON and OFF)...right now my program is not working where even the pump1 is nt triggering when the button is pressed..please help me
Remember, the output only changes when you trace across a digitalWrite() statement.
So you change the variable Pump2State, but neglect to write it to the output pin. Thus you see now activity on the Pump2 pin, or you relay or whatever drives the pump.
There was a flaw in your program beyond the blocking nature of your limo control.
Now go apply “blink without delay” principles and code your own “pump on and off without delay”… pump on and off are analogous to pump on and off, so.
I assume he don't understand how to use millis, among other things.
In case you don't know, millis() return the amount of seconds that has passed since the microcontroller/arduino board was reset.
So instead of doing delay(90000) (which tell the processor to not do anything for 15 minutes or whatever), you should be comparing the time between when you press the button and when 15 minutes have elapsed, since the internal clock keeps ticking as long as the MCU is powered. So instead of
//global var
uint32_t timeS0=0;
uint32_t timeS1=0;
//uint32_t timeS2=0;
circulation() {
timeS1 = millis(); //because millis() twice might take longer than storing the value
if ((timerS1-900000) >= timerS0) { //compare current time to "last flipped at" time
pump2State = !pump2State; //flip the boolean
timerS0 = timerS1; //set the "last flipped at" time to current time
}
}
If you want it to be on for a different amount of time it is off, you can branch the timer:
timerS0 = millis(); //because millis() twice might take longer than storing the value
if (pump2State) {
if ((timerS0-900000) >= timerS1) { //compare current time to "last flipped at" time
pump2State = false;
timerS1 = timerS0; //set the "last flipped at" time to current time
}
} else //at the moment of flipping sides, S1 will be set to current time, so we dont need separate variables
if ((timerS0-60000) >= timerS1) {
pump2State = true;
timerS1 = timerS0;
}
The max value for uint32_t is 4B. However, because millis() returns uint32_t and it's milisecond, it will overflow in 49.7094907 days after you last press the reset button or last plug it back in.
I don't know how will the comparison between a 'overflown value' and a 'non-overflown value that became overflown because of arithmetic operations' will result. I assume that since both of them are overflown the result should be consistent. However the problem will be that if one of the value has overflown while the other has not, it will take another cycle to resolve this issue (by adding the overflown value back up)
Hello
The start task is to build a timer, BWOD example based, which is event controlled like a, I will use the german word, "Eieruhr".
Have a nice day and enjoy coding in C++.
While your precision is commendable your accuracy is lacking.
I'm hoping you take the time to correct your sample code after learning the correct way to manage time so anyone stumbling on this thread is not led down the wrong path.
I specifically mentioned timer overflow because I am unable to figure out how to handle it. And I expect everyone that copy-paste the code to at least read the post and thus, aware of the flaw.
Stop shitting on everyone and describe them as a complete fool because not everyone know everything. Correction is great, but unnecessary toxicity is less than desirable.
The code is fixed, more or less. For those nitpick.
const byte Pump1 = 3;
const byte Pump2 = 5;
const byte manualpin = 27;
byte Pump1State = HIGH;
byte Pump2State = HIGH;
byte manualmodeState = 0;
unsigned long startTime, waitTime;
byte bhistory;
word bstart, bwait = 500; // 16 bit micros to time 1/2 millis.
void setup()
{
pinMode(Pump1, OUTPUT); //Pump 1
pinMode(Pump2, OUTPUT); //Pump 2
pinMode(manualpin, INPUT); //Manual push button
}
void loop()
{
manualButton();
manual();
circulation();
}
void manualButton() // because button state BOUNCES on change
// therefore a dedicated button handler updates 8 pin states over 4 millis time.
// When you get used to non-blocking Arduino code, one milli isn't short.
{
if ( micros() - bstart >= bwait )
{
bstart += bwait;
bhistory << 1; // moves the bits in bhistory up 1, same result as x2
bhistory += digitalRead(manualmode);
}
}
void manual() // this gets checked 1000's of times per millisecond
{
if ( bhistory == 0b01111111 ) // oldest bit is button UP with 7 button DOWN reads
// that means 3.5 millis without detecting bounce is taken as a stable state.
// this if() checks to see if the button changed from UP to DOWN+stable.
{
Pump1State = LOW;;
}
else //
{
Pump1State = HIGH;
}
digitalWrite(Pump1, Pump1State);
}
void circulation()
{
static byte circState = 0;
if (waitTime > 0 ) // 1-shot timer
{
if ( millis() - startTime >= waitTime )
{
waitTime = 0;
}
else
{
return;
}
}
switch( circState )
{
case 0 :
Pump2State = LOW;
startTime = millis();
waitTime = 900000;
circState = 1; // after the 1-shot timer finishes, run case 1
break;
case 1 :
Pump2State = HIGH;
startTime = millis();
waitTime = 60000;
circState = 2; // after the 1-shot timer finishes, run case 2
break;
case 2 :
Pump2State = LOW;
startTime = millis();
waitTime = 60000;
circState = 0; // after the 1-shot timer finishes, run case 0
break;
}
Not compiled or debugged but there's not room for much bugs, I promise and this code is not complicated though for you it has a few lessons to figure out... with help if it's not soon clear.