Simple Night Lamp on timer yet I'm lost!

Hello everyone,

I'm making a little night lamp for my daughter who is afraid of the dark. I built a small birdhouse-like housing with a frosted acrylic panel in front. So my idea was to put a strip of 6 ws2812b led inside the house with an arduino nano. I want her to be able to press a button to light up the lights (she wants pink obviously) and I want it to shut down after 30 minutes.

I played with the adafruit neopixel and fastled libraries and I'm able to get the led strips to cycle through most of the examples but I don't know exactly how to proceed for the whole thing.

To resume. I want to light up a strip of ws2812b LED to a certain color for 30 minutes and then shut them down.

I'm just starting with Arduino/electronics stuff and loving it but I'm not familiar with any of the lingo and most of the projects I found online are all related to light sensor.

Any help would be greatly appreciated!

Thanks...

Jay

If you've already found an example of some code which creates more or less the desired lighting pattern, and your problem is only to terminate it after 30 minutes, then post the code here and you'll get some suggestions.
Maybe also include a sketch of your circuit - even a picture of a hand drawn diagram is OK.

Pretty simple. Read about the Adafruit Neopixel library to send the 18 bytes of data needed to set the color.

6 LEDs x 3 bytes/LED = 18 bytes.
Wire up a button to connect a pin to Gnd when pressed.
In your code set up the pin with its internal pullup resistor.
After sketch start, continually read the pin, it will read HIGH until the button is pressed.
When pressed (reads LOW), send out the 18 bytes and delay for 1000mS x 60 sec/min x 30 min, and send out 18 bytes of 0s to turn off the LEDs.
Go back to reading the button.

Thanks for the quick reply.

Here’s a quick png of my setup in attachment. The code works minus the 30 min timer. When I start the arduino, the LEDs are all off when I push the button, they light up to magenta. I could add another button to another input to start the timer. That would allow me to cycle through different case to have different presets/colors. I used the Adafruit Neopixel library with the buttoncycle exemple. I stripped the code that didn’t apply to what I wanted to do.

Here’s the code:

// This is a demonstration on how to use an input device to trigger changes on your neo pixels.
// You should wire a momentary push button to connect from ground to a digital IO pin.  When you
// press the button it will change to a new pixel animation.  Note that you need to press the
// button once to start the first animation!

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
                       // driven with a pull-up resistor so the switch should
                       // pull the pin to ground momentarily.  On a high -> low
                       // transition the button press logic will execute.

#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 4

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}

void loop() {
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);

// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
 // Short delay to debounce button.
 delay(20);
 // Check if button is still low after debounce.
 newState = digitalRead(BUTTON_PIN);
 if (newState == LOW) {
   showType++;
   if (showType > 9)
     showType=0;
   startShow(showType);
 }
}

// Set the last button state to the old state.
oldState = newState;
}

void startShow(int i) {
switch(i){
 case 0: colorWipe(strip.Color(0, 0, 0), 100);    // Black/off
         break;
 case 1: colorWipe(strip.Color(255, 0, 125), 50);  // pink
         break;
 

}
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
 strip.setPixelColor(i, c);
 strip.show();
 delay(wait);
}
}

void rainbow(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256; j++) {
 for(i=0; i<strip.numPixels(); i++) {
   strip.setPixelColor(i, Wheel((i+j) & 255));
 }
 strip.show();
 delay(wait);
}
}



// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
 WheelPos -= 85;
 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Thanks guys!

Jay

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

OPs frittzy; :o
b7668f18ad809b974c848c2c887a544af4a6849f.jpg

Tom… :slight_smile:

startShow(showType);

The key to timing things is to know two things, when they started and how long they should run. In this case when it started is easy. At this point record the time into a global unsigned long variable, let's call it showStartTime.

showStartTime = millis();
startShow(showType);

where showStartTime was declared globally as:

unsigned long showStartTime;

Now we can have a line in loop that checks to see how long the show has been running by simply taking the time difference between the time now and the time at the start. Simple subtraction.

unsigned long currentDuration = millis() - showStartTime;

Now that I know how to figure out how long it has been since the show started, I can have an if statement based on that.

if (currentDuration >= 30ul * 60* 1000){ 

      // turn it off
}

