Created
March 12, 2024 16:18
-
-
Save msx80/c40d8da9854736c600b0d14af32a4cb0 to your computer and use it in GitHub Desktop.
Source code for this thing: https://fosstodon.org/@msx@livellosegreto.it/112059610172216598
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A non-blocking everyday NeoPixel strip test program. | |
// NEOPIXEL BEST PRACTICES for most reliable operation: | |
// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections. | |
// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel. | |
// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR. | |
// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS | |
// connect GROUND (-) first, then +, then data. | |
// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip, | |
// a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED. | |
// (Skipping these may work OK on your workbench but can fail in the field) | |
#include <Adafruit_NeoPixel.h> | |
#include<debounce.h> | |
// Which pin on the Arduino is connected to the NeoPixels? | |
// On a Trinket or Gemma we suggest changing this to 1: | |
#ifdef ESP32 | |
// Cannot use 6 as output for ESP. Pins 6-11 are connected to SPI flash. Use 16 instead. | |
#define LED_PIN 16 | |
#else | |
#define LED_PIN 6 | |
#endif | |
// How many NeoPixels are attached to the Arduino? | |
#define LED_COUNT 6 | |
// Declare our NeoPixel strip object: | |
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); | |
// Argument 1 = Number of pixels in NeoPixel strip | |
// Argument 2 = Arduino pin number (most are valid) | |
// Argument 3 = Pixel type flags, add together as needed: | |
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | |
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | |
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | |
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | |
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) | |
unsigned long pixelPrevious = 0; // Previous Pixel Millis | |
unsigned long patternPrevious = 0; // Previous Pattern Millis | |
int patternCurrent = 0; // Current Pattern Number | |
int patternInterval = 5000; // Pattern Interval (ms) | |
int pixelInterval = 10; // Pixel Interval (ms) | |
int pixelQueue = 0; // Pattern Pixel Queue | |
int pixelCycle = 0; // Pattern Pixel Cycle | |
uint16_t pixelCurrent = 0; // Pattern Current Pixel Number | |
uint16_t pixelNumber = LED_COUNT; // Total Number of Pixels | |
uint32_t tick = 0; | |
uint8_t mode = 0; | |
#define BUTTON_PIN 3 | |
#define NUM_MODES 4 | |
static void buttonHandler(uint8_t btnId, uint8_t btnState) { | |
if (btnState == BTN_PRESSED) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
mode++; | |
if(mode>=NUM_MODES) mode = 0; | |
} else { | |
// btnState == BTN_OPEN. | |
//Serial.println("Released button"); | |
digitalWrite(LED_BUILTIN, LOW); | |
} | |
} | |
// Define your button with a unique id (0) and handler function. | |
// (The ids are so one handler function can tell different buttons apart if necessary.) | |
static Button myButton(0, buttonHandler); | |
// setup() function -- runs once at startup -------------------------------- | |
void setup() { | |
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz. | |
// Any other board, you can remove this part (but no harm leaving it): | |
// END of Trinket-specific code. | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
pinMode(LED_BUILTIN, OUTPUT); | |
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) | |
strip.show(); // Turn OFF all pixels ASAP | |
strip.setBrightness(150); // Set BRIGHTNESS to about 1/5 (max = 255) | |
} | |
// loop() function -- runs repeatedly as long as board is on --------------- | |
void loop() { | |
myButton.update(digitalRead(BUTTON_PIN)); | |
tick ++; | |
uint32_t c; | |
/* | |
unsigned long currentMillis = millis(); // Update current time | |
if((currentMillis - patternPrevious) >= patternInterval) { // Check for expired time | |
patternPrevious = currentMillis; | |
patternCurrent++; // Advance to next pattern | |
if(patternCurrent >= 7) | |
patternCurrent = 0; | |
} | |
if(currentMillis - pixelPrevious >= pixelInterval) { // Check for expired time | |
pixelPrevious = currentMillis; // Run current frame | |
rainbow(10); // Flowing rainbow cycle along the whole strip | |
} | |
*/ | |
switch (mode) { | |
case 0: | |
c = strip.Color(255, 255, 255); | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
} | |
break; | |
case 1: | |
strip.rainbow(tick*4); | |
break; | |
case 2: | |
c = Wheel( (tick / 200) % 768); | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
} | |
break; | |
case 3: | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
double cc = (cos(((float) (tick+i*100)) / ((i*100)+300.0))+1.0); | |
double r = (cc*cc) / 4.0; | |
byte b = r * 255.0; | |
c = strip.Color(0,b,b/6); | |
strip.setPixelColor(i, c); | |
} | |
break; | |
} | |
strip.show(); | |
} | |
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames. | |
void rainbow(uint8_t wait) { | |
if(pixelInterval != wait) | |
pixelInterval = wait; | |
for(uint16_t i=0; i < pixelNumber; i++) { | |
strip.setPixelColor(i, Wheel((i + pixelCycle) & 255)); // Update delay time | |
} | |
strip.show(); // Update strip to match | |
pixelCycle++; // Advance current cycle | |
if(pixelCycle >= 256) | |
pixelCycle = 0; // Loop the cycle back to the begining | |
} | |
// 0-767 | |
uint32_t Wheel(uint32_t WheelPos) { | |
WheelPos = 767 - WheelPos; | |
if(WheelPos < 256) { | |
return strip.Color(255 - WheelPos, 0, WheelPos); | |
} | |
if(WheelPos < 512) { | |
WheelPos -= 256; | |
return strip.Color(0, WheelPos, 255 - WheelPos); | |
} | |
WheelPos -= 512; | |
return strip.Color(WheelPos, 255 - WheelPos, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment