Hello lovely people!
Bigos here.
I would like to know your or generally known methods of interacting with objects in terms of OOP. (Of course, I'm not talking about ready copy-paste code.) I hope my question doesn't sound weird I'm just an arduino enthusiast, so please forgive me if that doesn't have any sense
. Of course, I'm not talking about ready-made copy-paste code.
I have wrote two libraries.
The first one allows the tact-switch button to be used as toggle switch. The "toggled" function shown in the code below returns true when button is pushed and released, and false when button is not pushed or is held. So the true state is just a pulse. I think this library works very fine as for a novice coder.
The second library adds two functions for LEDs. You can make an object that can blink (you can set the High and Low durations) or fade (you can set speed).
What I'm trying to do is to interact with a button on led's behavior. I want to turn blinking On and of with my virtual toggle switch. I have created what I think is called a machine state (?). The 'state' variable changes when button is toggled. When the 'state' is TRUE, the blinking goes on, when 'state' is false the led is off. This works just fine i think but my intuition tells me that this is not a common way to do that. Shouldn't the object (LED) be shut down or destroyed?
So my question is:
What are the methods of interacting with objects in terms of OOP? What approach is more professional to that? In the next step, I would like to use my toggle button to change functions like skipping melodies, changing pre-set LED sequences etc.
Any concepts or ideas are welcome.
If you would like to see the whole of the libraries I'd be happy to show it.
The code below shows my approach to objects interaction.
#include <Arduino.h> //wrote on VSCode
#include <button.h> //button library
#include <BLINK.h> //LEDs library
bool led1State = false; //LEDs machine state is false for the beginning
const int button1pin = 2;
const int led1pin = 0;
const int buzzer1pin = 1; //additional buzzer, works the same as LED
Button button1(button1pin); //button object
BLINK led1(led1pin); //LED object
BLINK buzzer1(buzzer1pin); //buzzer object
void setup()
{
pinMode(button1pin, INPUT);
pinMode(led1pin, OUTPUT);
pinMode(buzzer1pin, OUTPUT);
}
void loop()
{
if(button1.toggled()) //toggled function returns true or false
{
led1State = !led1State;}
if(led1State)
{
led1.ledblink(50, 2000); //blinking function (HIGH duration, LOW duration)
buzzer1.ledblink(50,2000);
}
if(!led1State){ //This is what turns LEDs blinking off. Im not sure if this is common way to do this...
digitalWrite(led1pin,led1State);
digitalWrite(buzzer1pin,led1State);
}
}