Excellent set of effects here. Bravo! I am wanting to use a few in my ATMEGA328 Pro Trinket 5V. Im aware it is essentially an Arduino Uno.
Your code works perfect, but I am wanting to splice in a single, double, and long press state into this. Ideally 2 separate buttons to have 3 states like this. I am only going to be using 5 animations and an off LED state, but I can not for the life of me, get a button library working with this.
I sat and tried to figure out state machines, and I came up with this simple code, hoping to drop it into yours here, but I can’t get the state machine to work. Any suggestions on implementing these button states? Thanks for any advice at all, because I just keep hitting brick walls and nobody is giving me the time of day.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define LED_PIN 8
#define LED_COUNT 10
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
const int buttonPin = 3;
bool secondPress = false;
unsigned long timerOne;
unsigned long timerTwo;
unsigned long pressTime;
void setup(){
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
pinMode(buttonPin, INPUT_PULLUP);
strip.begin(); // INITIALIZE NeoPixel strip+++++++ object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop(){
if(buttonPin == LOW && secondPress == false) //first button press!
timerOne = millis();
secondPress = true;
if(buttonPin == LOW && secondPress == true) { //second press!
timerTwo = millis();
pressTime = timerTwo - timerOne; //presstime now holds the total time between presses in milliseconds.
if(pressTime <= 100);{
functionGo; // single press
}
if (pressTime > 300);{
functionGo2; // double press
//don't forget to reset!
timerOne = 0;
timerTwo = 0;
secondPress = false;
}
}
}
void functionGo() {
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color( 0, 255, 0), 50); // Green
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
}
void functionGo2(){
colorWipe(strip.Color( 0, 0, 255), 50); // Blue
colorWipe(strip.Color( 0, 255, 0), 50); // Green
colorWipe(strip.Color(255, 0, 0), 50); // Red
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
}
}