Go Down

Topic: Newbie needing help Timers issue. (Read 452 times) previous topic - next topic

MikelPikel

Apr 15, 2018, 12:09 am Last Edit: Apr 15, 2018, 05:47 am by MikelPikel
Hi

I need to some how only request the timer on the interval to start when the High pushbutton has been pressed or for it to stay reset after Low Pushbutton.

Any advice on how I could do this would be much appreciated!!!!

START OF CODE:

//Buttons simulating level Switch
const int highL = 3;    
const int lowL = 2;

//LEDs simulating Pumps & Alarm
const int Pump1 = 7;
const int Alarm = 6;
const int Pump2 = 5;

unsigned long previousMillis = 0;
unsigned long currentMillis;

//Switch State set to Low
int switchState = 0;
int interval = 10000;

void setup() {

// initialize the digital pin as an output.
 pinMode(Pump1, OUTPUT);
 pinMode(Alarm, OUTPUT);
 pinMode(Pump2, OUTPUT);
 pinMode(highL, INPUT_PULLUP);
 pinMode(lowL, INPUT_PULLUP);
}

void loop() {

 switchState = digitalRead(highL);  

//If Level is High Output Required.
 if(switchState== HIGH){
   digitalWrite(Pump1,HIGH);  
 }
 
 switchState = digitalRead(lowL);  

//If Level is Low no Output Required.

 if(switchState== HIGH){

   digitalWrite(Pump1,LOW);
   digitalWrite(Pump2,LOW);}
 

 unsigned long currentMillis = millis();

 if ((unsigned long)(currentMillis - previousMillis) >= interval) {

  digitalWrite(Pump2, !digitalRead(Pump2));
  previousMillis = currentMillis;}  

 
}







Grumpy_Mike

Quote
need to some how only request the timer on the interval to start when the High pushbutton has been pressed or for it to stay reset after Low Pushbutton.
Set the previousMillis to the current time only when you see  pushbutton pressed, not in the time out section.

You need the variable interval to be the same unsigned long as the time variables and you don't need a cast in the if statement.

MikelPikel

#2
Apr 16, 2018, 02:58 am Last Edit: Apr 16, 2018, 03:00 am by MikelPikel
 Not sure if this is what you meant but now the red LED won't come on anymore.  :o

switchState = digitalRead(lowL);
//If Level is Low no Output Required.
 if(switchState== HIGH){

   digitalWrite(Pump1,LOW);
   digitalWrite(Pump2,LOW);}
 
//when interval has ran out (2seconds) turn on pump2

  if ((currentMillis - previousMillis) >= interval) {

  digitalWrite(Pump2, !digitalRead(Pump2));
  previousMillis = currentMillis;} 

 
}


 
}

Grumpy_Mike

Quote
Not sure if this is what you meant
It is not what I said, in any way.

Quote
Set the previousMillis to the current time only when you see  pushbutton pressed, not in the time out section.
I do not see you doing that.

note that your buttons should be wired between input and ground and when pushed they will read LOW.

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

MikelPikel

Ok ill have a look, I'm totally new to this so sorry if I have come across as brain dead!

Go Up