I've attached a copy of my final code.
I opted to copy some code I used a couple of years back on a digispark, where I counted loops and delayed it 1000ms in order to save power.
Gave myself a much bigger target to hit that way.
I had some issues with indenting and parenthesis but that's always been something I struggle with in indented code like python.
That and modifying example code to get where I am.
I commented out the rollover code, as it's functionally pointless to the end use, and was causing compile issues.
Adding numbers helped me to correlate to real world pin numbers, to save mistakes when assembling.
//Kitchen timer code.
//running on arduino uno with attached 14 segment (4 digit) display (I2C)
//pin 8 currently attached to led, but in future will be a piezo beeper.
//pins 2-5 are toggle switches with common resistor and pull to ground
//the concept is that the timer will beep for a couple of seconds at intervals
//determined by the selected toggle switches.
#include <Wire.h>
#include <Adafruit_LEDBackpack.h>
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4(); // Use Adafruit_AlphaNum4 for 14-segment display
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
//below times will be altered to suit my needs when code is functiona;
const unsigned long targetTimeP2 = 900; // 900 time in seconds on pin2
const unsigned long targetTimeP3 = 1800; // 1800 time in seconds on pin3
const unsigned long targetTimeP4 = 2700; // 2700 time in seconds on pin4
const unsigned long targetTimeP5 = 3600; // 3600 time in seconds on pin5
const unsigned int maxMinutes = 99; // Maximum value for minutes
const int buttonPin2 = 2; // Pin 2 switch
const int buttonPin3 = 3; // Pin 3 switch
const int buttonPin4 = 4; // Pin 4 switch
const int buttonPin5 = 5; // Pin 5 switch
unsigned long loopCount; //workaround for problem where code won't trigger on individual miliseconds - also power saving as reduces processing
void setup() {
pinMode(8, OUTPUT); // Set pin 8 as output
alpha4.begin(0x70); // Address of HT16K33 module
alpha4.setBrightness(15); // Adjust the brightness (0-15)
alpha4.writeDigitAscii(0, '0'); // Initial display time: 00:00
alpha4.writeDigitAscii(1, '0');
alpha4.writeDigitAscii(2, '0');
alpha4.writeDigitAscii(3, '0');
alpha4.writeDisplay();
pinMode(buttonPin2, INPUT_PULLUP); // Set switch pin as input with internal pull-up resistor
pinMode(buttonPin3, INPUT_PULLUP); // Set switch pin as input with internal pull-up resistor
pinMode(buttonPin4, INPUT_PULLUP); // Set switch pin as input with internal pull-up resistor
pinMode(buttonPin5, INPUT_PULLUP); // Set switch pin as input with internal pull-up resistor
loopCount == 1; //zero out the loop counter (this should represent seconds roughly - for a kitchen timer, doesn't need to be super accurate)
}
void loop() {
//workaround to arduino missing individual miliseconds:
delay(1000);
loopCount += 1;
// Check if the timer has not started yet
if (startTime == 0) {
if (millis() >= 1000) { // Start the timer after 5 seconds
startTime = millis();
}
} else {
// Calculate the elapsed time
//elapsedTime = millis() - startTime;
// Check if the target time has been reached on pins 2-5
if (loopCount == targetTimeP2) {
if (digitalRead(buttonPin2) != LOW) {
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(500);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH);
delay(3000);
digitalWrite(8, LOW);
}
}
if (loopCount == targetTimeP3) {
if (digitalRead(buttonPin3) != LOW) {
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(500);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH);
delay(1500);
digitalWrite(8, LOW);
delay(500);
digitalWrite(8, HIGH);
delay(1500);
digitalWrite(8, LOW);
}
}
if (loopCount == targetTimeP4) {
if (digitalRead(buttonPin5) != LOW) {
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(500);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(500);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
delay(500);
digitalWrite(8, HIGH);
delay(1000);
digitalWrite(8, LOW);
}
}
if (loopCount == targetTimeP5) {
if (digitalRead(buttonPin5) != LOW) {
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(500);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH); // Set pin 8 high when time reached is switch is on
delay(50);
digitalWrite(8, LOW); // Set pin 8 low to turn off
delay(50);
digitalWrite(8, HIGH);
delay(750);
digitalWrite(8, LOW);
delay(400);
digitalWrite(8, HIGH);
delay(750);
digitalWrite(8, LOW);
delay(400);
digitalWrite(8, HIGH);
delay(750);
digitalWrite(8, LOW);
delay(400);
digitalWrite(8, HIGH);
delay(750);
digitalWrite(8, LOW);
}
}
}
// Calculate minutes and seconds
unsigned int minutes = loopCount / 60;
unsigned int seconds = (loopCount) % 60;
// Handle rollover if minutes exceed the maximum value
//if (minutes > maxMinutes) {
// minutes = minutes % (maxMinutes + 1);
// startTime = loopCount(); // Reset the start time to prevent overflow
//}
// Update the display with the elapsed time
int minuteTens = minutes / 10;
int minuteOnes = minutes % 10;
int secondTens = seconds / 10;
int secondOnes = seconds % 10;
// Display the time on the LED matrix
alpha4.writeDigitAscii(0, minuteTens + '0');
alpha4.writeDigitAscii(1, minuteOnes + '0');
alpha4.writeDigitAscii(2, secondTens + '0');
alpha4.writeDigitAscii(3, secondOnes + '0');
alpha4.writeDisplay();
}