Is there a CircuitPython equivalent?
Code: Select all
#include <ezButton.h>
#include <Adafruit_DotStar.h>
#include <SPI.h> // COMMENT OUT THIS LINE FOR GEMMA OR TRINKET
//#include <avr/power.h> // ENABLE THIS LINE FOR GEMMA OR TRINKET
#define EFFECT_STATE_NONE 0
#define EFFECT_STATE_MOVE 1
#define EFFECT_STATE_FADE 2
#define NUMPIXELS 30 // Number of LEDs in strip
// Hardware SPI is a little faster, but must be wired to specific pins
// (Arduino Uno = pin 11 for data, 13 for clock, other boards are different).
Adafruit_DotStar strip(NUMPIXELS, DOTSTAR_BRG);
ezButton button(7); // create ezButton object that attach to pin 7;
unsigned long EFFECT_TIME_ALL = 5000;// Length of the entire process
unsigned long EFFECT_TIME_FADE = 1000;// How long do you want them to flicker when they meet in the middle
unsigned long EFFECT_TIME_MOVE = EFFECT_TIME_ALL - EFFECT_TIME_FADE;
int EFFECT_FADE_NUM = 4; // fade 4 time between 200 and 255, you can change this value
int lengthA = NUMPIXELS;
int headX = 0;
int headY = NUMPIXELS;
int randomN = 0;
uint32_t color = 0xFFA500;
unsigned long timeMoveStart;
unsigned long timeFadeStart;
unsigned long progress;
unsigned long brightness = 0;
int fadeCount = 0;
int effectState = EFFECT_STATE_NONE;
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000L)
clock_prescale_set(clock_div_1); // Enable 16 MHz on Trinket
#endif
strip.begin(); // Initialize pins for output
strip.show(); // Turn all LEDs off ASAP
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
switch (effectState) {
case EFFECT_STATE_NONE:
if (button.isPressed()) {
effectState = EFFECT_STATE_MOVE;
timeMoveStart = millis();
randomN = random(0, lengthA); // The min is 0 and max is lengthA - 1
}
break;
case EFFECT_STATE_MOVE:
progress = millis() - timeMoveStart;
brightness = map(progress, 0, EFFECT_TIME_MOVE, 0, 200);
headX = map(progress, 0, EFFECT_TIME_MOVE, 0, randomN);
headY = map(progress, 0, EFFECT_TIME_MOVE, lengthA, randomN);
if (headX >= randomN) {
headX = randomN;
headY = randomN;
effectState = EFFECT_STATE_FADE;
timeFadeStart = millis();
fadeCount = 0;
}
break;
case EFFECT_STATE_FADE:
progress = millis() - timeFadeStart;
if ((fadeCount % 2) == 0) { // fade up
brightness = map(progress, 0, EFFECT_TIME_FADE / EFFECT_FADE_NUM, 200, 255);
if (brightness > 255) {
fadeCount++;
timeFadeStart = millis();
}
} else {// fade down
brightness = map(progress, 0, EFFECT_TIME_FADE / EFFECT_FADE_NUM, 255, 200);
if (brightness < 200) {
fadeCount++;
timeFadeStart = millis();
}
}
if (fadeCount > EFFECT_FADE_NUM) {
effectState = EFFECT_STATE_NONE;
brightness = 0; // turn off all
}
break;
}
strip.setBrightness(brightness);
for (int i = 0; i <= lengthA; i++) {
if (i == headX || i == headY)
strip.setPixelColor(i, color);
else
strip.setPixelColor(i, 0); // turn off
}
strip.show();
}