In an attempt at understanding OOP in Arduino/C++ better, I have been trying to write a program that checks whether two buttons are pressed and lights up their corresponding LEDs. However, halfway through my project I've been facing an issue where _lastButtonState and _lastLedOnTime of the Button class seem to be 0 at all times (see my comments in the code.) Why is this? Even right after setting the variable to HIGH, the serial monitor receives a 0 when I print it.
Please, post a sketch that at least gets compiled.
You have two buttons and two corresponding LEDs. The LEDs are expected to respond when the corresponding buttons are operated; that's your description.
Why have you included the Keyboard.h Library? What is the role of it here in your sketch.
The OP has said in his post that he wants to learn OOP (Object Oriented Programming/Class Structure Based Program) Programming in the Arduino MEGA Platform by operating 2 LEDs by 2 push buttons.
1. This is my imagination of his hardware setup.
Figure-1:
2. This is the non-Class Structure sketch to operate the LEDs of Fig-1; where, LED0 becomes ON and LED1 becomes OFF when K0 is pressed and LED1 becomes ON and LED0 becomes OFF when K1 is pressed.
3. The OP wants to transform the sketch of Step-2 into Class Structure based program. The Forum members may help him to achieve his goal either in correcting/modifying his posted sketch or in presenting a new sketch that will be simple and intuitive based on the standard procedures of creating class, object, methods etc.
4. The conceptual level of the Class Structured Program may look like this:
johnwasser:
Common mistake: '==' is the comparison operator. You want "_lastButtonState = HIGH;" because '=' is the assignment operator.
Oh, of course.. Thanks for pointing that out, I'm not even sure how I didn't spot that before.
GolamMostafa:
Please, post a sketch that at least gets compiled.
You have two buttons and two corresponding LEDs. The LEDs are expected to respond when the corresponding buttons are operated; that's your description.
Why have you included the Keyboard.h Library? What is the role of it here in your sketch.
I'm not sure why it doesn't compile for you? It works just fine on my machine.
Yes, that is indeed what I'm trying to accomplish.
I'm sorry if I wasn't clear in my original post, but I have not yet finished this project. I figured I didn't have to explain since it was probably unrelated to my error.
6v6gt:
Also, avoid doing this in the constructor:
pinMode(buttonPin, INPUT_PULLUP) ;
because the Arduino environment may not be stable at the time that the constructor is invoked.
Thank you for the tip, that actually makes a lot of sense. Would it be better to initialize my objects in setup()? That way it should be setting the pinmodes after the env has been fully initialized, correct?
DismissedGuy:
. . .
Thank you for the tip [arduino stuff in constructor], that actually makes a lot of sense. Would it be better to initialize my objects in setup()? That way it should be setting the pinmodes after the env has been fully initialized, correct?
Yes. That is it. The Arduino "standard" is to create a begin() method (as the example of Serial.begin() etc.) and call that from setup() for such initialisation.
6v6gt:
Yes. That is it. The Arduino "standard" is to create a begin() method (as the example of Serial.begin() etc.) and call that from setup() for such initialisation.
Ah alright, that actually looks a lot cleaner too. Thanks!