Creating a Pomodoro timer that works using the delay function from the library was much more difficult than I would have thought it was going to be. The standard delay function blocks input from other processes while it is running and causes it to be difficult to use for this purpose – timing a significant chunk of work means long delay times and thus long times during which no other operations can be achieved until the delay completes. The start button works correctly, but the stop button is blocked by the delay function and prevented from interrupting the sequence (I’m guessing).
The Milli function is much better suited to this project and I am working on getting a better handle on how that is implemented to create a timing calculation that doesn’t monopolize all of the limited resources and allows more options for other operations tied to the timing function.
The addition of the piezo also didn’t go as planned. I was trying to get it to trigger during the 15th LED illumination and play a tone tha let the user know that a break was coming up. I attempted to read the counter with an if statement and play a tone from an array of tones if i=>15 but was unable to get that to work properly.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
const byte ledPin=6;
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, ledPin, NEO_GRB + NEO_KHZ800);
int delayval = 18000; // led delay
int inPin = 3; // start button
int dnPin = 4; // stop button
int sndPin = 5; // piezo
int val = 0; // variable for reading the start pin status
int offState = 0; // variable for off button status
int notes[] = {262, 294, 330, 349}; // piezo tones
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT);
pinMode(sndPin, OUTPUT);
pinMode(dnPin, INPUT);
}
void loop()
{
val = digitalRead(inPin); // read input value
if (val == HIGH)
{ // (button pressed)
SetPixels();
digitalWrite(ledPin, LOW);
}
offState = digitalRead(dnPin);
if (offState == HIGH) {
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, HIGH);
}
}
void SetPixels()
{
for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(20, 150, 100));
pixels.show(); // updated pixel color
delay(delayval);
Serial.println(i);
if (i >= 15)
{
tone(5, notes[0]);
}
}
}
References that were most helpful:
Pomodoro timer using Millis – best bet: The Pomoduino: Make an Arduino-Powered Pomodoro Timer (makeuseof.com)
Individual LEDs with delay based timer:
https://create.arduino.cc/projecthub/355077/pomodoro-timer-using-arduino-1b353a
NeoPixel References:
https://learn.adafruit.com/adafruit-neopixel-uberguide/basic-connections
Adafruit solutions:
https://learn.adafruit.com/circuit-playground-hourglass/less-basic-timer
https://learn.adafruit.com/game-clock-with-circuit-playground-makecode/code-explanation