Field value seems to stop working.

I had a sketch that worked, and decided to Object-Orient it.

So, I create abstractions for an RGB-LED, and a Button, and a (abstract) ButtonListener and a concrete CButtonListener

Click the button, it notifies the ButtonListener and the CButtonListener implementation toggles the LED. That’s the plan!

I’ll post abbreviated code below, but basically:

  1. the constructor for the CButtonListener can flash the LED
  2. the constructor for the Button can convince the Listener to flash the LED
  3. actual button events do not flash the LED

I’ve verified:

  • The Startup Sequence includes a WHITE (CButtonListener.constructor), RED(Button.constructor), GREEN(Button.constructor) sequence
  • The CButtonListener receives the event, as the LED_BUILTIN toggles

I’m including all the code needed to run this and demonstrate the problem, but I think you should be able to ignore most of it.

I think the fact that the LED_BUILTIN toggles on button presses should let you ignore the Button class fully and accept that it calls ‘_listener.stateChanged(_buttonState, durationSinceButtonChange);’ with changing HIGH and LOW values for _buttonState.

(I say that not understanding why it’s not working, so clearly could be wrong).

I’m pasting this, the CButtonListener code, as it’s my most likely candidate for the bug. (this is also in the attachments)

#include "Arduino.h"
#include "CButtonListener.h"
#include "ButtonListener.h"
#include "ColorLed.h"

CButtonListener::CButtonListener(ColorLed *led) : _colorLed(*led), ButtonListener()
    _colorLed.setColor("WHITE"); // This works (I guess technically only 'once' since it's in the constructor)
    delay(500); // Let's see the 'white' for a bit.

void CButtonListener::stateChanged(int state, long durationInPreviousState)
    digitalWrite(LED_BUILTIN, state); // This works, gets toggled every time I press the button. So we're getting here.
    if (state != HIGH)
      _colorLed.setColor("GREEN"); // Works exactly once, in the call from the 'Button' constructor, but not in actual button press-events
      _colorLed.setColor("RED"); // Works exactly once, in the call from the 'Button' constructor, but not in actual button press-events
    delay(500); // Delays all over the place for debugging to ensure I'm not missing a transitory on, followed by something turning them off again.

Thanks in advance!

Caveat: It’s been a long time since I wrote any flavor of c++, but I write Java every day, so feel free to get technical if it helps.
Hardware: Arduino Nano 33 BLE

ArduinoQuestion.ino (352 Bytes)

Button.cpp (1.25 KB)

Button.h (1.41 KB)

ButtonListener.cpp (235 Bytes)

ButtonListener.h (316 Bytes)

CButtonListener.cpp (560 Bytes)

CButtonListener.h (458 Bytes)

ColorLed.cpp (2.09 KB)

ColorLed.h (829 Bytes)

Does setColor really accept a string?

Does setColor really accept a string?

It does, a hand enumerated set of Strings. (I abstracted that for my own reading convenience - see ColorLed.cpp for details, but suffice to say that part works - I may switch to an enum in the future if that's a thing in c++, but one hurdle at a time. :slight_smile: )

I still haven't figured this out. :frowning:

I have Serial working fine, so is there maybe something I could be printing out to debug this?