Hello,
I apologize for my English level, and my programming level.
I have some problems with my project. I've connected an MPR121, neopixels and an arduino nano for a lamp. The general idea is to be able to either have a static light by modifying the luminosity or some dynamic effects (fire, rainbow ...) by pressing one of the inputs of the MPR121.
I tried with the "switch" function which works on static lights but blocks on dynamic effects, and I don't have 12 dospinible pins
// MPR121 initialization
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap = Adafruit_MPR121();
// Neopixel initialization
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
#define LED_PIN 6 //Led for the column
#define LED_COUNT 47 //Led for the column
#define BRIGHTNESS 255
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
void setup() {
Serial.begin(9600);
while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(10);
}
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin();
strip.show();
strip.setBrightness(BRIGHTNESS);
}
void loop() {
currtouched = cap.touched();
switch (currtouched) {
case 1:
Fire(55,120,15);
break;
case 2:
colorWipe(strip.Color(0, 0, 0, 170));
break;
case 3:
colorWipe(strip.Color(0, 0, 0, 255));
break;
case 4:
colorWipe(strip.Color(255, 0, 0, 0));
break;
case 5:
colorWipe(strip.Color(0, 255, 0, 0));
break;
case 11:
Serial.print("touched");
break;
default:
currtouched = lasttouched;
break;
}
return;
}
void colorWipe(uint32_t color) {
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
}
}
/* From https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#LEDStripEffectFire */
void setPixel(int Pixel, byte red, byte green, byte blue, byte white) {
// #ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue, white));
// #endif
// #ifndef <Adafruit_NeoPixel.h>
}
void setAll(byte red, byte green, byte blue, byte white) {
for(int i = 0; i < LED_COUNT; i++ ) {
setPixel(i, red, green, blue, white);
}
strip.show();
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[LED_COUNT];
int cooldown;
// Step 1. Cool down every cell a little
for( int i = 0; i < LED_COUNT; i++) {
cooldown = random(0, ((Cooling * 10) / LED_COUNT) + 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= LED_COUNT - 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 < LED_COUNT; j++) {
setPixelHeatColor(j, heat[j] );
}
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);
}
}
I tried with "if ... else" but the program directly launches the first effect and I cannot change it.
void loop() {
currtouched = cap.touched();
if (currtouched = 1) {
Fire(55,120,15);
}
else if (currtouched = 2) {
colorWipe(strip.Color(0,0,0,255));
}
else {
currtouched = lasttouched;
}
return;
}
Thank you in advance for your ideas and advices.