#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN1 27
#define LED_PIN2 12
#define LED_PIN3 14
#define LED_COUNT 8
#define BRIGHTNESS 250
#define MODES 4
volatile byte mode = 3;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip1(LED_COUNT, LED_PIN1, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel strip2(LED_COUNT, LED_PIN2, NEO_RGB + NEO_KHZ800);
Adafruit_NeoPixel strip3(LED_COUNT, LED_PIN3, NEO_RGB + NEO_KHZ800);
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
strip1.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip1.show(); // Turn OFF all pixels ASAP
strip1.setBrightness(BRIGHTNESS);
attachInterrupt(digitalPinToInterrupt(15), onButton, CHANGE);
}
void onButton() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 2000)
{
BRIGHTNESS - 50;
}
else if (interrupt_time - last_interrupt_time > 200)
{
mode = (mode + 1) % MODES;
}
last_interrupt_time = interrupt_time;
}
void loop() {
if(mode == 0) {
// Fill along the length of the strip in various colors...
colorWipe(strip1.Color(125, 0, 0) , 50); // Red
colorWipe(strip1.Color(255, 0, 0) , 50); // Red
colorWipe(strip1.Color( 0, 125, 0) , 50); // Green
colorWipe(strip1.Color( 0, 255, 0) , 50); // Green
colorWipe(strip1.Color( 0, 0, 125) , 50); // Blue
colorWipe(strip1.Color( 0, 0, 255) , 50); // Blue
colorWipe(strip1.Color( 255, 255, 255, 255), 50); // True white (not RGB white)
colorWipe(strip1.Color( 255, 255, 255, 255), 150); // True white (not RGB white)
}
else if (mode == 1) {
// Fill along the length of the strip in various colors...
colorWipe(strip2.Color(125, 0, 0) , 50); // Red
colorWipe(strip2.Color(255, 0, 0) , 50); // Red
colorWipe(strip2.Color( 0, 125, 0) , 50); // Green
colorWipe(strip2.Color( 0, 255, 0) , 50); // Green
colorWipe(strip2.Color( 0, 0, 125) , 50); // Blue
colorWipe(strip2.Color( 0, 0, 255) , 50); // Blue
colorWipe(strip2.Color( 255, 255, 255, 255), 50); // True white (not RGB white)
colorWipe(strip2.Color( 255, 255, 255, 255), 150); // True white (not RGB white)
}
else if (mode == 2) {
// Fill along the length of the strip in various colors...
colorWipe(strip3.Color(125, 0, 0) , 50); // Red
colorWipe(strip3.Color(255, 0, 0) , 50); // Red
colorWipe(strip3.Color( 0, 125, 0) , 50); // Green
colorWipe(strip3.Color( 0, 255, 0) , 50); // Green
colorWipe(strip3.Color( 0, 0, 125) , 50); // Blue
colorWipe(strip3.Color( 0, 0, 255) , 50); // Blue
colorWipe(strip3.Color( 255, 255, 255, 255), 50); // True white (not RGB white)
colorWipe(strip3.Color( 255, 255, 255, 255), 150); // True white (not RGB white)
}
}
void EsperaRespuesta_old (){
for(int i=0; i<strip1.numPixels(); i++) { // For each pixel in strip1...
strip1.setPixelColor(i, (197, 249, 20));
strip2.setPixelColor(i, (197, 249, 20));
strip3.setPixelColor(i, (197, 249, 20));
strip1.show();
strip2.show();
strip3.show();
}
}
void EsperaRespuesta() {
const int delayTime = 2; // Tiempo de espera entre cambios de brillo
const int numSteps = 25; // Número de pasos para el efecto de respiración
// Efecto de respiración: aumentar y disminuir el brillo
for (int j = 0; j < 2; j++) { // Repetir el ciclo dos veces (sube y baja)
for (int step = 0; step <= numSteps; step++) {
// Calcular el brillo actual (de 0 a 255)
int brightness = map(step, 0, numSteps, 0, 255);
// Ajustar el brillo de los LEDs
for (int i = 0; i < strip1.numPixels(); i++) {
strip1.setPixelColor(i, strip1.Color(brightness, 249 * brightness / 255, 20 * brightness / 255));
strip2.setPixelColor(i, strip2.Color(brightness, 249 * brightness / 255, 20 * brightness / 255));
strip3.setPixelColor(i, strip3.Color(brightness, 249 * brightness / 255, 20 * brightness / 255));
}
// Mostrar los LEDs con el nuevo brillo
strip1.show();
strip2.show();
strip3.show();
// Esperar un poco antes de cambiar el brillo
delay(delayTime);
}
// Invertir la dirección del brillo
for (int step = numSteps; step >= 0; step--) {
int brightness = map(step, 0, numSteps, 0, 255);
// Ajustar el brillo de los LEDs
for (int i = 0; i < strip1.numPixels(); i++) {
strip1.setPixelColor(i, strip1.Color(brightness, 249 * brightness / 255, 20 * brightness / 255));
strip2.setPixelColor(i, strip2.Color(brightness, 249 * brightness / 255, 20 * brightness / 255));
strip3.setPixelColor(i, strip3.Color(brightness, 249 * brightness / 255, 20 * brightness / 255));
}
// Mostrar los LEDs con el nuevo brillo
strip1.show();
strip2.show();
strip3.show();
// Esperar un poco antes de cambiar el brillo
delay(delayTime);
}
}
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip1.numPixels(); i++) { // For each pixel in strip1...
if(mode == 0) {
strip2.fill();
strip2.show();
strip3.fill();
strip3.show();
strip1.setPixelColor(i, color); // Set pixel's color (in RAM)
strip1.show(); // Update strip to match
}
else if (mode == 1) {
strip1.fill();
strip1.show();
strip3.fill();
strip3.show();
strip2.setPixelColor(i, color); // Set pixel's color (in RAM)
strip2.show(); // Update strip to match
}
else if (mode == 2) {
strip1.fill();
strip1.show();
strip2.fill();
strip2.show();
strip3.setPixelColor(i, color); // Set pixel's color (in RAM)
strip3.show(); // Update strip to match
}
else if (mode == 3) {
EsperaRespuesta();
}
delay(wait); // Pause for a moment
}
}