Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
paulsb
Published © LGPL

4 Duration Control Timer

This is a control timer to turn on a connected device for a preset length of time. Set in program for 30, 60, 90 or 120 minutes.

BeginnerFull instructions provided2,609
4 Duration Control Timer

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
Or you could use a UNO
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×3
3 mm LED: Green
3 mm LED: Green
×1
3 mm LED: Red
3 mm LED: Red
×1
3 mm LED: Yellow
3 mm LED: Yellow
×4
General Purpose Transistor PNP
General Purpose Transistor PNP
I used a 2N2222
×1
1N4148 – General Purpose Fast Switching
1N4148 – General Purpose Fast Switching
×1
5V Relay
I used a RM85-2011-35-1005
×1

Story

Read more

Schematics

4 Duration Timer Schema

4 Duration Timer Circuit

Finished project

Code

4 Duration Timer

Arduino
// Paul Brace
// 30 minutes to 2 hour timer for running an external device
// Jan 2021

#define TIME_SELECT_BUTTON 2
#define START_BUTTON 3
#define STOP_BUTTON 4
#define TIME_30_LIGHT 6   //Yellow  
#define TIME_60_LIGHT 7   //Yellow
#define TIME_90_LIGHT 8   //Yellow
#define TIME_120_LIGHT 9  //Yellow
#define ACTIVE_LIGHT 10   //Green
#define ACTIVE_PIN 11

bool timerActive;
unsigned long timerLength;
unsigned long startTime;
unsigned long duration;



void setup() {
  pinMode(START_BUTTON, INPUT_PULLUP);
  pinMode(STOP_BUTTON, INPUT_PULLUP);
  pinMode(TIME_SELECT_BUTTON, INPUT_PULLUP);
  pinMode(ACTIVE_LIGHT, OUTPUT);
  pinMode(ACTIVE_PIN, OUTPUT);
  pinMode(TIME_30_LIGHT, OUTPUT);
  pinMode(TIME_60_LIGHT, OUTPUT);
  pinMode(TIME_90_LIGHT, OUTPUT);
  pinMode(TIME_120_LIGHT, OUTPUT);
  digitalWrite(ACTIVE_LIGHT, LOW);
  digitalWrite(TIME_30_LIGHT, HIGH);
  digitalWrite(TIME_60_LIGHT, LOW);
  digitalWrite(TIME_90_LIGHT, LOW);
  digitalWrite(TIME_120_LIGHT, LOW);
  digitalWrite(ACTIVE_PIN, LOW);

  timerLength = 30;     // Default to 30 minutes
  timerActive = false;  // Timer in inactive state
}

void WaitForStart(){
  // Called when timer inactive and will loop until 
  // the start button is pressed.
  // Also checks to see if the select time button has been pressed and
  // changes the duration if it has.
  while (digitalRead(START_BUTTON) == HIGH){
    // Check if the timer select button has been pressed
    // if so cycle through times for each press
    if (digitalRead(TIME_SELECT_BUTTON) == LOW){
      switch(timerLength){
        case 30:
          timerLength = 60;
          digitalWrite(TIME_30_LIGHT, LOW);
          digitalWrite(TIME_60_LIGHT, HIGH);
          break;
        case 60:
          timerLength = 90;
          digitalWrite(TIME_60_LIGHT, LOW);
          digitalWrite(TIME_90_LIGHT, HIGH);
          break;
        case 90:
          timerLength = 120;
          digitalWrite(TIME_90_LIGHT, LOW);
          digitalWrite(TIME_120_LIGHT, HIGH);
          break;
        case 120:
          timerLength = 30;
          digitalWrite(TIME_120_LIGHT, LOW);
          digitalWrite(TIME_30_LIGHT, HIGH);
          break;
      }
      // Delay to cater for button bounce and repeat if held down
      delay(500);
    }
  }
  // Start button has been pressed
  // Timer now active
  timerActive = true;       
  // Record the start time in milliseconds
  startTime = millis();     
  // Calculate duration to activate timer for in milliseconds
  // Calculation will be using unsigned long aritmatic 
  // as first argument is an unsigned long
  duration = timerLength * 60 * 1000;  
  // Turn on active light and activate relay
  digitalWrite(ACTIVE_PIN, HIGH);
  digitalWrite(ACTIVE_LIGHT, HIGH);
}

void loop() {
  // If timer is not active then wait for the start button
  if (!timerActive){
    WaitForStart();
  }
  // The following code will only be reached when the timer is active
  // Check every second if the selected time has elapsed or stop button pressed
  while (timerActive){
    if ((digitalRead(STOP_BUTTON) == LOW) ||
       ((millis() - startTime) >= duration)){
        // Duration reached or stop button pressed so
        // set to not active, turn off active light and relay
        timerActive = false;
        digitalWrite(ACTIVE_PIN, LOW);
        digitalWrite(ACTIVE_LIGHT, LOW);
     }
     // Pause for 1 second
     delay(1000); 
  }
}

Credits

paulsb
4 projects • 28 followers

Comments