Button Toggle Library

May someone, please, help with my code? I need the code to be able to toggle a regular push button with an LED, but I am wanting to create a library for this to be as simple as possible. I am also using another library called Bounce2 (credit to the person who made it - Bounce2 Library). How do I make the code be able to do what I want it to do? Thanks in advance. Code is in the file below. The hardware does work, by the way, because I already did a test on this, but the code was different.

ButtonToggleLibrary.zip (1.22 KB)

UptownKitten:
May someone, please, help with my code? I need the code to be able to toggle a regular push button with an LED, but I am wanting to create a library for this to be as simple as possible. I am also using another library called Bounce2 (credit to the person who made it - Bounce2 Library). How do I make the code be able to do what I want it to do? Thanks in advance. Code is in the file below. The hardware does work, by the way, because I already did a test on this, but the code was different.

Lets tighten up the definitions of what you want to do.

A toggle means the device stays in last position you moved it to. As in TOGGLE SWITCH.

A regular push button switch is push on and it goes off when released. I have some push button switches that do toggle, in that they stay on when pushed and need to be pushed again to turn off.

What purpose do you think a library code will serve in your program that is better than just coding the logic yourself?

Paul

I think this example is a good mix between readability and usability:

Pieter

Like I said I THINK that the library will simplify the code, I know what a toggle switch is as well. I am trying to use a joystick basically, like a pushbutton, which is a momentary switch. I am not using a toggle switch, I am trying to use a momentary switch to make a toggle switch in programming, writing a library to simplify things even more than what it already is.

UptownKitten:
Like I said I THINK that the library will simplify the code, I know what a toggle switch is as well. I am trying to use a joystick basically, like a pushbutton, which is a momentary switch. I am not using a toggle switch, I am trying to use a momentary switch to make a toggle switch in programming, writing a library to simplify things even more than what it already is.

Let's face reality for a bit. If you don't know how to program the Arduino to do what you want, how can you possible think of creating a library. Are you going to be using that library in a bunch of new programs?

Paul

Have you written non library code that does what you want ?

Yes, Paul, I will be using the library in other programs. And, yes, I have written non library code that does what I want.

yes, I have written non library code that does what I want.

That's a good start but it is at odds with what you said earlier in the thread

Have you looked at Library tutorial

Yes, I have looked at the library tutorial, I still couldn't figure out what was wrong. I am not getting any errors either.

Please, someone help me with my code.

Post your code, not a .zip file.

Not a lot of people are going to open a .zip from an unknown source.

This part is for the main loop part:

#include "ButtonToggleLibrary.h"

Button button(36);

void setup() {
}

void loop() {
  button.buttonToggle();
}

This is the .cpp part or actual code part for the library:

#include <Bounce2.h>
#include "Arduino.h"
#include "ButtonToggleLibrary.h"

boolean ledState = false;

#define LED_PIN 13

Bounce debouncer = Bounce(); // Instantiate a Bounce object

Button::Button(byte pin){
  pinMode(pin, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  debouncer.interval(25); // Use a debounce interval of 25 milliseconds
}


void Button::buttonToggle() {

  debouncer.update(); // Update the Bounce instance

  if ( debouncer.fell() ) {  // Call code if button transitions from HIGH to LOW
    ledState = !ledState; // Toggle LED state
    digitalWrite(LED_PIN, ledState); // Apply new LED state
  }
}

Here is the header file or class part:

/*
 ButtonToggleLibrary.h - Library for toggling button easier.
 Created by UptownKitten453, October 5, 2019.
 Released into the public domain.
 */

#ifndef BTL_h  //if listed twice skip to #endif
#define BTL_h  //define our libary

#include "Arduino.h" //include the Arduino library for helping out making the library

class Button{
  // Everything about how a button works.
  public:
  Button(byte pin);  //Constructor
  void buttonToggle(); // Method
};


#endif

The point of object oriented programming is that each object has its own member variables.

In your code, "debouncer" is a global object. Move it into the class declaration, as a member variable.

Also, you can only have one instance of your class, because the LED pin is hard-coded. Make it an argument to your constructor as well.

Also, don't put any hardware interacting code (e.g. pinMode) in your constructor. Put it in a begin method instead and call it from setup.

I still don't get what I did wrong, none of that made any sense to me. Why do I have to put the pinMode in a begin method instead and call it from setup? Why can't I have global variables?

Why do I have to put the pinMode in a begin method instead and call it from setup?

As part of its startup behind the scenes the Arduino needs to initialise the hardware associated with the pins. If you use pinMode() in the constructor of your class there is a chance that it runs before the hardware is fully initialised, hence the advice to put it in setup() where it will not be called until the hardware initialisation is complete behind the scenes