/* 4-7-8 breathing guide, for Kitty Yeung's BLE Flower Brooches
* by Alex Glow (based on example code from https://wiki.dfrobot.com/SKU_DFR0748_Kitty_Flower)
* Project link:
*/
#include <Adafruit_NeoPixel.h>
#define VMPIN 5 // Vibration motor pin
#define LED_PIN 9 // The signal pin connected with Arduino
#define LED_COUNT 7 // the amount of the leds of your strip
int mode = 0; // Inhaling, holding, or exhaling
int sec = 0; // Second counter for LED wipe
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(VMPIN, OUTPUT); // Vibration motor on pin 5
leds.begin(); // Call this to start up the LED strip.
clearLEDs(); // This function, defined below, turns all LEDs off...
leds.show(); // ...but the LEDs don't actually update until you call this.
}
void loop() {
// A switch case starting on GREEN (inhale), controlling LEDs + vibe motor.
switch (mode) {
case 1: // hold breath: count 7 with yellow LEDs
colorWipe(leds.Color(250,250,0), 1000, 7); // Yellow
mode = 2;
break;
case 2: // exhale: count down 8 with blue LEDs
setLEDs(leds.Color(0,150,255)); // Blue
colorBack(leds.Color(0,0,0), 1000, 7); // Turn off, one by one
clearLEDs();
leds.show();
// digitalWrite(VMPIN, HIGH);
delay(1000);
// digitalWrite(VMPIN, LOW);
mode = 3;
break;
default: // inhale: count 4 with green LEDs
colorWipe(leds.Color(0,255,100), 1000, 4); // Green
mode = 1;
break;
}
}
// Sets all LEDs to off, but DOES NOT update the display;
// call leds.show() to actually turn them off after this.
void clearLEDs() {
for (int i=0; i<LED_COUNT; i++) {
leds.setPixelColor(i, 0);
}
}
// Prints a rainbow on the ENTIRE LED strip.
// The rainbow begins at a specified position.
// ROY G BIV!
void rainbow(byte startPosition) {
// Need to scale our rainbow. We want a variety of colors, even if there
// are just 10 or so pixels.
int rainbowScale = 192 / LED_COUNT;
// Next we setup each pixel with the right color
for (int i=0; i<LED_COUNT; i++) {
// There are 192 total colors we can get out of the rainbowOrder function.
// It'll return a color between red->orange->green->...->violet for 0-191.
leds.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
}
// Finally, actually turn the LEDs on:
leds.show();
}
// Input a value 0 to 191 to get a color value.
// The colors are a transition red->yellow->green->aqua->blue->fuchsia->red...
// Adapted from Wheel function in the Adafruit_NeoPixel library example sketch
uint32_t rainbowOrder(byte position) {
// 6 total zones of color change:
if (position < 31) // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
{
return leds.Color(0xFF, position * 8, 0);
} else if (position < 63) // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
{
position -= 31;
return leds.Color(0xFF - position * 8, 0xFF, 0);
} else if (position < 95) // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
{
position -= 63;
return leds.Color(0, 0xFF, position * 8);
} else if (position < 127) // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
{
position -= 95;
return leds.Color(0, 0xFF - position * 8, 0xFF);
} else if (position < 159) // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
{
position -= 127;
return leds.Color(position * 8, 0, 0xFF);
} else //160 <position< 191 Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
{
position -= 159;
return leds.Color(0xFF, 0x00, 0xFF - position * 8);
}
}
void colorWipe(uint32_t color, int wait, int sec) {
for(int i=0; i<sec; i++) { // For each pixel in strip...
leds.setPixelColor(i, color); // Set pixel's color (in RAM)
leds.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorBack(uint32_t color, int wait, int sec) {
for(int i=sec; i>0; i--) { // For each pixel in strip...
leds.setPixelColor(i, color); // Set pixel's color (in RAM)
leds.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void setLEDs(uint32_t color) {
for (int i=0; i<LED_COUNT; i++) {
leds.setPixelColor(i, color);
leds.show();
}
}
Comments