Devices & Components
Arduino Nano
3 mm LED: Green
1N4148 – General Purpose Fast Switching
Tactile Switch, Top Actuated
3 mm LED: Red
5V Relay
General Purpose Transistor PNP
3 mm LED: Yellow
Project description
Code
4 Duration Timer
arduino
1// Paul Brace 2// 30 minutes to 2 hour timer for running an external device 3// Jan 2021 4 5#define TIME_SELECT_BUTTON 2 6#define START_BUTTON 3 7#define STOP_BUTTON 4 8#define TIME_30_LIGHT 6 //Yellow 9#define TIME_60_LIGHT 7 //Yellow 10#define TIME_90_LIGHT 8 //Yellow 11#define TIME_120_LIGHT 9 //Yellow 12#define ACTIVE_LIGHT 10 //Green 13#define ACTIVE_PIN 11 14 15bool timerActive; 16unsigned long timerLength; 17unsigned long startTime; 18unsigned long duration; 19 20 21 22void setup() { 23 pinMode(START_BUTTON, INPUT_PULLUP); 24 pinMode(STOP_BUTTON, INPUT_PULLUP); 25 pinMode(TIME_SELECT_BUTTON, INPUT_PULLUP); 26 pinMode(ACTIVE_LIGHT, OUTPUT); 27 pinMode(ACTIVE_PIN, OUTPUT); 28 pinMode(TIME_30_LIGHT, OUTPUT); 29 pinMode(TIME_60_LIGHT, OUTPUT); 30 pinMode(TIME_90_LIGHT, OUTPUT); 31 pinMode(TIME_120_LIGHT, OUTPUT); 32 digitalWrite(ACTIVE_LIGHT, LOW); 33 digitalWrite(TIME_30_LIGHT, HIGH); 34 digitalWrite(TIME_60_LIGHT, LOW); 35 digitalWrite(TIME_90_LIGHT, LOW); 36 digitalWrite(TIME_120_LIGHT, LOW); 37 digitalWrite(ACTIVE_PIN, LOW); 38 39 timerLength = 30; // Default to 30 minutes 40 timerActive = false; // Timer in inactive state 41} 42 43void WaitForStart(){ 44 // Called when timer inactive and will loop until 45 // the start button is pressed. 46 // Also checks to see if the select time button has been pressed and 47 // changes the duration if it has. 48 while (digitalRead(START_BUTTON) == HIGH){ 49 // Check if the timer select button has been pressed 50 // if so cycle through times for each press 51 if (digitalRead(TIME_SELECT_BUTTON) == LOW){ 52 switch(timerLength){ 53 case 30: 54 timerLength = 60; 55 digitalWrite(TIME_30_LIGHT, LOW); 56 digitalWrite(TIME_60_LIGHT, HIGH); 57 break; 58 case 60: 59 timerLength = 90; 60 digitalWrite(TIME_60_LIGHT, LOW); 61 digitalWrite(TIME_90_LIGHT, HIGH); 62 break; 63 case 90: 64 timerLength = 120; 65 digitalWrite(TIME_90_LIGHT, LOW); 66 digitalWrite(TIME_120_LIGHT, HIGH); 67 break; 68 case 120: 69 timerLength = 30; 70 digitalWrite(TIME_120_LIGHT, LOW); 71 digitalWrite(TIME_30_LIGHT, HIGH); 72 break; 73 } 74 // Delay to cater for button bounce and repeat if held down 75 delay(500); 76 } 77 } 78 // Start button has been pressed 79 // Timer now active 80 timerActive = true; 81 // Record the start time in milliseconds 82 startTime = millis(); 83 // Calculate duration to activate timer for in milliseconds 84 // Calculation will be using unsigned long aritmatic 85 // as first argument is an unsigned long 86 duration = timerLength * 60 * 1000; 87 // Turn on active light and activate relay 88 digitalWrite(ACTIVE_PIN, HIGH); 89 digitalWrite(ACTIVE_LIGHT, HIGH); 90} 91 92void loop() { 93 // If timer is not active then wait for the start button 94 if (!timerActive){ 95 WaitForStart(); 96 } 97 // The following code will only be reached when the timer is active 98 // Check every second if the selected time has elapsed or stop button pressed 99 while (timerActive){ 100 if ((digitalRead(STOP_BUTTON) == LOW) || 101 ((millis() - startTime) >= duration)){ 102 // Duration reached or stop button pressed so 103 // set to not active, turn off active light and relay 104 timerActive = false; 105 digitalWrite(ACTIVE_PIN, LOW); 106 digitalWrite(ACTIVE_LIGHT, LOW); 107 } 108 // Pause for 1 second 109 delay(1000); 110 } 111}
Downloadable files
Finished project
Finished project

4 Duration Timer Circuit
4 Duration Timer Circuit

4 Duration Timer Schema
4 Duration Timer Schema

4 Duration Timer Circuit
4 Duration Timer Circuit

Finished project
Finished project

4 Duration Timer Schema
4 Duration Timer Schema

Comments
Only logged in users can leave comments