Arduino nano and 8-10 RGB led strip

Hello!

Im building a little lamp out of acrylic with engravings. My plan is to light it up with a little strip of led lights. Im planning to build a base with an arduino nano, and a little push button.

Then when I press the button, my idea is one of those 3 scenaries:
Press - Lights all white
Press - Light all rainbow color/Ramdom/ramp, whatever looks awsome
Press - Lamp off

As far I can read the nanbo has both D i/o, so it should be sufficient to do the work.

I admit I dont have any progamming experiences for arduino, and at the time, most of my spare/interest time is set to do sequences christmas lightings.. :o

-Am I lucky that some one will throw a quick sketch with the tree examples above, then Im confident I can adjust it so it fit my needs... It would be very appreciated!

Thanks in advance.. :slight_smile:

Start by telling us what kind of ledstrip :slight_smile: Preferable with a link to it.

Haha, foolishif me, thanks!

Its 5V (dont really matter that part, ill figure that out) :slight_smile:

Its WS2812B diodes :slight_smile:
(And I have had them running on one of the standard scripts I can find in the arduino library somewhere (think it was a year ago, but its stille the code laying on the arduino...) :smiley:

Alright, how long?

Do note, those WS2812B strips needs quite a lot of current!

I know, but its only gonna be 8-10 pixels, so it should be able to do it.. :slight_smile:

If you have the option, use an APA102 LED strip instead of the WS2812B's. For the code, you should probably ask the Gigs and collabs board instead.

Ahh, 8 or 10 pixels isn't to bad indeed. I was in doubt if you meant 10 pixels or 10m of pixels :smiley:

But yeah, we are willing to help you code but normally we don't write code. Because the idea of the forum is to learn :slight_smile:

As a starting point I would:

  • Grab a library like Bounce2 to handle the button (debounce and state change)
  • Grab FastLed for the ledstrip
  • Read about a state machine :slight_smile:
  • DON'T use delay anywhere :smiley:

septillion:
Ahh, 8 or 10 pixels isn't to bad indeed. I was in doubt if you meant 10 pixels or 10m of pixels :smiley:

But yeah, we are willing to help you code but normally we don't write code. Because the idea of the forum is to learn :slight_smile:

As a starting point I would:

  • Grab a library like Bounce2 to handle the button (debounce and state change)
  • Grab FastLed for the ledstrip
  • Read about a state machine :slight_smile:
  • DON'T use delay anywhere :smiley:

I agree, last I messed with arduino, LDR, relays and analog signals, I also used the forum in the way was it is intended.. :slight_smile:

This just need to be done in a hurry, more than I gonna think a lot about how to mess it together :slight_smile:

Thanks for the hints, I will see if I can get it to do what im looking for :wink:

Ow, and an extra tip, first try to make the whole 3 states frame (but only make three different LEDs light or something). After which you can post it here to get some tips :slight_smile:

Ive managed to understand how my 4 state state-machine is working...

Attached 3 LEDs, and one button, for each button press, another led turhs on, and 4th power all off, that seems pretty decent.

But, when I start to consider combining one state with the FastLED-Colorpalette, it gets messy..

I Dont have any clue how much if the code needed to be within setup, how much in the "loop" for the given state?

(Ofc I know, I need to delete my ledpin_out, and replace it with one pin) :wink:

Any know a tutorial telling a little about how to bring a state machine into some FastLED? :smiley: :smiley:

Septilion: What I can see, some small delay in the debounce routine is a good idea?

My code is like this:

#define button 3    //trykknap pin Digital pin 3
#define LED1 5      //LED1 på Digital pin 5
#define LED2 6      //LED2 på Digital pin 6
#define LED3 7      //LED3 på Digital pin 7

int state = 0;      //integrer til holde
int old = 0;        //integrer til huske sidste
int buttonPoll = 0; //integrer til huske nuværende stadie




void setup() {
  pinMode(button,INPUT);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);

  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);

}


void loop() {

      //debounce rutine
      buttonPoll = digitalRead(button);
      if(buttonPoll == 1) {
        delay(50);
        buttonPoll = digitalRead(button);
        if(buttonPoll == 0){
          state = old +1;
        }}

     else{
      delay(100);
        }
        switch (state) {

case 1:
          digitalWrite(LED1,HIGH);
          digitalWrite(LED2,LOW);
          digitalWrite(LED3,LOW);
          old = state;
          break;

case 2:
          digitalWrite(LED1,LOW);
          digitalWrite(LED2,HIGH);
          digitalWrite(LED3,LOW);
          old = state;
          break;

case 3:
          digitalWrite(LED1,LOW);
          digitalWrite(LED2,LOW);
          digitalWrite(LED3,HIGH);
          old = state;
          break;
          default: 
          digitalWrite(LED1,LOW);
          digitalWrite(LED2,LOW);
          digitalWrite(LED3,LOW);
          old = 0;
          break;
        }
}

FastLed are just some calls to set colors on your strip. So instead of turning a single led on you just call FastLed in a way that you have the right color.

But let's start off by posting the code you made so far :slight_smile:

Was adding it while you wrote :slight_smile:

While your at it, please change the post to use code tags :wink: But I already see you ignored my first advice :wink:

septillion:
While your at it, please change the post to use code tags :wink: But I already see you ignored my first advice :wink:

Hehe, you are right, i have some delay in the debounce, the tutorials I found mentioned it was a good idea to have a little delay here.. But you are sure I can go without it?

And tagged code, so its easier to read :slight_smile:

No, my first advice :wink:

septillion:

  • Grab a library like Bounce2 to handle the button (debounce and state change)

Does the work for you and doesn't use delay :wink:

And a tip, press ctrl + T in the IDE, looks better doesn't it? :wink:

And extra bonus tips:

  • Don't use macro's (#define) for pin definition
  • If you start numbering variables it's time for arrays
  • Don't forget to reset the state to the beginning again. Now you have to press the button an awful lot of times to turn it on again.

Agree the CTRL + T does a world of difference for readability for code, thanks! :slight_smile:

Well, I think my experience/logical understanding is big enough then...

If I use the "Bounce" example, I dont have any clue to tell what case/state it shall enter? only that its gonna be high or low (for one output, with either 1 or 0.... (basically)

void loop() {
  // Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  int value = debouncer.read();

  // Turn on or off the LED as determined by the state :
  if ( value == LOW ) {
    digitalWrite(LED_PIN, HIGH );
  } 
  else {
    digitalWrite(LED_PIN, LOW );
  }

}

'<-- I dont know where to tell what step to enter? :o

What I meant is that it takes care of the sate change of the button, not of your state machine :wink:

#include <Bounce2.h>

const byte ButtonPin = 3;
const byte NrStates = 3;
const byte LedPins[] = {5, 6};

Bounce button;
byte state;

void setup(){
  button.attach(ButtonPin, INPUT_PULLUP);
  pinMode(LedPins[0], OUTPUT);
  pinMode(LedPins[1], OUTPUT);
}

void loop(){
  updateState();
  
  switch(state){
    case 1:
      digitalWrite(LedPins[0], HIGH);
      digitalWrite(LedPins[1], LOW);
    break;
    case 2:
      digitalWrite(LedPins[0], LOW);
      digitalWrite(LedPins[1], HIGH);
    break;
    default:
      digitalWrite(LedPins[0], LOW);
      digitalWrite(LedPins[1], LOW);
    break;
  }
}

void updateState(){
  button.update();
  if(button.fell()){
    if(++state >= NrStates){
      state = 0;
    }
  }
}