Independent neopixel with independent buttons , single Arduino nano

Read through your post a few minutes ago and was editing previous code but going to try this code right now.

  • Another test question.
    Watch the heartbeat LED, LED L on the Arduino PCB, while you push the different switches.
    When you reach 10 presses on a switch, what happens with the L LED ? :thinking:

On the 10th press it stops blinking and stays lit , on the next press it goes back to blinking.

Is there anyway that I could make the rainbow keep going until another button press?

  • First things first, do you know why the heartbeat LED stops toggling ?
    Explain ?

  • If you were to include a heartbeat LED in your future projects, what benefit would you get from it ?

  • Why does the heartbeat LED toggle ? :roll_eyes:

Ok I'm going to give this a try
Its stops toggling because it has come to the end of the "cases or mode count" ?

Benefit : I guess it could prove that everything in the code has been executed if it stays lit?

It toggles to show that the board is working or that the code is running?

Code in Post #15 under "using arrays"... the five buttons can independently move from color to color until the 10th press (of one button), at which point that LED/button cycles through the rainbow three times, and all button presses are ignored. To make it cycles "forever" would block buttons "forever".... unless you got into "button interrupts"... not difficult, give it a try.

Ok I'm going to give this a try
Its stops toggling because it has come to the end of the "cases or mode count" ?
It stops toggling because somewhere code is executing in a closed loop situation.
i.e. when code executes in a loop over and over again there is no way for the heartbeat LED code to execute, subsequently toggling the L LED stops. :cry:

Benefit : I guess it could prove that everything in the code has been executed if it stays lit?
Several benefits: if toggle stops or the normal toggling rhythm changes it tells us there is code blocking happening somewhere in our sketch.
We can use the toggling LED to give us feedback that code is executing; when we have a project with no lights, sounds etc. How can we be sure anything is happening ? :thinking:
It acts as a dynamic pilot light like the pilot light on a stove element.
loop() runs over and over again forever. We strive for this to happen at a fast rate so our code remains responsive.

It toggles to show that the board is working or that the code is running?
As long as the loop() function continues to cycle, the non-blocking heartbeat TIMER (made with the help of the millis() function) is check to see if it has expired. When it does expire, we examine the state of the heartbeat LED and write the opposite state back to it. We toggle the LED.

Ok I've found attach interrupt and I know it uses pin 2 on the Nano.

I know it uses rising and falling depending on whether button is pulled high or low.
So I would add something like this

attachInterrupt(0,ButtonPressed,RISING)

I'm pretty sure "ButtonPressed" should be changed to one of the variables in my code : "newState" or "OldState" ?

I should then also remove the (20) from " case 10: rainbow(i, 20); break; "

  • Again, let's take this one step at a time.
    When the rainbow effect is finished, the LED in question stays ON and is RED.
    How would you modify the sketch so after the LED animation is finished, the LED goes to Black ? :thinking:

Follow this...

It does go back to black as in case 0

  • In post #20, when the rainbow effect is finished, the LED in question stays ON and is RED.
    How would you modify the sketch so after the LED animation is finished, the LED goes to Black ? :thinking:

would strip.clear help?

void rainbow(byte i, uint8_t wait)
{
  for (uint16_t j = 0; j < 256; j++)
  {
    strip.setPixelColor(i, Wheel(j));
    strip.show();          // update strip each step
    delay(wait);
//strip.clear();
  }
  • Try it to see what happens.

It didn't work :face_with_peeking_eye:

I did it!!!

void rainbow(byte i, uint8_t wait)
{
  for (uint16_t j = 0; j < 256; j++)
  {
    strip.setPixelColor(i, Wheel(j));
    strip.show();          // update strip each step
    delay(wait);
    //strip.clear();
  }

  for(int i=0; i<strip.numPixels(); i++) {
  strip.setPixelColor(i, strip.Color(0, 0, 0)); // Set to black
}
strip.show();

So this basically runs after "Rainbow" has completed and changes the color to black and its placed outside our loop

I almost had it , it now turns all 5 neopixels off

  • You are doing well, isn't software fun ? :smiling_face_with_sunglasses:

  • Try this change:

//                                        r a i n b o w ( )
//================================================^================================================
//
void rainbow(byte i, uint8_t wait)
{
  for (uint16_t j = 0; j < 256; j++)
  {
    strip.setPixelColor(i, Wheel(j));
    //Update strip each step.
    strip.show();
    delay(wait);
  }

  //Turn OFF the pixel.
  setPixelColor(i, 0, 0, 0);                     // <------<<<<<<
  strip.show();                                  // <------<<<<<<

} //END of   rainbow()

That works :grinning_face:
I was missing the Pixel Index.

This is very very different from my day job but its fun. The last time I looked at any code was 25 years ago! Now I need to learn how to keep the rainbow on till a button interrupt.