All,
I am trying to create a light object as follows. Using an Arduino (nano likely) connected to a Neopixel 8 LEDs strip (NeoPixel Stick - 8 x 5050 RGBW LEDs - Cool White - ~6000K : ID 2869 : $7.95 : Adafruit Industries, Unique & fun DIY electronics and kits) connected to pin 6, and a button to ground on pin A0, when booted, all LEDs should light white. Then, upon pressing the button, I would like it to cycle through some different lighting effects, basically, all LEDs red, all LEDs blue, all LEDs green, then a random fading in and out of various colors, then back to all LEDs white.
I am no programmer, so I have been trying to generate code with ChatGPT, and using Tinkercad to run simulations, but it is not working. The final code so far is:
#include <Adafruit_NeoPixel.h>
#define PIN 6 // Define the pin to which the data input of the NeoPixel strip is connected
#define NUMPIXELS 8 // Define the number of NeoPixels in your strip
#define BUTTON_PIN A0 // Define the pin to which the analog button is connected
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int buttonState = 0;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int currentMode = 0;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
int reading = analogRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState > 900) { // Adjust this threshold based on your button characteristics
// Button is pressed, change lighting mode
currentMode = (currentMode + 1) % 5;
switch (currentMode) {
case 0:
setAllPixelsColor(255, 255, 255, 0); // White
break;
case 1:
setAllPixelsColor(255, 0, 0, 0); // Red
break;
case 2:
setAllPixelsColor(0, 0, 255, 0); // Blue
break;
case 3:
randomFade();
break;
case 4:
setAllPixelsColor(0, 0, 0, 0); // Turn off all LEDs
break;
}
}
}
}
lastButtonState = reading;
}
void setAllPixelsColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(r, g, b, w));
}
strip.show();
}
void randomFade() {
for (int i = 0; i < 256; i++) {
for (int j = 0; j < NUMPIXELS; j++) {
uint8_t color = random(256);
strip.setPixelColor(j, strip.Color(color, color, color, 0));
}
strip.show();
delay(20);
}
}
When I simulate the code in Tinkercad, absolutely nothing happens. The LEDs don't light upon boot up, and pressing the button does nothing.
Any help is greatly appreciated.
Thanks,
Michael