0

starting and stopping led strip with a button Pro-trinket
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: starting and stopping led strip with a button Pro-trinke

by jonascale85 on Sun Aug 25, 2019 1:29 pm

OK IM real close
new code
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>

#define pinStrip1 5
#define pinStrip2 6
#define pinButton 9

boolean on = false;
bool buttonState = LOW;
bool oldState = LOW;

Adafruit_NeoPixel Strip1 = Adafruit_NeoPixel(144, pinStrip1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel Strip2 = Adafruit_NeoPixel(144, pinStrip2, NEO_GRB + NEO_KHZ800);

void setup()
{
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  Strip2.begin();
  Strip1.begin();
}

void loop()
{
//-------------------------begin state code--------------------
  buttonState = digitalRead(pinButton);
  if (buttonState == LOW && oldState == HIGH) {
    //you have a new press
    on = !on;    //toggle the on variaable
    digitalWrite(13, on); //toggle the onboard led with the 'on' variable   
    delay(20); //debounce delay
  }
  oldState = buttonState;
//-------------------------------end state code----------------
  if (on == true)
  {
    runAnimation();  //call start funtion
  } else {
    StopAnimation(); //call stop funtion
  }

  delay(500);

}

void chase1(uint32_t c) {
  for (uint16_t i = 0; i < Strip1.numPixels() + 4; i++) {
    Strip1.setPixelColor(i  , c); // Draw new pixel
    Strip1.setPixelColor(i - 4, 0); // Erase pixel a few steps back
    Strip1.show();
    delay(25);
  }
}

void chase2(uint32_t c) {
  for (uint16_t i = 0; i < Strip2.numPixels() + 4; i++) {
    Strip2.setPixelColor(i  , c); // Draw new pixel
    Strip2.setPixelColor(i - 4, 0); // Erase pixel a few steps back
    Strip2.show();
    delay(25);
  }
}

void runAnimation()
{
  chase1(Strip1.Color(255, 0, 0));
  chase2(Strip2.Color(255, 0, 0));
}
void StopAnimation()
{
  Strip1.clear();
  Strip1.show();
  Strip2.clear();
  Strip2.show();
}


I tried it and nothing happened so i cross linked the wires on the switch on the breadboard
i can turn it on but it wont turn off. The LED13 on the protrinket does turn on, but wont turn off so it seems that its toggling on but not off.

any ideas what i should look for?

jonascale85
 
Posts: 11
Joined: Thu Aug 22, 2019 12:14 pm

Re: starting and stopping led strip with a button Pro-trinke

by jonascale85 on Sun Aug 25, 2019 1:30 pm

ok nm what i just said i was holding down the button long enough

jonascale85
 
Posts: 11
Joined: Thu Aug 22, 2019 12:14 pm

Re: starting and stopping led strip with a button Pro-trinke

by jonascale85 on Sun Aug 25, 2019 2:13 pm

So its kinda working but is there a way to reduce the amount of time to hold down the button? Ive been playing the delay but not to much success

jonascale85
 
Posts: 11
Joined: Thu Aug 22, 2019 12:14 pm

Re: starting and stopping led strip with a button Pro-trinke

by kcl1s on Sun Aug 25, 2019 3:29 pm

Your animation has delay in the for loops so you are getting 25 ms x 8 pixels x 2 strips = about 400 ms between button checks even if you removed the delay in the void loop section. Without going deeper in the code this is as good as it gets. You can look at pin interrupts or multitasking for more advanced ways of dealing with it.
Part 1 https://learn.adafruit.com/multi-tasking-the-arduino-part-1
Part 2 https://learn.adafruit.com/multi-tasking-the-arduino-part-2
Part 3 https://learn.adafruit.com/multi-tasking-the-arduino-part-3

They are definitely harder concepts to grasp.

Keith

kcl1s
 
Posts: 1337
Joined: Tue Aug 30, 2016 12:06 pm

Please be positive and constructive with your questions and comments.