#include <Adafruit_CircuitPlayground.h>
#include <FastLED.h>
#include <Wire.h>
#include <SPI.h>
#include <math.h>
// — External strip —
#define STRIP_PIN A2
#define STRIP_LEDS 30
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB stripLeds[STRIP_LEDS];
int sensorPin = A1; // there is no A10…
int sensorValue = 0;
int brightVal = 255;
// — Color list: {red, green, blue} —
const uint8_t colors[][3] = {
// you need three things in this array
{ 0, 180, 255 }, // cyan
{ 9, 9, 217 }, // blue
{140, 0, 255}, // {140, 0, 255}, // purple
{ 252, 3, 82 }, // hot pink
};
const int NUM_COLORS = sizeof(colors) / sizeof(colors[0]);
int colorIndex = 0;
// — Shake detection tuning —
const float SHAKE_THRESHOLD = 7.0;
const int SHAKE_COOLDOWN = 350;
unsigned long lastShakeTime = 0;
bool wasShaking = false;
void setup() {
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
CircuitPlayground.setBrightness(brightVal);
FastLED.addLeds<LED_TYPE, STRIP_PIN, COLOR_ORDER>(stripLeds, STRIP_LEDS)
.setDither(brightVal < 255);
FastLED.setBrightness(brightVal);
// Sanity flash: red on both outputs for 1 second
// fill_solid(stripLeds, STRIP_LEDS, CRGB::Red);
// FastLED.show();
// for (int i = 0; i < 10; i++) CircuitPlayground.setPixelColor(i, 255, 0, 0);
// CircuitPlayground.strip.show();
// delay(1000);
// fill_solid(stripLeds, STRIP_LEDS, CRGB::Black);
// FastLED.show();
// for (int i = 0; i < 10; i++) CircuitPlayground.setPixelColor(i, 0, 0, 0);
// CircuitPlayground.strip.show();
}
void setAllPixels(uint8_t r, uint8_t g, uint8_t b) {
// CPX onboard 10 pixels
for (int i = 0; i < 10; i++) {
// CircuitPlayground.strip.setPixelColor(i, r, g, b); // not strip
CircuitPlayground.setPixelColor(i, r, g, b);
}
// CircuitPlayground.strip.show(); //not needed
// External 30-pixel strip
fill_solid(stripLeds, STRIP_LEDS, CRGB(r, g, b));
FastLED.setBrightness(brightVal);
FastLED.show();
}
void loop() {
// Brightness from pot
sensorValue = analogRead(sensorPin);
brightVal = map(sensorValue, 0, 1023, 0, 225);
// CircuitPlayground.setBrightness(brightVal); redundant with FastLED brightness
// Shake detection
float x = CircuitPlayground.motionX();
float y = CircuitPlayground.motionY();
float z = CircuitPlayground.motionZ();
float magnitude = sqrt(x * x + y * y + z * z);
float shakeForce = abs(magnitude – 9.8);
unsigned long now = millis();
if (shakeForce > SHAKE_THRESHOLD) {
if (!wasShaking && (now – lastShakeTime > SHAKE_COOLDOWN)) {
colorIndex = (colorIndex + 1) % NUM_COLORS;
lastShakeTime = now;
}
wasShaking = true;
} else {
wasShaking = false;
}
setAllPixels(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2]);
delay(10); // teeny break
}