Skip to content

Instantly share code, notes, and snippets.

@iamnbutler
Created October 8, 2015 06:28
Show Gist options
  • Save iamnbutler/7c099d36bd405f6bd5a0 to your computer and use it in GitHub Desktop.
Save iamnbutler/7c099d36bd405f6bd5a0 to your computer and use it in GitHub Desktop.
Arduino Timer with Time Selector
// include url: http://www.forward.com.au/pfod/ArduinoProgramming/elapsedMillis.zip
#include <elapsedMillis.h>
// Init Hardware
int led[] = {8,9,10,11,12,13}; // Progress LEDs
int startBtn = 7; // Start btn
int resetBtn = 5; // Reset btn
int flip = 6; // Turn system off/on
int dial = 2; // Dial to set time
// Loop variables
int systemStatus = 0; // 0 = system is off, 1 = system is on
int runState = 0;
int resetState = 0;
int dialValue = 0;
int counterTime = 0; // Set up counter
int running = 0; // Is the timer running?
elapsedMillis timer0;
#define interval 60000
void setup() {
pinMode(led[0], OUTPUT); // Define LED outputs
pinMode(led[1], OUTPUT);
pinMode(led[2], OUTPUT);
pinMode(led[3], OUTPUT);
pinMode(led[4], OUTPUT);
pinMode(led[5], OUTPUT); // Status indicator light
timer0 = 0; // clear the timer at the end of startup
Serial.begin(9600);
}
void loop() {
systemStatus = digitalRead(flip);
if (systemStatus == HIGH) {
// Start the system
digitalWrite(led[5], HIGH); // Yellow system indicator LED
if (running == 0) {
// Only show dial's time estimate when timer isnt already running
dialValue = analogRead(dial);
counterTime = map(dialValue, 0, 1023, 0, 4);
} else {
// Timer
if (timer0 > interval) {
timer0 -= interval;
counterTime = counterTime - 1;
}
// Make sure counter doesn't go below 0
if (counterTime < 0) {
counterTime = 0;
}
}
// Light LEDs based on counterTime
if (counterTime > 3) {
digitalWrite(led[0], HIGH);
digitalWrite(led[1], HIGH);
digitalWrite(led[2], HIGH);
digitalWrite(led[3], HIGH);
digitalWrite(led[4], LOW);
} else if (counterTime <= 3 && counterTime > 2) {
digitalWrite(led[0], LOW);
digitalWrite(led[1], HIGH);
digitalWrite(led[2], HIGH);
digitalWrite(led[3], HIGH);
digitalWrite(led[4], LOW);
} else if (counterTime <= 2 && counterTime > 1) {
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], HIGH);
digitalWrite(led[3], HIGH);
digitalWrite(led[4], LOW);
} else if (counterTime <= 1 && counterTime > 0.1) {
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], HIGH);
digitalWrite(led[4], LOW);
} else if (counterTime <= 0.1) {
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
digitalWrite(led[4], HIGH);
}
// Start / Reset button
runState = digitalRead(startBtn); // read the state of the pushbutton value:
resetState = digitalRead(resetBtn); // read the state of the pushbutton value:
if (resetState == HIGH) {
running = 0;
// turn LEDs on:
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
digitalWrite(led[4], HIGH);
delay(200);
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
digitalWrite(led[4], LOW);
delay(400);
}
if (runState == HIGH) {
timer0 = 0; // clear the timer on button press
running = 1;
// turn LEDs on:
digitalWrite(led[0], HIGH);
digitalWrite(led[1], HIGH);
digitalWrite(led[2], HIGH);
digitalWrite(led[3], HIGH);
digitalWrite(led[4], LOW);
delay(200);
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
digitalWrite(led[4], LOW);
delay(400);
} else {
// turn LEDs off:
digitalWrite(led[0], LOW);
digitalWrite(led[1], LOW);
digitalWrite(led[2], LOW);
digitalWrite(led[3], LOW);
digitalWrite(led[4], LOW);
}
} else {
// System is off, reset variables and do nothing
running = 0;
digitalWrite(led[5], LOW); // Yellow system indicator LED
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment