hi guys.I want to make a project which can power on and off by itself.
Relaypin connect to powerbutton.
buttonPin connect to MB's usb power.
when i short the powerbutton,the buttonState will be pull HIGH,then after 15seconds
the Relaystate also be pull HIGH(ps.short the powerbutton makes the MB power off) at the same time the buttonState also be pull LOW.my problem is when buttonState == LOW,how to pull high the Relaystate for 1second then Relaystate pull LOW???
delay() is ok,but have to use millis().
if (buttonState == HIGH) {
delay(15000);//(power on duration)
digitalWrite(relayPin, HIGH); //(power off the MB)
delay(1000);
digitalWrite(relayPin, LOW); //(power off duration)
}
if (buttonState == LOW) {
delay(3000);
digitalWrite(relayPin, HIGH);//(power on the MB)
delay(1000);
digitalWrite(relayPin, LOW); (power on duration)
}
#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 = 15000; // milliseconds of on-time
long OffTime1 = 1000; // milliseconds of on-time
long OnTime2 = 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() {
unsigned long currentMillis = millis();
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) && (currentMillis - previousMillis >= OnTime1){
buttonState = HIGH; // Turn it off
digitalWrite(relayPin, buttonState); // Update the actual LED
}
else if (buttonState == LOW) && (currentMillis - previousMillis >= OnTime2) {
buttonState = LOW; // Turn it off
digitalWrite(relayPin, buttonState); // Update the actual LED
}