Here the rotary encoder is set up using the new built-in support for the rotation of the switch and I use my debouncer library to clean up the encoder push switch. Finally the NeoPixel strip is configured. If you are unfamiliar with NeoPixels, there is a great guide on them.

Each time through the loop, it checks for a push on the encoder switch. That's the job of the debouncer's update function. If one was detected (i.e. the button signal went from high to low... it fell) the code enters the mode to set the length of the work phase, followed by the break phase, then back to timing mode.

The next step checks to see if it's time to update the timer. It does this every second (as defined by the last line). So once a second, the time remaining is decreased by 1 (second) and the ring is updated. If it reaches 0, it's time to make some noise and change mode.

In addition to the setup and loop there are a handful of helper functions.

The check_encoder function tracks the position of the encoder, comparing it to the last known position. Based on that comparison it returns -1, 0, or 1 to indicate that the encoder rotated counter-clockwise, didn't move, or rotated clockwise.

The show_time function updates the time displayed on the NeoPixel ring. It sets pixels to the specified color starting at pixel zero (which is at the bottom of the ring in the final build) and moving clockwise. The final pixel is handled differently. Depending on the bright parameter, it's either set to the same color as the rest, or white. In the main loop we saw that this value gets toggled each second. The result is that the highest pixel blinks.

Note that for the work and break times, each pixel is worth a different number of seconds. This is because it's typical for the work phase to be a half hour, an hour, or longer, while the break phase is usually between five and fifteen minutes. To give a better, brighter display, I decided to use a different scale for each. This is all adjustable in the compute_mode_settings function:

This is straightforward. It sets up the pulse-width modulation (PWM), loops for the number of beeps requested, and shuts down the PWM. For each beep it sets the duty cycle to 50%, waits for the beep duration, sets the duty cycle to 0% (effectively turning of the sound), then waits for the interstitial duration before playing the next beep (if any).

To start, it initializes from the current encoder position and flashes the ring. Following that there's a loop that shows the current setting (using the show_time function we looked at above) and checks the encoder push switch. If it was pressed the current setting is returned. Otherwise the encoder's rotation is checked and the time setting changed based on the result. Finally the time setting is capped at 0 and 16, which reflects the size of the ring.

That's it. Each piece is fairly simple, but the overall functionality is interesting and useful. Below is all of it, with comments.

OUT OF STOCK NOTIFICATION

YOUR NAME

YOUR EMAIL

You have been successfully subscribed to the Notification List for this product and will therefore receive an e-mail from us when it is back in stock!

For security reasons, an e-mail has been sent to you acknowledging your subscription. Please remember that this subscription will not result in you receiving any e-mail from us about anything other than the restocking of this item.

If, for any reason, you would like to unsubscribe from the Notification List for this product you will find details of how to do so in the e-mail that has just been sent to you!