Hi I have read through the guide thread which explains difference between the two and I understand delay()holds everything up so millis() needs to be used when there are several things happnening at the same time. But the codes below doesn't require several things at the same time. i.e when push button is pushed, fan gets switched on. Why is mills() being used instead of delay(50)?
int buttonPin=2;
int relayPin=3;
int relayState=HIGH;
int buttonState;
int lastButtonState=LOW;
long lastDebounceTime=0;
long debounceDelay = 50;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, relayState);
}
void loop() {
// put your main code here, to run repeatedly:
int reading=digitalRead(buttonPin);
if(reading!=lastButtonState){
lastDebounceTime=millis();
}
if((millis()-lastDebounceTime)>debounceDelay){
if(reading!=buttonState){
buttonState=reading;
if(buttonState==HIGH){
relayState=!relayState;
}
}
}
digitalWrite(relayPin,relayState);
lastButtonState=reading;
}