Hi,
for a project i need to program 12 capacitive touch buttons to control a neopixel jewel which contains 7 neopixels.
I want the neopixels to act as one pixel.
when no buttons are touched i want the neopixel to pulse white.
When i touch a button i want the pixel to turn in one color (small fade in)... I tried this for 3 different buttons but i have some problems:
- when a button is touched it doesnt directly change color, it waits until the loop is finished and then changes.
- when a button is touched and changes color it keeps pulsing
i am pretty new to arduino and programming in general..
any help is welcome!!
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "Adafruit_MPR121.h"
#include <avr/power.h>
uint16_t padTouched = 0;
Adafruit_MPR121 cap = Adafruit_MPR121();
#define PIXEL_PIN 3 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 7
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
cap.begin(0x5A);
}
void loop() {
int padTouched = cap.touched();
if (!padTouched) {
pulSe(255, 255, 255, 10);
}
if (padTouched & (1 << 0)) {
fadeIn(255, 0, 0, 1);
}
if (padTouched & (1 << 4)) {
fadeIn(0, 255, 0, 1);
}
if (padTouched & (1 << 8)) {
fadeIn(0, 0, 255, 1);
}
}
void pulSe(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
for (uint8_t b = 0; b < 255; b++) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
}
for (uint8_t b = 255; b > 0; b--) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
}
}
void fadeIn(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
for (uint8_t b = 0; b < 255; b++) {
for (uint8_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
}
strip.show();
delay(wait);
}
}
demo2.ino (2.27 KB)