Here's what I am trying to achieve:
Until Button1 is pressed, the LED display reads "----"
Button1 - starts timer shown on LED display (30 minutes) - timer starts counting down.
Button2 - freezes timer
Button3 - starts timer counting down from where it was when button2 was pressed
LED1 - lights up while timer is counting down, stays on while frozen.
LED2 - lights up while timer is frozen, goes out when timer is resumed counting down.
When timer is started PIN7 is written HIGH
When timer reads 20 min, PIN 8 is written HIGH
at 10min left, PIN9 -> HIGH
at 5min left, PIN10 -> HIGH
at 1min left, PIN11 -> HIGH
Timer runs out completely , PIN12 -> HIGH
After time has run out, button1 can be pressed to restart the whole process.
What's working:
- With the code below, the display shows "----" until Button1 is pressed.
- Button1 successfully starts time at 30 minutes and lights up LED1.
- Button2 successfully freezes the time and lights up LED2.
I cannot get button3 to start the time up again, and I have no idea how to mix in the timed digitalWrites for pins 7 - 12 shown above or how to get my Button1 to restart the whole process. I don't have a very good understanding of the whole "loop" function and how to pause it, restart it etc. I feel that my code I've written does it in a very roundabout incorrect way, with variable "timerRunning" and "pausePushed" being set to 1 from 0.
I have connected the Adafruit 1.2" 4-Digit 7-Segment Display w/I2C Backpack to the correct pins as instructed here and its working perfectly.
Using the following libraries (as you can see in the code):
- Time.h
- Wire.h
- Adafruit_LEDBackpack.h
- Adafruit_GFX.h"
Can someone help me edit this code to function how I've described above and in the most clean and streamlined way? At this point, I'm not getting any errors, but I have hit a wall as far as how to complete the rest of my goals with my current coding knowledge.
Thanks in advance!
-Kayden
#include <Time.h>
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_7segment matrix = Adafruit_7segment();
time_t t;
tmElements_t tm;
int seconds, minutes;
int startButton = 3;
int inProgLED = 4;
int pausedLED = 5;
int pauseButton = 6;
int timerRunning = 0;
int pausePushed = 0;
void setup(void)
{
Serial.begin(9600); //begin Serial
pinMode(startButton, INPUT);
pinMode(pauseButton, INPUT);
pinMode(inProgLED, OUTPUT);
pinMode(pausedLED, OUTPUT);
digitalWrite(startButton, HIGH);
digitalWrite(pauseButton, HIGH);
matrix.begin(0x70);
tm.Second = 0;
tm.Minute = 30;
tm.Day = 24;
tm.Month = 4;
tm.Year = CalendarYrToTm(2015);
t = makeTime(tm);
}
void loop(void)
{
matrix.print(10000, DEC);
matrix.writeDisplay();
if (digitalRead(startButton) == LOW) {
timerRunning = 1;
}
if (digitalRead(pauseButton) == LOW) {
pausePushed= 1;
}
Serial.println(pauseButton);
Serial.println(startButton);
if(timerRunning == 1){
digitalWrite(inProgLED, HIGH);
seconds = second(t);
minutes = minute(t);
matrix.writeDigitNum(0, (minutes / 10) % 10, false);
matrix.writeDigitNum(1, (minutes % 10), false);
matrix.drawColon(true);
matrix.writeDigitNum(3, (seconds / 10) % 10, false);
matrix.writeDigitNum(4, seconds % 10, false);
matrix.writeDisplay();
--t; //subtract a second
if (seconds + minutes == 0) {
//digitalWrite(pin4, HIGH);
}
if(pausePushed == 1){
digitalWrite(pausedLED, HIGH);
while(1);
}
delay(1000);
}
}