Help required using libraries inside classes

Status
Not open for further replies.

Robhilken

Member
Hi,

I am really struggling with instantiating objects inside other objects in general but in particular I am trying to get the bounce.h and FastLED libraries working.

I'm starting with the bounce.h library as I feel it should be straight forward.

Candlerack.h

Code:
/*
  ========== CandleRack.h ============
  Candle Rack to hold animated Candles with
  Pushbuttons to operate. Rob Hilken, March 2016
*/

// ensure this library description is only included once
#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 _previousState;
    byte _candleRackStatus;    
    byte _candlePosition;
    byte _candleStatus[];
    elapsedMillis _candleTimeElapsed;
    unsigned int _candleOnForMs;
};
#endif

cpp file

Code:
/*
  ========== CandleRack.h ============
  Candle Rack to hold animated Candles with
  Pushbuttons to operate. Rob Hilken, March 2016
*/

#include "Arduino.h"
#include <FastLED.h>
#include <elapsedMillis.h>
#include <Bounce.h>
#include "CandleRack.h"

#define NUM_LEDS 50

CandleRack::CandleRack(byte candleRackStatus)
{
  _candleRackStatus = candleRackStatus;
}

void CandleRack::begin()
{ 
  randomSeed(analogRead(0));

 // FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
 // FastLED.setBrightness( BRIGHTNESS );
 // fill_solid(leds, NUM_LEDS, CRGB::Blue);
 
  _pushButtonPin = 3;
  _debouncePushButton = Bounce(_pushButtonPin, 20); 
  _previousState = HIGH; 
  _candleOnForMs = 20000;
  pinMode(_pushButtonPin, INPUT_PULLUP);

  Serial.println("Begin Complete");  // Debug ************************
}


void CandleRack::pushButton()
{
  // Look for a button push
  if (_debouncePushButton.update())
  {
    if (_debouncePushButton.fallingEdge())
    {
      selectRandomCandle();
    }
  }
}


void CandleRack::selectRandomCandle()
{
  byte randomCandlePosition;
  do
  {
    randomCandlePosition = random(0, NUM_LEDS);
  }
  while (_candleStatus[randomCandlePosition] != 0);
  
  Serial.print("Button Pushed: ");  // Debug ************************
  Serial.println(randomCandlePosition);  // Debug ************************
  
  turnOnCandle(randomCandlePosition);
}


void CandleRack::turnOnCandle(byte _candlePosition)
{
  // leds[_candlePosition] = CRGB::Red;
  _candleStatus[_candlePosition] = 1;
}


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)
{
 // leds[_candlePosition] = CRGB::Black;
  _candleStatus[_candlePosition] = 0;
}

sketch:

Code:
/*
  ========== Votive Gambler ============
  Votive Candle Donations Box
  Interactive audio visual sculpture
  by Rob Hilken, March 2016
*/

#include "Arduino.h"
#include <FastLED.h>
#include <elapsedMillis.h>
#include <Bounce.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();
}

Error:

Code:
Arduino: 1.6.7 (Windows 10), TD: 1.27, Board: "Teensy 3.2 / 3.1, Serial, 96 MHz optimized (overclock), US English"

In file included from C:\Users\jay-jay\AppData\Local\Temp\builddda6eeb7b86b477f5785b2788d5e8008.tmp\sketch\CandleRack.cpp:8:0:
C:\Users\jay-jay\Documents\Dropbox\Arduino\libraries\FastLED-3.1.0/FastLED.h:12:2: warning: #warning FastLED version 3.001.000 (Not really a warning, just telling you here.) [-Wcpp]
#warning FastLED version 3.001.000  (Not really a warning, just telling you here.)
^
C:\Users\jay-jay\AppData\Local\Temp\builddda6eeb7b86b477f5785b2788d5e8008.tmp\sketch\CandleRack.cpp: In member function 'void CandleRack::begin()':
CandleRack.cpp:29: error: invalid use of member function (did you forget the '()' ?)
_debouncePushButton = Bounce(_pushButtonPin, 20);
^
C:\Users\jay-jay\AppData\Local\Temp\builddda6eeb7b86b477f5785b2788d5e8008.tmp\sketch\CandleRack.cpp: In member function 'void CandleRack::pushButton()':
CandleRack.cpp:41: error: '((CandleRack*)this)->CandleRack::_debouncePushButton' does not have class type
if (_debouncePushButton.update())
^
CandleRack.cpp:43: error: '((CandleRack*)this)->CandleRack::_debouncePushButton' does not have class type
if (_debouncePushButton.fallingEdge())
^
exit status 1
'((CandleRack*)this)->CandleRack::_debouncePushButton' does not have class type

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

If I can get some help with this I think it will help with a few other problems I am having...

Using FastLED in a library seems to be a bigegr challenge which I'll leave for now :)

Thanks for an help or guidance!

Rob
 
Hi, I sorry I realise that this wasn't teensy related and I think I have received other good advice that will help me understand how to use pointers to solve this. Many thanks, Rob
 
Try using Bounce2.h instead of Bounce.h.

Teensyduino 1.28-beta1 has Bounce2. Or you can download it from github.

Thanks Paul. It was actually using Bounce2.h that was the breakthrough (although there were several of my own mistakes at the root of the problems I was having). Having a simplified interface really helps us newbies :)
 
Status
Not open for further replies.
Back
Top