#include // NeoPixel Variables... #define PIXELPIN 21 #define NUMPIXELS 8 Adafruit_NeoPixel pixels(NUMPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800); // Button Variables... #define BUTTON A3 int buttonState; int lastButtonState = HIGH; // Button Bounce Fix... long bounceTime = 0; long bounceDelay = 50; // Setup Variables... long resetTimer = 0; int resetState = 0; // Anim Variables... long animTimer = millis(); int animPixel = 0; // Global Variables... int deviceMode = 0; // Function to toggle device modes............................................ void changeMode() { if (deviceMode != 1) { for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(0,100,0)); } delay(50); pixels.show(); deviceMode = 1; Serial.println("Mode 1"); } else { for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(0,0,100)); } delay(50); pixels.show(); deviceMode = 2; Serial.println("Mode 2"); } } // Function to put device in set-up mode............................................ void setupMode() { if (deviceMode != 3) // enter set-up mode... { for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(100,60,0)); } delay(50); pixels.show(); deviceMode = 3; Serial.println("Set-Up Mode"); } else // exit set-up mode... { for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(0,100,0)); } delay(50); pixels.show(); deviceMode = 0; } } // Function to Handle Pixel Patterns for Animations.................................. int pixelNo(int x) { if (x < 0) { x = (NUMPIXELS) + x; } if (x >= (NUMPIXELS)) { x = (NUMPIXELS) - x; } return x; } // Animation Function................................................................. void anim(){ if (deviceMode == 3) { if (millis() - animTimer > 120) { animTimer += 120; pixels.clear(); // Needed Due to ESP32 Timing Issues for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(2,0,0)); } pixels.setPixelColor(pixelNo(animPixel), pixels.Color(100,0,0)); pixels.setPixelColor(pixelNo(animPixel - 1), pixels.Color(20,0,0)); delay(1); // Needed Due to ESP32 Timing Issues pixels.show(); animPixel += 1; if (animPixel >= NUMPIXELS) { animPixel = 0; } } } } //--------------------------------------------------------------------------------------------- void setup() { // Start Serial Interface... Serial.begin(115200); while(!Serial) Serial.println("Serial Started..."); // Clear Pixels... pixels.begin(); pixels.clear(); pixels.show(); pixels.show(); // Needed Due to ESP32 Timing Issues Serial.println("Pixels Cleared..."); delay(2000); // Set Pixels to Red... for (int i = 0; i < NUMPIXELS; i++) { pixels.setPixelColor(i, pixels.Color(100,0,0)); } pixels.show(); Serial.println("Pixels Red..."); // Set Pin Mode for BUTTON... pinMode(BUTTON, INPUT); // Set up stuff here... delay(4000); // Used to simulate a delay... changeMode(); } //--------------------------------------------------------------------------------------------- void loop() { int reading = digitalRead(BUTTON); if (reading != lastButtonState) // If button state has changed previously... { bounceTime = millis(); } if ((millis() - bounceTime) > bounceDelay) // If it's been [bounceDelay] since last button state change... { if (reading != buttonState) // If button state has changed again... { buttonState = reading; if (buttonState == LOW && deviceMode != 3) { Serial.println("Button Up"); changeMode(); } } } if (reading == HIGH) { if (resetState == 0) { resetTimer = millis(); } resetState = 1; if ((millis() - resetTimer) > 5000) // If the button has been pressed for 5 seconds... { resetState = 0; resetTimer = 0; setupMode(); } } else { resetState = 0; resetTimer = 0; } lastButtonState = reading; anim(); } //---------------------------------------------------------------------------------------------