Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Turn ON/OFF a Relay/LED, when button is pushed
- // Relay Automatically turns OFF/ON again after 5 seconds (it is possible to adjust interval)
- // With option to reset the timer - so Relay turns ON/OFF at push of 'reset timer button'
- const int SetPin = 6; //pin connected to ON button - (other side of button is connected to ground/minus)
- const int ResetPin = 7; //pin connected to 'Reset timer button' - (other side of button is connected to ground/minus)
- const int relayPin = 9; //pin connected to control of Relay/LED
- //makes it easy to switch between relays that are controlled by HIGH or LOW signal
- const int RELAY_ON = HIGH;
- const int RELAY_OFF = LOW;
- int buttonState1 = 0;
- int buttonState2 = 0;
- unsigned long timerMillis = 0; // is set when timer is started
- unsigned long interval = 5000; // interval of the timer - (time that the LED/relay is ON)
- // (1000 = 1 second), (60000 = 1 minute), (600000 = 10 minutes)
- int ledStatus = 0; // is used to keep track of relay/LED status
- void setup() {
- pinMode(relayPin, OUTPUT);
- pinMode(SetPin, INPUT_PULLUP);
- pinMode(ResetPin, INPUT_PULLUP);
- digitalWrite(relayPin, RELAY_OFF);
- } // End - setup()
- void loop() {
- // read buttons
- buttonState1 = digitalRead(SetPin);
- buttonState2 = digitalRead(ResetPin);
- // take action if a button was pressed
- if (buttonState1 == LOW) {
- digitalWrite(relayPin, RELAY_ON);
- ledStatus = 1;
- }
- if (buttonState2 == LOW) {
- digitalWrite(relayPin, RELAY_OFF);
- ledStatus = 0;
- }
- // if relay is ON, then check to see if timer has expired
- if (ledStatus == 1) {
- if (millis() - timerMillis >= interval) {
- timerMillis = millis();
- digitalWrite(relayPin, RELAY_OFF);
- ledStatus = 0;
- }
- }
- } // End - loop()
Add Comment
Please, Sign In to add comment