Just finished a prototype for a Turn Sequence Counter for Arkham Horror LCG by Fantasy Flight and I have completed the soldering and wiring to take this project to the WS2812b level instead of boring LED's. Need some help with the Coding. Not sure how to get buttons to turn on the 2812 led's one at a time and they stay on until another button is pressed. I have 3 buttons wired in and need them to control 2 strands of ws2812b rbg's. I have attached a pic of the wiring and both 4 light strips have been tested.
I have some code that worked led's but now I need to convert it to work with the RGB's. Coding is not my strong suit so any help will be much appreciated. Here is my code so far.
#include <Adafruit_NeoPixel.h>
#define PIN1 5
#define PIN2 3
#define NUM_LEDS 3
#define NUM_LEDS2 3
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUM_LEDS, PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(NUM_LEDS2, PIN2, NEO_GRB + NEO_KHZ800);
class PushButton
{
public:
PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
: pin(pin) { // remember the push button pin
pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state = digitalRead(pin); // read the button's state
int8_t stateChange = state - previousState; // calculate the state change since last time
if (stateChange == falling) { // If the button is pressed (went from high to low)
if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange == rising) { // if the button is released or bounces
previousBounceTime = millis(); // remember when this happened
}
previousState = state; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin;
bool previousState = HIGH;
unsigned long previousBounceTime = 0;
const static unsigned long debounceTime = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
// this is for led's looks like ill need to change this for RGB's
const uint8_t ledPins[] = {5, 3};
const uint8_t nb_leds = sizeof(ledPins) / sizeof(ledPins[0]);
PushButton pushbutton = {6};
class PushButton2
{
public:
PushButton2(uint8_t pin2) // Constructor (executes when a PushButton object is created)
: pin2(pin2) { // remember the push button pin
pinMode(pin2, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state2 = digitalRead(pin2); // read the button's state
int8_t stateChange2 = state2 - previousState2; // calculate the state change since last time
if (stateChange2 == falling) { // If the button is pressed (went from high to low)
if (millis() - previousBounceTime2 > debounceTime2) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange2 == rising) { // if the button is released or bounces
previousBounceTime2 = millis(); // remember when this happened
}
previousState2 = state2; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin2;
bool previousState2 = HIGH;
unsigned long previousBounceTime2 = 0;
const static unsigned long debounceTime2 = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
PushButton2 pushbutton2 = {7};
class PushButton3
{
public:
PushButton3(uint8_t pin3) // Constructor (executes when a PushButton object is created)
: pin3(pin3) { // remember the push button pin
pinMode(pin3, INPUT_PULLUP); // enable the internal pull-up resistor
};
bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
{
bool pressed = false;
bool state3 = digitalRead(pin3); // read the button's state
int8_t stateChange3 = state3 - previousState3; // calculate the state change since last time
if (stateChange3 == falling) { // If the button is pressed (went from high to low)
if (millis() - previousBounceTime3 > debounceTime3) { // check if the time since the last bounce is higher than the threshold
pressed = true; // the button is pressed
}
}
if (stateChange3 == rising) { // if the button is released or bounces
previousBounceTime3 = millis(); // remember when this happened
}
previousState3 = state3; // remember the current state
return pressed; // return true if the button was pressed and didn't bounce
};
private:
uint8_t pin3;
bool previousState3 = HIGH;
unsigned long previousBounceTime3 = 0;
const static unsigned long debounceTime3 = 25;
const static int8_t rising = HIGH - LOW;
const static int8_t falling = LOW - HIGH;
};
PushButton3 pushbutton3 = {9};
void setup() {
strip1.begin();
strip1.show(); // Initialize all pixels to 'off'
strip2.begin();
strip2.show(); // Initialize all pixels to 'off'
for (const uint8_t &ledPin : ledPins)
pinMode(ledPin, OUTPUT);
}
void loop() {
static uint8_t nb_presses = 0;
if (pushbutton.isPressed()) {
if (nb_presses == nb_leds) {
for (const uint8_t &ledPin : ledPins)
digitalWrite(ledPin, LOW);
nb_presses = 0;
} else {
digitalWrite(ledPins[nb_presses], HIGH);
nb_presses++;
}
}
if (pushbutton2.isPressed()){
clearRound();
}
}
void clearRound(){
digitalWrite (8, LOW);
digitalWrite (5, LOW);
digitalWrite (4, LOW);
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip1.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
This project is to help keep track of the Phases of a Round and the amount of actions a player has during the investigation phase of The Arkham Horror LCG, game is amazing but can be a chore with out a tracker, I have a strictly hard wired tracker that uses SPST switches to control each led no arduino and it has made the game much more enjoyable because it forces you to follow turn sequence correctly. This RBG model will be more intuitive and better looking.