📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Attention all domestic customers! Please place UPS Ground and USPS First Class & Priority orders by 11am ET Monday December 9, 2019 📬❄️📦 Attention all international customers! Please place all UPS WORLDWIDE EXPRESS; SAVER, and DHL EXPRESS WORLDWIDE orders by 11am ET Monday December 9, 2019 📬❄️📦
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.

starting and stopping led strip with a button Pro-trinket

by jonascale85 on Thu Aug 22, 2019 12:29 pm

ok in a nut shell, I currently have 2 ledstrips (WS2812B) hooked up to an adafruit protrinket. This works with the current version of the code that i'm running.

I would like to beable to start and stop the animation on the lights by pressing a button.

So far i cant figure out how to do this, im mostly sure that my hardware is setup properly so i guessing im doing something wrong in my code.

anyone think that they can point me in the right direction??

thanks

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>


#define pinStrip1 7
#define pinStrip2 8
#define pinButton 2

boolean on=true;
int buttonState = 0;

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

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

void loop()
{
  buttonState = digitalRead(pinButton);
  if (buttonState == HIGH)
  {   
    if(on==true)
   {
         on=false;
       }else{
         on=true;
       }
  }
 
   if (on == true)
   {   
   runAnimation();  //call start funtion
     }else{
   StopAnimation(); //call stop funtion
     }
 
   delay(500);

}
 
static 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);
  }
}

static 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);
  }
}
 
static void runAnimation()
{
chase1(Strip1.Color(255,0,0));
chase2(Strip2.Color(255,0,0));
}
static void StopAnimation()
{
Strip1.clear();
Strip2.clear();
}
 

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 Thu Aug 22, 2019 1:14 pm

At a quick glance your button logic looks ok.

I believe you need a strip1.show(); command after the strip1.clear(); and the same for strip2. The .clear() just sets the memory you still need to use .show() to send the data out to the pixels.

Fellow hobbyist
Keith

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

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

by jonascale85 on Fri Aug 23, 2019 11:00 am

have added the strip.show() still having the problem

created the circuit on a tinkercad

still not working

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 Fri Aug 23, 2019 12:04 pm

A couple of things...

First you have INPUT_PULLUP for the button pinMode in your code but you have the button wired with a pull down resistor. You should not use both. Since you have it wired that way and the rest of the code is looking for HIGH I would just delete the pinMode line altogether. Input is the startup state for the digital pins.

Second you say the Neopixels work when coded by themselves. With the new code do the Neopixels work and just do not go off? Your code will have to run through both animations between button checks when runAnimation(); is active (about 900 ms total) but when StopAnimation(); is active it runs in a few milliseconds. So the chances of your switch registering on a stop press and not still registering in the 500ms after the clear() may be slim. So the stop function runs but the run function will run again because the button is still down. For a quick test of this you could add a 1000 or 2000 ms to the loop delay.

A better solution would be to use state machine code for the button presses. That way the code would only respond once for each button press. Here is a modification of your code. It compiles but is not tested.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>


#define pinStrip1 7
#define pinStrip2 8
#define pinButton 2

boolean on = true;
bool buttonState = HIGH;
bool oldState = HIGH;

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

void setup()
{
  Strip2.begin();
  Strip1.begin();
}

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

  delay(500);

}

static 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);
  }
}

static 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);
  }
}

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


Keith
Last edited by kcl1s on Fri Aug 23, 2019 12:58 pm, edited 1 time in total.

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

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

by jonascale85 on Fri Aug 23, 2019 12:53 pm

thanks keith,

ill have to give this code a try,

i'm new to electronics and i'm filling my way thru by watching tutorials and trying to learn what i need.

i used the input_pull because i didn't think it needed a resistor and for my purposes i would rather not have one if i can.
I did add one in my design but that was just to see if i needed one after all as it still wasn't working i figured, why not.
I was following the build of the superhero power gauntlet here on adafruit

the diaogram shows that only 2 pins on the button that are used
leds_Super_hero_glove.png
leds_Super_hero_glove.png (409.09 KiB) Viewed 164 times

gnd and the 5 pin
below is the link for the project

