Hi folks,
as you might know, Im not a coder. And because I only use Arduino for some of my scale model projects, I just get lost everytime I start coding again.
So, thats why I don´t know where to start and Im stuck on "easy" tasks.
Im not just a Trekkie but also a big classic Power Rangers Fan. So I bought a Hasbro Lightning Collection Power Morpher to change the toy like electronics and make it my own.
The toy has a coin recognition (the coins have nodges which press buttons). So depending on which buttons are pressed, the light has different colors. I would like to keep this idea, so I can keep the micro-switches.
I want to use a WS2812 ring for the light and as an animation I had a rotating color gradient in mind, maybe with some white sparkling.
To start simple, I was thinking about something like: Button1 pressed = red_gradient, Button2 pressed = blue_gradient, Button1 AND Button2 pressed = green_gradient
That is the way the toy already works.
My first poor attempt is just a code I found online which I tried to customize.
#include <FastLED.h>
#define NUM_LEDS 48
#define buttonPin1 5
#define buttonPin2 3
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds < NEOPIXEL, PB1 > (leds, NUM_LEDS);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
if (digitalRead(buttonPin1) == LOW) {
CRGB pattern[] = {
CRGB(255, 255, 255),
CRGB(255, 255, 255),
CRGB(255, 0, 0),
CRGB(255, 0, 0),
};
size_t pattern_sz = sizeof(pattern) / sizeof( * pattern);
// a 8.8 fixed point position (so 256 = 1.0, 512 = 2.0, 65535 = 255.996)
static uint16_t basepos = 0;
// a copy of basepos to be modified over the length of the strip
uint16_t ledpos = basepos;
for (uint8_t ledno = 0; ledno < NUM_LEDS; ledno++) {
// find the integer and fractional parts for this LED
uint8_t intpos = ledpos >> 8;
uint8_t fracpos = ledpos & 0xff;
// select the two colours to be blended for this LED
CRGB colA = pattern[intpos % pattern_sz];
CRGB colB = pattern[(intpos + 1) % pattern_sz];
// fade colA towards colB by the fractional position
leds[ledno] = fadeTowardColor(colA, colB, fracpos);
// move along the source pattern by some fraction of a pixel
ledpos -= 128;
}
FastLED.show();
delay(10);
// move the base position by some fractional amount
basepos += beatsin8(5, 1, 128);
}
if (digitalRead(buttonPin2) == LOW) {
CRGB pattern[] = {
CRGB(255, 255, 255),
CRGB(255, 255, 255),
CRGB(0, 0, 255),
CRGB(0, 0, 255),
};
size_t pattern_sz = sizeof(pattern) / sizeof( * pattern);
// a 8.8 fixed point position (so 256 = 1.0, 512 = 2.0, 65535 = 255.996)
static uint16_t basepos = 0;
// a copy of basepos to be modified over the length of the strip
uint16_t ledpos = basepos;
for (uint8_t ledno = 0; ledno < NUM_LEDS; ledno++) {
// find the integer and fractional parts for this LED
uint8_t intpos = ledpos >> 8;
uint8_t fracpos = ledpos & 0xff;
// select the two colours to be blended for this LED
CRGB colA = pattern[intpos % pattern_sz];
CRGB colB = pattern[(intpos + 1) % pattern_sz];
// fade colA towards colB by the fractional position
leds[ledno] = fadeTowardColor(colA, colB, fracpos);
// move along the source pattern by some fraction of a pixel
ledpos -= 128;
}
FastLED.show();
delay(10);
// move the base position by some fractional amount
basepos += beatsin8(5, 1, 128);
}
if (digitalRead(buttonPin1 == LOW) && (buttonPin2 == LOW)) {
CRGB pattern[] = {
CRGB(255, 255, 255),
CRGB(255, 255, 255),
CRGB(0, 255, 0),
CRGB(0, 255, 0),
};
size_t pattern_sz = sizeof(pattern) / sizeof( * pattern);
// a 8.8 fixed point position (so 256 = 1.0, 512 = 2.0, 65535 = 255.996)
static uint16_t basepos = 0;
// a copy of basepos to be modified over the length of the strip
uint16_t ledpos = basepos;
for (uint8_t ledno = 0; ledno < NUM_LEDS; ledno++) {
// find the integer and fractional parts for this LED
uint8_t intpos = ledpos >> 8;
uint8_t fracpos = ledpos & 0xff;
// select the two colours to be blended for this LED
CRGB colA = pattern[intpos % pattern_sz];
CRGB colB = pattern[(intpos + 1) % pattern_sz];
// fade colA towards colB by the fractional position
leds[ledno] = fadeTowardColor(colA, colB, fracpos);
// move along the source pattern by some fraction of a pixel
ledpos -= 128;
}
FastLED.show();
delay(10);
// move the base position by some fractional amount
basepos += beatsin8(5, 1, 128);
}
}
// Mark Kriegsman's fadeTowardColor from:
// https://gist.github.com/kriegsman/d0a5ed3c8f38c64adcb4837dafb6e690
// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8(uint8_t & cur,
const uint8_t target, uint8_t amount) {
if (cur == target) return;
if (cur < target) {
uint8_t delta = target - cur;
delta = scale8_video(delta, amount);
cur += delta;
} else {
uint8_t delta = cur - target;
delta = scale8_video(delta, amount);
cur -= delta;
}
}
// Blend one CRGB color toward another CRGB color by a given amount.
// Blending is linear, and done in the RGB color space.
// This function modifies 'cur' in place.
CRGB fadeTowardColor(CRGB & cur,
const CRGB & target, uint8_t amount) {
nblendU8TowardU8(cur.red, target.red, amount);
nblendU8TowardU8(cur.green, target.green, amount);
nblendU8TowardU8(cur.blue, target.blue, amount);
return cur;
}
c