I am trying to get my head around how to instantiate objects from existing libaries inside a class object. Specifically I am trying to get the bounce2.h debouncer working.
At the moment my code compiles but i think the debounce function is not working as the button is filling my array with lots of random numbers (even from setup) whereas it works fine on a non-class based sketch, so I think that I am probably not instantiating the bounce object correctly. If anyone can help I would be very grateful!
My header:
#ifndef CandleRack_h
#define CandleRack_h
class CandleRack
{
public:
CandleRack(byte candleRackStatus);
void begin();
void pushButton();
void selectRandomCandle();
void turnOnCandle(byte candlePosition);
void burnCandlesForMs();
void fadeOutCandle(byte candlePosition);
private:
Bounce _debouncePushButton;
byte _pushButtonPin;
byte _candleRackStatus;
byte _candlePosition;
byte _candleStatus[];
elapsedMillis _candleTimeElapsed;
unsigned int _candleOnForMs;
};
#endif
My cpp
#include "Arduino.h"
#include <FastLED.h>
#include <elapsedMillis.h>
#include <Bounce2.h>
#include "CandleRack.h"
#define NUM_LEDS 50
CandleRack::CandleRack(byte candleRackStatus)
{
_candleRackStatus = candleRackStatus;
}
void CandleRack::begin()
{
randomSeed(analogRead(0));
_debouncePushButton = Bounce();
_pushButtonPin = 3;
_candleOnForMs = 20000;
pinMode(_pushButtonPin, INPUT_PULLUP);
_debouncePushButton.attach(_pushButtonPin);
_debouncePushButton.interval(10);
for (byte b = 0; b < NUM_LEDS; b++)
{
_candleStatus[_candlePosition] = 0;
}
Serial.println("Candle Array: "); // Debug ************************
for (byte b = 0; b < NUM_LEDS; b++) // Debug ************************
{ // Debug ************************
Serial.print(_candleStatus[b]); // Debug ************************
Serial.print(", "); // Debug ************************
} // Debug ************************
Serial.println(""); // Debug ************************
Serial.println("Begin Complete"); // Debug ************************
}
void CandleRack::pushButton()
{
// Look for a button push
if (_debouncePushButton.update())
{
if (_debouncePushButton.fell())
{
selectRandomCandle();
}
}
}
void CandleRack::selectRandomCandle()
{
byte randomCandlePosition;
do
{
randomCandlePosition = random(0, NUM_LEDS);
}
while (_candleStatus[randomCandlePosition] != 0);
turnOnCandle(randomCandlePosition);
}
void CandleRack::turnOnCandle(byte _candlePosition)
{
// leds[_candlePosition] = CRGB::Red;
_candleStatus[_candlePosition] = 1;
Serial.println("Button Pushed: "); // Debug ************************
Serial.println(_candlePosition); // Debug ************************
Serial.println("Candle Array: "); // Debug ************************
for (byte b = 0; b < NUM_LEDS; b++) // Debug ************************
{ // Debug ************************
Serial.print(_candleStatus[b]); // Debug ************************
Serial.print(", "); // Debug ************************
} // Debug ************************
Serial.println(""); // Debug ************************
}
void CandleRack::burnCandlesForMs()
{
// See if it is time to turn the candle off
for (byte b = 0; b < NUM_LEDS; b++)
{
if (_candleTimeElapsed > _candleOnForMs && _candleStatus[b] == 1)
{
CandleRack::fadeOutCandle(b);
_candleTimeElapsed = 0;
}
}
}
void CandleRack::fadeOutCandle(byte _candlePosition)
{
_candleStatus[_candlePosition] = 0;
}
My sketch:
#include "Arduino.h"
#include <FastLED.h>
#include <elapsedMillis.h>
#include <Bounce2.h>
#include "CandleRack.h"
CandleRack MyCandleRack(0);
void setup() {
delay(3000); // sanity delay
Serial.begin(9600);
while (!Serial);
Serial.println("Setup Starts"); // Debug ************************
MyCandleRack.begin();
Serial.println("Setup Complate"); // Debug ************************
}
void loop() {
MyCandleRack.pushButton();
// MyCandleRack.burnCandlesForMs();
FastLED.show();
}
edit - added the code from the original debounce example for a single sketch
// Detect the falling edge
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce-Arduino-Wiring
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
int ledState = LOW;
// Instantiate a Bounce object :
Bounce debouncer = Bounce();
void setup() {
// Setup the button with an internal pull-up :
pinMode(BUTTON_PIN,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(500);
// Setup the LED :
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}
void loop() {
// Update the Bounce instance :
debouncer.update();
// Call code if Bounce fell (transition from HIGH to LOW) :
if ( debouncer.fell() ) {
// Toggle LED state :
ledState = !ledState;
digitalWrite(LED_PIN,ledState);
}
}