https://learn.adafruit.com/superhero-power-gauntlet?view=all

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 Fri Aug 23, 2019 1:10 pm

I generally use INPUT_PULLUP for buttons to make wiring easier. Just remember that the logic is inverted when using it. (button pressed = LOW, button unpressed = HIGH).

Keith

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

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

by jonascale85 on Fri Aug 23, 2019 3:54 pm

kcl1s wrote:I generally use INPUT_PULLUP for buttons to make wiring easier. Just remember that the logic is inverted when using it. (button pressed = LOW, button unpressed = HIGH).

Keith


So then if i apply this to the new code then i want to change everything to be inverted?

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>


#define pinStrip1 7
#define pinStrip2 8
#define pinButton 2

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

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

void setup()
{
  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   
    delay(20); //debounce delay
  }
  oldState = buttonState;
//-------------------------------end state code----------------
  if (on == true)
  {
    runAnimation();  //call start funtion
  } else {
    StopAnimation(); //call stop funtion
  }

  delay(500);

}

static 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);
  }
}

static 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);
  }
}

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

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 Fri Aug 23, 2019 6:14 pm

Yes. Don't forget to add INPUT_PULLUP

Keith

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

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

by jonascale85 on Fri Aug 23, 2019 8:03 pm

Thanks man,

One last question, if I’m putting the pullup back in the code would it still be in the setup position of the code?

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 Sat Aug 24, 2019 6:09 am

Yes in setup

pinMode(pinButton, INPUT_PULLUP);

Keith

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

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

by jonascale85 on Sat Aug 24, 2019 1:23 pm

Sweet!

Thanks Keith

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 Sat Aug 24, 2019 4:12 pm

keith,

i thought that i had reversed the logic correctly but it still isn't working

do you see somthing that im missing
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>


#define pinStrip1 7
#define pinStrip2 8
#define pinButton 2

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

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

void setup()
{
  pinMode(pinButton, INPUT_PULLUP);
  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   
    delay(20); //debounce delay
  }
  oldState = buttonState;
//-------------------------------end state code----------------
  if (on == true)
  {
    runAnimation();  //call start funtion
  } else {
    StopAnimation(); //call stop funtion
  }

  delay(500);

}

static 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);
  }
}

static 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);
  }
}

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

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 Sat Aug 24, 2019 10:07 pm

i thought that i had reversed the logic correctly but it still isn't working

What is it doing? No pixels on or just does not turn off when button pressed?

Also I have never used static before void in functions. I understand basically what it does but maybe not fully clear what it does under the hood. I do not think they are helping here and may be causing some trouble. I would try taking them out. void runAnimation() instead of static void runAnimation() for all the functions.

Another thing to help debug your code is to put some serial prints in but I see you are using the Pro Trinket and I don't believe it supports serial through the USB like a regular Uno does. This is unfortunate. You may want to get a different controller for testing maybe Adafruit Metro 328 https://www.adafruit.com/product/2488 or other Uno like board.

An alternate way to to check button operation is to add this line
digitalWrite(13, on); //toggle the onboard led with the 'on' variable after this line on = !on; //toggle the on variable You will also need to add this line in setup pinMode(13, OUTPUT);

Keith

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

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

by kcl1s on Sat Aug 24, 2019 10:54 pm

I just wired this up and it works with your last code. Because of the delay you have to hold the button for a while.

I just thought of something some of my students had problems with. Make sure you are wiring across the button contacts. On those 4 pin buttons there are 2 pins tied together on each side of the switch. You can use a multimeter to test for continuity when pressed and open when unpressed. Another trick is to wire to diagonal terminals. That will insure you are wired on both sides of the switch contacts.

Keith

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

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

by jonascale85 on Sat Aug 24, 2019 11:09 pm

Keith,

I’m traveling at the moment so the only way I’ve been able to test the code is using the tinkercad simulator, and on there it doesn’t seem to be working, but I will be home tomorrow morning. I will wire it up with real hard ware and see if that makes a difference. Thanks for all the help. I really appreciate it!

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

Please be positive and constructive with your questions and comments.