Lab_interaccio/2019/RAQUETA-Flaix/Raqueta_v1/Raqueta_v1.ino
2025-02-25 21:29:42 +01:00

294 lines
7.2 KiB
C++

#include "Adafruit_NeoPixel.h"
#define NUMPIXELS 156
#define LED_PIN 5
//#define LED_PIN 4
#define BRIGHTNESS 50
#define BUTTON 12 //botón en GPIO12 (D6)
// #define BUTTON 14 //botón en GPIO14 (D5) - PLACA DE BACKUP NR3 porque el D6 esta petado
volatile int sceneNumber = 0;
volatile bool sceneFlag = 0;
//volatile bool oldSceneFlag = 0;
//volatile int ledState = HIGH; // the current state of the output pin
//volatile int buttonState; // the current reading from the input pin
//volatile int lastButtonState = LOW; // the previous reading from the input pin
//unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
//unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
//bool previousPinState = HIGH;
Adafruit_NeoPixel strip(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
void ICACHE_RAM_ATTR scene();
void setup()
{
Serial.begin(115200);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(BUTTON, HIGH);
attachInterrupt(BUTTON, scene, FALLING);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(BRIGHTNESS); // Set BRIGHTNESS to about 1/5 (max = 255)
delay(1000);
initSeq(); // secuencia de inicio blanca
}
void scene()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 500)
{
sceneFlag = true;
if(!digitalRead(BUTTON))
{
//Serial.println(sceneFlag);
sceneFlag = 0;
sceneNumber++;
if(sceneNumber>4)
sceneNumber = 0;
}
}
last_interrupt_time = interrupt_time;
}
void colorWipe(uint32_t color, int wait)
{
for(int i=0; i<strip.numPixels(); i++)
{ // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorWipe2(uint32_t color, int wait)
{
for(int i=0; i<=(NUMPIXELS/2); i++) {
strip.setPixelColor(i, color);
strip.setPixelColor(NUMPIXELS - i, color);
strip.show();
delay(wait);
}
}
void colorGradient(int r1, int g1, int b1, int r2, int g2, int b2, int wait, int modo)
{
int steps = NUMPIXELS;
for (int i = 0; i < steps; i++) {
uint8_t red = (((r1 * (steps - i)) + (r2 * i)) / steps);
uint8_t green = (((g1 * (steps - i)) + (g2 * i)) / steps);
uint8_t blue = (((b1 * (steps - i)) + (b2 * i)) / steps);
// Sets the pixels to the color adjusted in the fade
strip.setPixelColor(i, red, green, blue);
if(modo)
strip.setPixelColor(NUMPIXELS - i, strip.getPixelColor(i)); // clonamos el interior
strip.show();
delay(wait);
}
}
uint8_t white(uint32_t c) {
return (c >> 24);
}
uint8_t red(uint32_t c) {
return (c >> 16);
}
uint8_t green(uint32_t c) {
return (c >> 8);
}
uint8_t blue(uint32_t c) {
return (c);
}
void initSeq()
{
///////////////////
// FADE IN WHITE
///////////////////
for(int i=0; i<=(NUMPIXELS/2); i++)
{
strip.setPixelColor(i, strip.Color(255, 255, 255));
strip.setPixelColor(NUMPIXELS - i, strip.Color(255, 255, 255));
strip.show();
}
///////////////////
// FADE OUT
///////////////////
//fadeOut(25, 10, 10); // frames, fadevalue, delay ;
}
void fadeOut(int frames, int fadeValue, int wait)
{
for(int f=0; f<=frames; f++) // number of fade frames
{
for(int i=0; i<=NUMPIXELS; i++)
{
fadeToBlack(i, fadeValue); // fadevalue = time of fade out
}
strip.show();
delay(wait);
}
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[NUMPIXELS];
int cooldown;
// Step 1. Cool down every cell a little
for ( int i = 0; i < NUMPIXELS ; i++) {
cooldown = random(0, ((Cooling * 10) / NUMPIXELS) + 2);
if (cooldown > heat[i]) {
heat[i] = 0;
} else {
heat[i] = heat[i] - cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for ( int k = NUMPIXELS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if ( random(255) < Sparking ) {
int y = random(7);
heat[y] = heat[y] + random(160, 255);
//heat[y] = random(160,255);
}
// Step 4. Convert heat to LED colors
for ( int j = 0; j < NUMPIXELS; j++) {
setPixelHeatColor(j, heat[j] );
}
for(int i=0; i<=(NUMPIXELS/2); i++)
{ // For each pixel in strip...
strip.setPixelColor(NUMPIXELS - i, strip.getPixelColor(i));
}
strip.show();
delay(SpeedDelay);
}
void setPixelHeatColor (int Pixel, byte temperature)
{
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature / 255.0) * 191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
// figure out which third of the spectrum we're in:
if ( t192 > 0x80) { // hottest
setPixel(Pixel, 255, 255, heatramp, 0);
} else if ( t192 > 0x40 ) { // middle
setPixel(Pixel, 255, heatramp, 0, 0);
} else { // coolest
setPixel(Pixel, heatramp, 0, 0, 0);
}
}
void setPixel(int Pixel, byte red, byte green, byte blue, byte white)
{
strip.setPixelColor(Pixel, strip.Color(red, green, blue, white));
}
void setAll(byte red, byte green, byte blue, byte white)
{
for (int i = 0; i < NUMPIXELS; i++ ) {
setPixel(i, red, green, blue, white);
}
}
void fadeToBlack(int ledNo, byte fadeValue)
{
uint32_t oldColor;
uint8_t r, g, b, w;
int value;
oldColor = strip.getPixelColor(ledNo);
w = (oldColor & 0xff000000UL) >> 24;
r = (oldColor & 0x00ff0000UL) >> 16;
g = (oldColor & 0x0000ff00UL) >> 8;
b = (oldColor & 0x000000ffUL);
r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
w=(w<=10)? 0 : (int) w-(w*fadeValue/256);
strip.setPixelColor(ledNo, r,g,b,w);
}
void loop()
{
// if( sceneFlag )
// {
// sceneFlag = 0;
// }
switch (sceneNumber)
{
case 0 : { // OFF
//setAll(0, 0, 0, 0); // Aqui sería mejor tener un Fade Out
//strip.show();
fadeOut( 25, 10, 5 );
break;
}
case 1 : {
Fire(55,120,10); // FIRE
break;
}
case 2 : { // RED
colorWipe2(strip.Color(255, 0, 0), 3); // RED: Estaria bien un degradado
//colorGradient(255,0,0,255,255,0, 10, 0);
//colorGradient(255,0,0,255,255,0, 10, 1);
break;
}
case 3 : { // BLUE
colorWipe2(strip.Color(0, 0, 255), 3); // BLUE: Estaria bien un degradado
break;
}
case 4 : { // GREEN
colorWipe2(strip.Color(0, 255, 0), 3); // GREEN: Estaria bien un degradado
break;
}
}
}