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.
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?
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?
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
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