MTradioButton: Example 2
Event management for Uno, Nano, Mega

LW-MW-FM avec
with derivative class

This program will simulate an MW-LW-FM choice. We choose the range by wired buttons on A0, A1, and A2. The console indicates the changes. We will use an object derived to write only the actions of the buttons only once. It is also for educational purposes for show how to refer a button.

Complete program recommended

This program is completely under interruption, and releases loop which can be used to do something else.

// This program will simulate an MW-LW-FM choice. We choose the range by
// wired buttons on A0, A1, and A2. The console tells us about changes
// We will use a class derived from MtradioButton

#include <MTobjects.h> // V1.0.6 See http://arduino.dansetrad.fr/en/MTobjects

const uint8_t PIN_BUTTON_MW = A0; // Button wired between A0 and GND
const uint8_t PIN_BUTTON_LW = A1; // Button wired between A1 and GND
const uint8_t PIN_BUTTON_FM = A2; // Button wired between A2 and GND

// Definition of a new class that overloads onSelect and onUnselect
class MyButton: public MTradioButton
{
 public:
  MyButton (uint8_t pin, String p_name, byte value) // New constructor
    :MTradioButton(pin, NO_ACTION, NO_ACTION, HIGH if_not_pressed, value), name(p_name)
    {};
 private:
  String name; // Name that will be displayed on the console
  virtual void onSelect(void) // Overload function which informs the selection
  {
    Serial.print("We just chosen ");
    Serial.println(name);
  }
  virtual void onUnselect(void) // Function to be overloaded which informs deselection
  {
    Serial.print("We have just canceled ");
    Serial.println(name);
  }
};



// Implementation of the buttons
MyButton BoutonMW(PIN_BUTTON_MW, "MW", RADIO_BUTTON_0);
MyButton BoutonLW(PIN_BUTTON_LW, "LW", RADIO_BUTTON_1);
MyButton BoutonFM(PIN_BUTTON_FM, "FM", RADIO_BUTTON_2);



void setup()
{
  Serial.begin(115200);
 ButtonMW.select(); // So that there is at least one selected range
  delay(16 milli_seconds); // So we are sure that the button is selected (possible delay in taking into account 16ms)
}


String names[3] = {"MW", "LW", "FM"};

void loop()
{
  Serial.print("The current range is ");
  Serial.println(names[getMTradioButtonValue()]);
  delay(2000 milli_seconds); // To have a display that does not parade too quickly
}