Hi,Guys.
I made a motherboard autopower on/off testing counter project.please see the testing counter.
Just remain a problem,that is how to keeps the "relayState" in
"LOW" state after "buttonState" is constantly "LOW" or "HIGH" in 30's.
when i unplug the motherboard's dc power,the "buttonState" is constantly "LOW" or when the motherboard is freeze that mean the "buttonState" will constantly in "LOW" or "HIGH" state.
The main fucntion of this project below.
Auto POWER ON/OFF the motherboard and count the ON/OFF times.
#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;
const int buttonPin = 2; // (Connect to motermoard's 5V)
const int relayPin = 13; // (Control Relay,Relay connect to Motherboard's power button)
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
int relayState;
unsigned long previousMillis = 0;
long OnTime = 15000; // milliseconds of on-time
long OffTime = 1500; // milliseconds of off-time
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();
if ( currentMillis - ts >= 5 ) {
display.setDigits( sbuf, 4 );
display.update();
ts += 5; // display-update interval = 5msec
}
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) { // Check the buttonstate (pressed)
// if pressed then buttonState becomes 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 );
}
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 );
}
}
lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
if (buttonState == HIGH){
if((relayState == LOW) && (currentMillis - previousMillis >= OnTime)){
previousMillis = currentMillis; // Remember the time
relayState = HIGH; // Turn it on
digitalWrite(relayPin, relayState); // Turn on the Relay
}
else if((relayState == HIGH) && (currentMillis - previousMillis >= OffTime)){
relayState = LOW; // turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(relayPin, relayState);} // Turn on the Relay
}
else if (buttonState == LOW) {
if(currentMillis - previousMillis >= OffTime) {
previousMillis = currentMillis;
if (relayState == HIGH)
relayState = LOW;// Turn it off
else
relayState = HIGH;// Turn it on
digitalWrite(relayPin, relayState);// Turn on the Relay
}
}
}