I have this project where a pushbutton pressed for over 7 seconds would open my NC relay.
I designed this with arduino UNO and it worked well.
As I’m trying to pass the project on wemos D1 mini, I’m encountering many issues, especially a soft reset as soon as the pushbutton reaches the trigger state for relay control.
This is my code:
#include <SimpleTimer.h>
#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
int door = D5; // the input to the relay pin
const int buttonPin = D2; // The number of the Pushbutton pin
// Variables will change:
int buttonState = 0; // Variable for reading the pushbutton status
long previousMillis = 0; // will store last time LED was updated
long interval = 7000; // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;
void setup() {
// Debug console
Serial.begin(74880);
pinMode(door, OUTPUT); // initialize pin as OUTPUT
pinMode(buttonPin, INPUT); // initialize pin as OUTPUT
digitalWrite(door, LOW);
}
void loop() {
//REDBUTTON EMERGENCY
previousMillis = millis();
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
//The button is pushed
while (buttonState == HIGH) {
Serial.println(buttonState);
currentValue = millis() - previousMillis;
if (currentValue > interval) //If the button has been pressed for over 7 secs, open it
{
// save the last time relays has been turned on
previousMillis = millis();
digitalWrite(door, HIGH); //opendoor
delay(4000); //give time to get in
digitalWrite(door, LOW); //close it
}
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
}
}