Hi,Guys.
I made a auto power on/off project.the main function is ready.but i'd like
to stop the loop when some situation was happened.like,
i want to set the overtime,when buttonState == HIGH or buttonState == LOW over 30seconds then to stop the loop or make the relayState to keeps LOW .etc.
#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; // (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
int relayState;
unsigned long previousMillis = 0; // will store last time LED was updated
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); // Update the actual LED
}
else if((relayState == HIGH) && (currentMillis - previousMillis >= OffTime)){
relayState = LOW; // turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(relayPin, relayState);} // Update the actual LED
}
else if (buttonState == LOW) {
if(currentMillis - previousMillis >= OffTime) {
previousMillis = currentMillis;
if (relayState == HIGH)
relayState = LOW;
else
relayState = HIGH;
digitalWrite(relayPin, relayState);
}
}
}