Hi,Guys.
I'd like to make a project of the motherboard auto power on/off,but
if i use millis() to delay activating the relayState.it has no any delay?
why?
i just like to have the same result as below.
if (buttonState == HIGH) {
delay(8000); //delay 8s after detecting crt_signal,then pull high the relaypin
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
}
if (buttonState == LOW) {
delay(1000); //delay 1s after detecting crt_signal,then pull high the relaypin
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
}
#include <Four7Seg74hc595.h>
/*
* PRESS BUTTON COUNTER INCREMENT
*/
esl::Four7Seg74hc595 display( 10,9,8 );
// Pin connected to Pin 14 of 74HC595 (Data=DIO,arduino pin8)
// Pin connected to Pin 12 of 74HC595 (Latch=RCLK,arduino pin9)
// Pin connected to Pin 11 of 74HC595 (Clock=SCLK,arduino pin10)
char sbuf[5];
uint32_t ts,ts1,ts2;
const int buttonPin = 2; // (pushbutton)
const int relayPin = 13; // (Relay)
int buttonPushCounter = 0; //counter for the number of button presses
int buttonState = 0 ; //current state of the button
int lastButtonState = 0; //previous state of the button
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime1 = 1000; // milliseconds of on-time
long OnTime2 = 15000; // milliseconds of on-time
long OffTime1 = 1000; // milliseconds of on-time
long OffTime2 = 1000; // milliseconds of on-time
int relayState = LOW;
void setup() {
Serial.begin(9600); // Serial port, 9600 bps
pinMode(buttonPin, INPUT); // buttonPin setup to INPUT
pinMode(relayPin, OUTPUT); // relayPin setup to OUTPUT
for (uint8_t i=0; i < 100; i++) {
display.setDigits( "----", 4 );
display.update();
delay(10);
}
delay(1000);
buttonPushCounter = 0;
sprintf( sbuf, "%04u", buttonPushCounter );
display.setDigits( sbuf, 4 );
display.update();
}
void loop() {
ts = millis();
if ( ts - ts2 >= 5 ) {
display.setDigits( sbuf, 4 );
display.update();
ts2 += 5; // display-update interval = 5msec
}
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) { // Check the buttonstate (pressed)
// if pressed then buttonState become to HIGH
if (buttonState == HIGH){
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
Serial.print("number of button pushesON: ");
Serial.println(buttonPushCounter,DEC);
sprintf( sbuf, "%04u", buttonPushCounter );
if (currentMillis - previousMillis >= OnTime1) {
previousMillis = currentMillis; // Remember the time
relayState = LOW; // Turn it off
digitalWrite(relayPin, relayState); // Update the actual LED
}
}
else {
if (currentMillis - previousMillis >= OnTime2) {
relayState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(relayPin, relayState); // Update the actual LED
}
}
}
else if (buttonState == LOW) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
Serial.print("number of button pushesOFF: ");
Serial.println(buttonPushCounter,DEC);
sprintf( sbuf, "%04u", buttonPushCounter );
if (currentMillis - previousMillis >= OffTime1) {
previousMillis = currentMillis; // Remember the time
relayState = LOW; // Turn it off
digitalWrite(relayPin, relayState); // Update the actual LED
}
else {
if (currentMillis - previousMillis >= OffTime2) {
relayState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(relayPin, relayState); // Update the actual LED
}
}
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
}