Unbounce a button and object oriented programming

First post here, so hi to everyone

I recently started to play with Arduino for two main reasons: learn/refresh electronics skills and to learn C. In the last years I've been mostly coding in Python so I think it's better if I push myself into a completely new field where I can't have the temptation to go back to a language I already know.

My first project is a circuit that "accumulates" button presses and then sends key presses at certain fixed rate. The button will trigger a LED blink. Easy, but I'm trying to do it with a bit of object oriented programming that I can use in other projects.

I created one class for the LEDs that I can initialize with the pin number and a blink delay so I can control their brightness (I quickly discovered that if you simply switch on-off the LEDs the amount of light perceived by the eyes is too low. So, the way to periodically call that blinking led could something as simple as:

#include "ledslib.h"

// Variables initialization
Led o_led(8, 30);  // Led connected to pin 8 that will blink for 30 ms

// Setup code
void setup() {
  o_led.init();
}

// Main loop
void loop() {
  o_led.blink();
  delay(500);

  // Update of all the "automatic" objects in the program
  o_led.update();
}

Now comes the tricky part... I want to create a Button class in a similar manner. It should be able to store:

  1. a Led instance (so I can link the button to a blinking LED).
  2. a function (so I can trigger any function I want when I press the button).

So far, my non-working Button class looks like:

class Button {
  unsigned long i_start = 0;  // Time when the button was pressed the last time
  int i_delay = 100;          // Minimum time between two consecutive presses
  int i_pin = 0;              // Pin associated to the button
  bool b_on = false;          // The internal state of the button
  int *i_func;                // The function that will be executed by the button
  int *i_led;                 // Led pointer that will blink when the button is pressed
  
  public:
  Button(int pi_pin, int pi_delay, int pi_led) {
    i_pin = pi_pin;
    i_delay = pi_delay;
    i_led = pi_led;
    }

  void init() {
    pinMode(i_pin, INPUT); 
  }
  
  void press() {
    if (!b_on) {
      b_on = true;
      i_start = millis();
      if (i_led != 0) {
        *i_led.blink();
        }
    }
  }
};

The problem is I don't know how to properly do both things. I've been reading about how to store pointers/references to other class instances or functions but I'm really confused and not making any progress. Examples of the things I've trying are:

class Button {
  ...
  int i_func = 0;  // The function that will be executed by the button
  Led *o_led;      // Led pointer that will blink when the button is pressed
  
  public:
  Button(int pi_pin, int pi_delay, int *po_led) {
    o_led = po_led;
    }

  void press() {
    if (!b_on) {
      b_on = true;
      if (o_led != NULL) {
        o_led.blink();
        }
    }
  }
};

Any help or guidance will be more than welcome. Please remember I have a background in Python so basic stuff like getting/storing/using pointers/references from classes/instances/functions is quite overwhelming at the moment.
Regards

You can refer to this button library

Thanks. That would help me to avoid rewriting the debounce code but I still suffer the "linking" of the led and functions into a Button class.

Check this:

Many thanks, aarg

Now it starts to make sense. I haven't had time yet to try all the changes required but at least now I'm able to initialise the Button class and the compiler doesn't complain.

For completess, here is the partial code:

class Button {
  unsigned long i_start = 0;  // Time when the button was pressed the last time
  bool b_on = false;          // Internal state of the button
  int i_delay = 100;          // Minimum time betwe two consecutive presses
  int i_pin = 0;              // Pin associated to the button
  Led *o_led;                 // Led pointer that will blink when the button is pressed
  
  public:
  Button(int pi_pin, int pi_delay, Led &po_led) {
    i_pin = pi_pin;
    i_delay = pi_delay;
    o_led = &po_led;
    }

  void press() {
    if (!b_on) {
      b_on = true;
      i_start = millis();
      if (o_led != NULL) {
        o_led->blink();
        }
    }
  }
};

Then I just have to create a Led object and link it to a Button object like:

Led o_led(7, 30);                // pin, delay
Button o_button(8, 100, o_led);  // pin, delay, led object

Regards

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.