0

HELP with code for 2 Buttons and 1 Neopixel Strip
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

HELP with code for 2 Buttons and 1 Neopixel Strip

by gavinchilly on Wed May 29, 2019 2:04 am

Hi all!! Hope you can help me out. I'm by no means a coder, but I have played around with some code to 'get the jist' of what is going on. I THINK i have a fairly simple coding issue: How to get two buttons to control one set of Neopixels. NOTHING fancy at all!

The code I have now uses PIN 3 for the button and PIN 1 for the command to the neopixels. When the button goes to ground, the 8 neopixels will color swipe to one color, then to black. When it is hit again, it will go to another color, then to black. I have made a groovy setup for different swipes and Cases, but I NEED two buttons to control the action.
What it does Now:
Button 1 - color swipe blue, then swipe to black.
Button 1 - color swipe green, then swipe to black.

What I would Like:
Button 1 - color swipe blue, then swipe to black.
Button 2 - color swip green, then swipe to black.

I dont know enough code to be able to add in that second input.
I would think I need to define the other button, but how I dont know.

I'm willing to do my own research if I was given a push...Lord knows I've tried on arduino and adafruit forums and youtube and google..I.just.cant.get.it!

Here is what I have:
Code: Select all | TOGGLE FULL SIZE
// 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   3     
#define PIXEL_PIN    1   
#define PIXEL_COUNT 8

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 > 1)
        showType=0;
      startShow(showType);
    }
  }

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

void startShow(int i) {
  switch(i){
       case 0:
              //sub-name(starting LED, ending LED, color, wait??)
              colorWipe(0, 8, strip.Color(0, 255, 255), 10 ); //  Blue
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      case 1:
              colorWipe(0, 8, strip.Color(0, 255, 0), 10 ); // Green
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      }
}

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



Hope you all can help!!! thanks!!
gavin

gavinchilly
 
Posts: 8
Joined: Tue Sep 25, 2018 5:28 pm

Re: HELP with code for 2 Buttons and 1 Neopixel Strip

by franklin97355 on Wed May 29, 2019 2:22 am

You should check two buttons in the loop and do showtype 0 for one and showtype 1 for the other.

franklin97355
 
Posts: 21071
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: HELP with code for 2 Buttons and 1 Neopixel Strip

by gavinchilly on Wed May 29, 2019 2:52 am

Thanks for the info!
I THINK i added the two buttons in..but the question is what gets passed to startShow?
I was guessing a 0 or 1 based on my edits, but nope. this just makes case0 and case1 run one right after another.

Any help would be much appreciated!!

Code: Select all | TOGGLE FULL SIZE
// 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_PIN0   3     
#define BUTTON_PIN1   4
#define PIXEL_PIN    1   
#define PIXEL_COUNT 8

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType0 = 0;
int showType1 = 0;

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

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN0);
 
  // 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_PIN0);
    if (newState == LOW) {
      showType0++;
      if (showType0 > 1)
        showType0=0;
      startShow(showType0);
    }

//ADDED-Check in the other button
newState = digitalRead(BUTTON_PIN1);
    if (newState == LOW) {
      showType0++;
      if (showType0 > 1)
        showType1=1;
      startShow(showType1);
    }
//end - ADDED-Check in the other button
   
  }

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

void startShow(int i) {
  switch(i){

    if (i == 0) {
       case 0:
              //sub-name(starting LED, ending LED, color, wait??)
              colorWipe(0, 8, strip.Color(0, 255, 255), 10 ); //  Blue
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
       if (i == 1)     
      case 1:
              colorWipe(0, 8, strip.Color(0, 255, 0), 10 ); // Green
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      }
}
}


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

gavinchilly
 
Posts: 8
Joined: Tue Sep 25, 2018 5:28 pm

Re: HELP with code for 2 Buttons and 1 Neopixel Strip

by franklin97355 on Wed May 29, 2019 10:23 am

