Found this code for 1 button and would like help on making it work for 2 buttons with 2 different RGB strips, plus can't figure out how to make half of the LEDS red and half green. total new to arduino been trying to find help for days and I just cant seem to find it elsewhere, half of the Leds will be inside door 1 and half outside for button 1 and same scenario for door 2.
this is the code I found that works for 1 button
#include <Adafruit_NeoPixel.h>
#define BTN_A 12 // Digital IO pin connected to the button. This will be
//#define BTN_B 13 // driven with a pull-up resistor so the switch should
// pull the pin to ground momentarily. On a high -> low
// transition the button press logic will execute.
#define PIXEL_PIN 15 // Digital IO pin connected to the NeoPixels.
// will put pin 2 here
#define PIXEL_COUNT 24 // number of neopixel (change this accordingly)
// wonder if pixel count can be for both Pixel units
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
bool oldState = HIGH;
int showType = 0;
void setup() {
pinMode(BTN_A, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Get current button state.
bool newState = digitalRead(BTN_A);
// 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(BTN_A);
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: colorWipe(strip.Color(255, 0, 0), 25); // Red
break;
case 1: colorWipe(strip.Color(0, 255, 0), 25); // Green
break;
//...
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Rest of code not needed for this project..
tried copy and paste changed btn #, maybe copied it wrong, will try again if i get a chance.
will post updated when i get it done and see where i went wrong.
#include <Adafruit_NeoPixel.h>
#define BTN_A 12 //orange// Digital IO pin connected to the button. This will be
#define BTN_B 14 //RED// driven with a pull-up resistor so the switch should
// pull the pin to ground momentarily. On a high -> low
// transition the button press logic will execute.
#define PIXEL_PIN_A 15 // Digital IO pin connected to the NeoPixels.
//#define PIXEL_PIN_B 15 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 16 // number of neopixel (change this accordingly)
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN_A, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN_A, NEO_GRB + NEO_KHZ800);
//Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN_B, NEO_GRB + NEO_KHZ800);
bool oldState = HIGH;
int showType = 0;
void setup() {
pinMode(BTN_A, INPUT_PULLUP);//orange
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(BTN_B, INPUT_PULLUP);//red
strip2.begin();
strip2.show(); // Initialize all pixels to 'off'
}
void loop() {
// Get current button state.
bool newState = digitalRead(BTN_A);//orange
// 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(BTN_A);
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: colorWipe(strip.Color(255, 0, 0), 25); // Red
break;
case 1: colorWipe(strip.Color(0, 255, 0), 25); // Green
break;
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
// Get current button state.
bool newState = digitalRead(BTN_B);
// 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(BTN_B);
if (newState == LOW) {
showType++;
if (showType > 1)
showType=0;
startShow2(showType);
}
}
// Set the last button state to the old state.
oldState = newState;
}
void startShow2(int i) {
switch(i){
case 0: colorWipe(strip.Color(0, 0, 255), 25); // BLUE
break;
case 1: colorWipe(strip.Color(255, 55, 0), 25); // ORANGE
break;
}
}
// Fill the dots one after the other with a color
//void colorWipe(uint32_t c, uint8_t wait) {
// for(uint16_t i=0; i<strip2.numPixels(); i++) {
// strip2.setPixelColor(i, c);
// strip2.show();
// delay(wait);
//
//}
now i have to hold btn2 and then btn1 and it will goe throug both loops with out stopping
just btn2 alone does nothing
and still not figued out how to display half red-half green, then switch to half green-half red
thanks for the help so far, guess i have not learned enough the short periods i have to work on this and reasearch.
You need to remember the old state of both your buttons so you will need oldStateBtn1 and oldStateBtn2 in your code. They way you have it now, pressing button 1 influences your logic for detecting button 2 and a press. You should not be checking the state of button 2 inside your colorWipe() function, it should be in loop() just like your button 1 code.
You also asked about making half the strip red and half green. Your current colorWipe() function cycles through all your leds, setting them to the same color. You would have to change that function to pass in 2 colors or create a new function that takes the two colors your want. Something like this:
void colorWipe(uint32_t c1, uint32_t c2, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels() / 2; i++) {
strip.setPixelColor(i, c1);
}
for (uint16_t i = strip.numPixles() / 2; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c2);
}
strip.show();
delay(wait);
}
}
and then change your calls to colorWipe() to include the second color. If you want them all the same color, pass the same value to both colors.
colorWipe(strip.Color(255, 0, 0), strip.Color(0, 255, 0), 25); // Red + green
And finally, if you want both your buttons to be independent, then you will have to get rid of those delay() calls since the processor does nothing while they are happening. Look at the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay) to see how to do it. Based on your current level of understanding, that might be a bit difficult and something you save for a later day.