Where the *60 and *1000 convert to seconds and milliseconds respectively.

You might also need to have a variable to keep track of whether or not a show is currently running, but I'll leave that as an exercise for the reader.

Thanks Delta_G

I'll look into that! Hopefully, I'll get my Arduino for dummies by the end of the week. I'm by no means a programmer and I think I should go back to basics....

cheers

Jay

jdenommee:
Arduino for dummies

What will do you far more good than that would be to follow through any of the many many free online C++ tutorials. They print with a different looking function since they have screens on computers and a few other things like that, so it isn't 1 to 1 Arduino, but you'll really benefit from learning the language and how it works and what the rule are and usually in the course of one of those tutorials there will be quite a few basic programming logic lessons thrown in. 99.9999% of it all applies directly to what you do with Arduino.

Sparkfun has a free tutorial you can download and there should be one here also - both specific to Arduino and I would expect there are other good ones - I started with a beginner kit from Sparkfun with a Uno and parts - I had programmed in C many years ago so not totally new, I also have a PLC process control background and it is still tough to do some things with the Arduino - I find starting with basics nice and if I alredy know something the review helps to reinforce what I already know

good luck

If your daughter only wants pink, you can buy ordinary pink LEDs. Simple to use compared to ws2812b.
s-l400 (3).jpg

s-l400 (3).jpg

Thanks for the quick replies everyone!

PaulRB: I just had a bunch of WS2812b strips left from a previous project and I’m trying to re-use some of the stuff.

Saildude: I’ll look into that and might buy a starter kit too. It’s just that the shipping from the US to Canada is ridiculously high.

Delta_G: Good point but I don’t know if C++ is a good introduction to programming for a 40year old :). My friends, who are all programmers by trade tell me it’s a pretty steep language. What do you think?

Thanks guys, I’m so impressed at how helpful the community is. This is great!

Jay

Delta_G: Good point but I don't know if C++ is a good introduction to programming for a 40year old :). My friends, who are all programmers by trade tell me it's a pretty steep language. What do you think?

Well it's the language Arduino uses. So you'll have to learn it if you want to code for Arduino. Maybe learning Python would be easier, but it wouldn't get you any closer to being able to write code for the Arduino.

Yes the full C/ C++ language can be tough when starting IF you are looking to understand the full language including three dimensional arrays, structures. indexed pointers and passing pointers to functions

BUT - you don't need all of that for what you want to do - that is why I suggested the intro's above geared specific to Arduino and beginners - basic things like a bit of delay is only a bit confusing the first time as you will probably need the mills timing which the first time through I had to go slow - but got there

Please start simple at the beginning

Good luck

Well I didn't suggest that they go through and learn the whole language, just do a basic intro tutorial. All the Arduino specific stuff seems like it tries to dumb it down to the point that you end up missing important concepts like variable scope for example.

Yes, C++ is probably the best and most advanced language that is still efficient enough to run on something as basic as an Uno. After all, 2K ram, 8 bits and 16MHz has not been considered "powerful" since the mid '80s.

Your programmer friends will be used to quad core 3+Ghz 64 bit machines with gigabytes of ram at the very least. In that environment, there are more friendly development languages and IDEs than Arduino/C++, but they would be a complete non-starter for Uno/Mega.

I am the wrong side of 50, but I am in the process of learning a complex new language. It's tough, but also interesting and amazingly powerful and I'm enjoying the experience. It's called Scala and I'm using the IntelliJ IDE. It makes the Arduino IDE look like something from the stone age.

But I still enjoy using the Arduino IDE, C++ and the humble Nano or Attiny. Why? Because it's amazing what performance you can squeeze out of something basic but incredibly efficient, compared to more modern MCUs and computers.

Possibly helpful: Ralph Bacon's night light.

Thanks doug,

I think I could use some of the code. I'm jumping back into that project head first this weekend!

thanks for the share...

Jay

You're welcome.

I know this is a really old thread, and my apologies for reviving it, but I'm super anxious to know how this worked out and whether a final sketch that achieves the desired result was arrived to?

If so, I think everyone would love to see your final sketch, I know I would because I've kind of got my own ideas on how I expect it worked out?