You seem to have vestages of your old code mixed in with the new. Try this:
Code: Select all | TOGGLE FULL SIZE
// 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_PIN0   3     
#define BUTTON_PIN1   4
#define PIXEL_PIN    1   
#define PIXEL_COUNT 8
#define showType0  0
#define showType1  1


Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;

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

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN0);
 
  // 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_PIN0);
    if (newState == LOW) {
      startShow(showType0);
    }

//ADDED-Check in the other button
  newState = digitalRead(BUTTON_PIN1);
  // 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_PIN1);
    if (newState == LOW) {
      startShow(showType1);
    }
//end - ADDED-Check in the other button
   
  }

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

void startShow(int i) {
  switch(i){

       case 0:
              //sub-name(starting LED, ending LED, color, wait??)
              colorWipe(0, 8, strip.Color(0, 255, 255), 10 ); //  Blue
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      case 1:
              colorWipe(0, 8, strip.Color(0, 255, 0), 10 ); // Green
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      }
}



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

franklin97355
 
Posts: 21071
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: HELP with code for 2 Buttons and 1 Neopixel Strip

by gavinchilly on Fri Jun 21, 2019 12:24 pm

Thanks so much again for looking at this!
I've finally gotten to the point where I'm going to put this into play!!

I pasted the full code you had in your last post into the Arduino IDE 1.8.9, hit the Verify button and get an error message that a "}" was missing. Fount it on line 39, added it and it verified!

I hooked up the Trinket and the neopixels (first testing them on a script I knew worked), uploaded the code and it does not trigger any lightshow. I reloaded the test script that has one pushbutton switch and verified that all my connections were good. Going back to the script for two pushbutton switches, and the neopixels are not triggered.

I made some edits to look like more like my test script and still nada.

I commented out the entire section for "BUTTON_PIN1" and tested and BUTTON_PIN0 works. It runs through both cases with every button push.
I took the comments out for the entire section for "BUTTON_PIN1" and tested and button0 and 1 do not work.

Not sure why adding that BUTTON_PIN1 is causing confusion.

Here is the code: (forgive the gratuitous comments..i was trying to figure out what was going on and THANK YOU for all of your assistance!!)

Code: Select all | TOGGLE FULL SIZE
// 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_PIN0   3     
#define BUTTON_PIN1   4
#define PIXEL_PIN    2   
#define PIXEL_COUNT 8
//#define showType0  0
//#define showType1  1


Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType0 = 0; //!!!!!!
int showType1 = 1; //!!!!!!

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

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN0);
 
  // 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_PIN0);
    if (newState == LOW) {
      showType0++; //!!!!!!!!!!!!!!!!!!!!!
      if (showType0 > 1)//!!!!!!!!!!!!!!!!!!!!!
        showType0=0;//!!!!!!!!!!!!!!!!!!!!!
      startShow(showType0);
    }
  }

//ADDED-Check in the other button
  newState = digitalRead(BUTTON_PIN1);
 // 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_PIN1);
    if (newState == LOW) {
      showType1++; //!!!!!!!!!!!!!!!!!!!!!
      if (showType1 > 1)//!!!!!!!!!!!!!!!!!!!!!
        showType1=0;//!!!!!!!!!!!!!!!!!!!!!
      startShow(showType1);
    }
  }
//end - ADDED-Check in the other button
  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {
  switch(i){

       case 0:
              //sub-name(starting LED, ending LED, color, wait??)
              colorWipe(0, 8, strip.Color(0, 255, 255), 10 ); //  Blue
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      case 1:
              colorWipe(0, 8, strip.Color(0, 255, 0), 10 ); // Green
              colorWipe(0, 8, strip.Color(0, 0, 0), 10 ); // off/black
            break;
      }
}



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

gavinchilly
 
Posts: 8
Joined: Tue Sep 25, 2018 5:28 pm

Please be positive and constructive with your questions and comments.