Go Down

Topic: esp8266 as countdown timer to control relay  (Read 330 times) previous topic - next topic

tngw1500se

I'm using a NodeMcu (with a momentary SPST button) to switch on a water valve on for 15 minutes when a button press is detected. The code works as is except that there is no way to reset it during the 15 minute cycle. I'd like it to "reset" to 15 min every time the button is pressed. Looks like my "900000" delay causes the Node to just sit there and wait. I guess this is where I need to add in some kind of counter that subtracts an amount from the remaining time, every time it goes through the loop and checks for another button press??? I'm lost. Ideas?

Code: [Select]
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize pins
  pinMode(D5, OUTPUT);
  pinMode(D1, INPUT);
}
void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(D1);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    digitalWrite(D5,HIGH);
    delay(900000); //15 min on time
        }
 else {
    // turn off:
    digitalWrite(D5,LOW);
  }
}

tngw1500se

I think I may have figured it out. Will this work:


Code: [Select]
int buttonState = 0;         // variable for reading the pushbutton status
int timeleft = 0;
void setup() {
  // initialize pins
  pinMode(D5, OUTPUT);
  pinMode(D1, INPUT);
  Serial.begin(9600);
}
void loop() {

delay (10);
 
  // read the state of the pushbutton value:
  buttonState = digitalRead(D1);

if (buttonState == HIGH) {
   (timeleft == 900000);  // sets time to 15 min
    digitalWrite(D5,HIGH);  // set D5 HIGH
    Serial.print("Reset to 15 min");
    delay(2000);
    Serial.print(timeleft);
}

else {
  (timeleft =- 10);
  Serial.print(timeleft);
}

if (timeleft < 0) {
   digitalWrite(D5,LOW);
   Serial.print("Expired");
   (timeleft == 0);
}
}

tngw1500se

#2
Apr 17, 2018, 07:56 pm Last Edit: Apr 17, 2018, 07:57 pm by tngw1500se
This works:

Code: [Select]

/* This code is used to control a 12 volt DC water valve on my RV using a Mosfet.

The purpose is to automaticly turn the water supply off when it is not needed.

A Mosfet is used to switch the 12 volt ground on the valve.

D5 is is used to control the Mosfet as an OUTPUT.

A pushbutton is used to reset the countdown timer.

When pushed, it applies 3.3 volts D1, doing so sets the countdown timer value to 900000.

With each push of the pushbutton, the counter time is reset to 900000 and the countdown starts over again.

Anytime the countdown value is greater than 1, the code sends a HIGH signal out D5 to the Mosfet.

If the countdown value is less than zero, D5 is switched LOW turning the valve off.

NOTE: The value of the countdown timer is set to 900000 milliseconds at boot up, just in case the NodeMCU resets during a shower.

Serial Print is used to test operation.

Feel free to pass this code on to anyone and modify it as needed.

NOTE: I'm using a pull down resister between D1 and ground.

Connect any number of pushbuttons between D1 and any 3.3 volt on the NodeMCU.
*/

int PushButtonState = 0; // variable for reading the pushbutton status
int TimeLeft = 900000; //sets on time to 15 minutes on boot up in case it reboots during shower
int MinutesLeft = 0;

void setup() {
// initialize pins
pinMode(D5, OUTPUT);
pinMode(D1, INPUT);
Serial.begin(9600);
}
void loop() {
PushButtonState = digitalRead(D1); // reads the state of the PushButton value:

if (PushButtonState == HIGH) {
(TimeLeft = 900000); // resets on time to 15 minutes with push of button
}
if (TimeLeft 1) {
digitalWrite(D5, HIGH); // sets D5 HIGH
TimeLeft = TimeLeft - 28; // I played with this # util the time was really close to 15 minutes
delay(10);
MinutesLeft = TimeLeft / 60000; // Calculates minutes based upon milliseconds left
Serial.print("Minutes Until Shut Off: ");
Serial.println( MinutesLeft);
}
}

Juraj

did you see the BlinkWithoutDelay example?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

tngw1500se


Riva

Code: [Select]
(timeleft == 900000);  // sets time to 15 min
You use a single = to assign a value to a variable name and two == to check a value against a variable name.
Your checking if timeleft equals 900000 (and doing nothing with the answer) but I assume you want to set it to 900000.
Don't PM me for help as I will ignore it.

Go Up