I want to start by saying I'm a beginner, I've been researching this issue for weeks now and haven't come up with a reliable solution yet.
I'm trying to use two pads on an MPR121 to trigger an animation while the pads is held down, and stop the animation when the pad is released. Each pad controls 12 LEDs on a ring of 24.
Right now I can press both pads simultaneously to turn the lights on and off, but instead of running an animation, it just turns on one light from each side. If I press the pads rapidly, it will turn random lights on and off, but never goes through the animation.
I'm not sure if the issue is with my code or something else.
Hardware:
Arduino Nano, MPR121, Neopixel ring w/ 24 LEDs.
I'm afraid that in my attempts to fix this, I overcomplicated my code for what I'm trying to achieve.
Both of the animations worked on their own without being controlled by the MPR121.
What am I missing to make this work?
This is what my code is now:
```cpp
#include <FastLED.h>
#include <Wire.h>
#include "Adafruit_MPR121.h"
#define NUM_LEDS 24
#define LED_PIN 7
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap = Adafruit_MPR121();
CRGB leds[NUM_LEDS];
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
void setup() {
cap.begin(0x5a);
FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(50);
}
void loop() {
currtouched = cap.touched();
int state = 0;
for (uint8_t i = 0; i < 12; i++) {
if ((currtouched & _BV(2)) && !(lasttouched & _BV(2))) {
state = 1;
}
if ((currtouched & _BV(8)) && !(lasttouched & _BV(8))) {
state = 2;
}
if (!(currtouched & _BV(2)) && (lasttouched & _BV(2))) {
state = 3;
}
if (!(currtouched & _BV(8)) && (lasttouched & _BV(8))) {
state = 4;
}
lasttouched = currtouched;
}
switch (state) {
case 1:
blueDot();
break;
case 2:
orangeDot();
break;
case 3:
blueStop();
break;
case 4:
orangeStop();
break;
}
}
void blueDot() {
uint8_t sinBeat = beatsin8(30, 0, 11, 0, 0);
leds[sinBeat] = CHSV(120, 255, 150);
fadeToBlackBy(leds, sinBeat, 10);
FastLED.show();
}
void orangeDot() {
uint8_t sinBeat2 = beatsin8(30, 12, 23, 0, 0);
leds[sinBeat2] = CHSV(11, 255, 150);
fadeToBlackBy(leds, sinBeat2, 10);
FastLED.show();
}
void orangeStop() {
for (int i = 12; i <= 23; i++) {
leds[i] = CRGB::Black;
FastLED.show();
}
}
void blueStop() {
for (int i = 0; i <= 11; i++) {
leds[i] = CRGB::Black;
FastLED.show();
}
}
Thanks in advance.