Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample code is for Contact-Less Hand Washing LED Ring Timer Using Maker UNO tutorial.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define LED_RING 5
#define SENSOR 6
#define PIEZO 8
#define NUMPIXELS 12 // NeoPixel ring size
Adafruit_NeoPixel pixels(NUMPIXELS, LED_RING, NEO_GRB + NEO_KHZ800);
#define NOTE_B5 988
int startNoteDurations[] = {8,4,4};
int stopMelody[] = {NOTE_B5};
int stopNoteDurations[] = {8};
#define playstopMelody() playMelody(stopMelody, stopNoteDurations, 2)
char inChar;
String inString;
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
pinMode(SENSOR, INPUT);
pinMode(PIEZO, OUTPUT);
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
Serial.begin(9600);
}
void loop() {
pixels.clear(); // Set all pixel colors to 'off'
pixels.show(); // Send the updated pixel colors to the hardware.
if (digitalRead(SENSOR) == HIGH) {
for (int i = NUMPIXELS; i >= 0; i--) {
pixels.setPixelColor(i, pixels.Color(100, 5, 5));
pixels.show();
delay(250); // Pause before next pass through loop
}
for (int i = 0; i < NUMPIXELS; i++) {
Serial.print("i = ");
Serial.println(i);
pixels.setPixelColor(i, pixels.Color(10, 100, 10));
pixels.show();
delay(1700);
}
blinking(3);
}
else {
pixels.clear();
pixels.show();
}
}
void blinking(int times)
{
for (int i = 0; i < times; i++) {
pixels.clear();
pixels.show();
delay(200);
for (int i = 0; i < 12; i++) {
pixels.setPixelColor(i, pixels.Color(5, 100, 5));
pixels.show();
}
delay(200);
playstopMelody();
}
}
void playMelody(int *melody, int *noteDurations, int notesLength)
{
for (int thisNote = 0; thisNote < notesLength; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment