I'm new to both Arduino and to this forum. This is my first post. I know there are plenty of posts regarding both generic leds, other libraries and neopixels but I'm not experienced enough to see the forest for just trees and piece something together. I expect nothing, but I would really appreciate any help possible.
I recently had a son, and got hold of glow in the dark PLA filament, which resulted in an idea to make a night lamp for him using parts I had laying around (Arduino Nano, 2x pushbuttons, and a ledstrip of 6x WS2812 GRB LEDs.)
Link to my schematic
- Am I right in believing I am within the power budget, powering 6 "Neopixels" from USB? From my research this should total a max current draw of 360mA
- Can I also get by without the resistor between the Nano and the ledstrip, or is this strictly necessary?
- Can I get by using the internal pullup-resistor for the two pushbuttons or do I need resistors here aswell?
Based on my testing, it seems to work alright but I would appreciate an experienced opinion.
Self-aware of my lack of prowess in coding, I turned to ChatGPT. This got me quite a ways, but context from one iteration to another quickly got lost, the code got messy and I gave up.
I never achieved the primary goal with ChatGPT, but here's the latest output which lets me toggle the lights, but when I turn them on they dim down and then back up to full brightness and that's it.
#include <Adafruit_NeoPixel.h>
#define PIN 6 // The digital pin connected to the NeoPixel strip
#define BUTTON_PIN 7 // The digital pin connected to the push button
#define NUMPIXELS 4 // The number of NeoPixels in your strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
bool lightsOn = false;
int brightness = 255; // Initial brightness (0 to 255)
int buttonState = HIGH; // Initial button state
unsigned long timePUSHED = 0; // Add this line for the time of the last button press
// Function declaration
void colorWipe(uint32_t color, int wait);
void decreaseBrightness();
void increaseBrightness();
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable internal pull-up resistor for the button pin
}
void loop() {
// Check button press
if (digitalRead(BUTTON_PIN) == LOW && !lightsOn) {
// Button is pressed, turn on the lights
lightsOn = true;
brightness = 255; // Reset brightness to full
colorWipe(strip.Color(255, 255, 255), 10);
delay(500); // Debounce delay
} else if (digitalRead(BUTTON_PIN) == LOW && lightsOn) {
// Button is pressed, turn off the lights
lightsOn = false;
colorWipe(strip.Color(0, 0, 0), 10);
delay(500); // Debounce delay
}
// Check button hold for continuous brightness adjustment
if (digitalRead(BUTTON_PIN) == HIGH && millis() - timePUSHED > 500) {
// Debounce delay and ensure it's not triggered immediately after button press
delay(50);
// Check the state of the button
if (buttonState == LOW) {
// Button was low, decrease the lights continuously
decreaseBrightness();
} else {
// Button was high, increase the lights continuously
increaseBrightness();
}
timePUSHED = millis(); // Reset timePUSHED to avoid immediate re-triggering
}
// Update the button state
buttonState = digitalRead(BUTTON_PIN);
}
// Function definition
void colorWipe(uint32_t color, int wait) {
for(int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
// New functions for continuous brightness adjustment
void decreaseBrightness() {
while (digitalRead(BUTTON_PIN) == HIGH && brightness > 26) {
brightness = constrain(brightness - 5, 26, 255); // Decrease brightness to minimum 10%
strip.setBrightness(brightness);
strip.show();
delay(50);
}
}
void increaseBrightness() {
while (digitalRead(BUTTON_PIN) == HIGH && brightness < 255) {
brightness = constrain(brightness + 5, 26, 255); // Increase brightness to maximum
strip.setBrightness(brightness);
strip.show();
delay(50);
}
}
Primary goal
I want start with the LEDs OFF and toggle a white light ON/OFF with a short press of pushbutton connected to D7. Then I would like to be able to dim/increase brightness with a button hold determined by if the current brightness is below or above mid brightness. If I am below mid brightness and hold the button, I want the LEDs to increase from current brightness to full brightness or the level I release the button at, and if I am above mid brightness, I want the LEDs to dim from the current brightness down to 10% or the brightness where I release the button.
Secondary goal
I would like the second pushbutton to cycle through a few colours on buttonpress and back to white in the cycle, maintaining the functionality of the first button on each colour
Above and beyond goal
Holding both buttons down to reset LEDs to white and implement some more colours
