MTradioButton
Event management for Uno, Nano, Mega
Pimples type buttons, it activates when you press it. It deactivates if you press a button from the same group.
Groups and values
By default:
- there are 8 groups maximum and they are numbered from 0 to 7.
- there may be maximum 16 buttons per group.
- each button can have a value (identifier) freely chosen between 0 and 15.
We can change these default values if:
- we want more than 8 groups
or
- we want more than 16 switches in one of the groups.
In the library file MTobjects.h, the first lines indicate:
#define RADIO_NB_BITS_GROUPE 3 // Number of bits for the definition of the radio group 0..6 // 0: 1 group of 128 buttons maximum // 1: 2 groups maximum of 64 buttons max each // 2: 4 groups maximum of 32 buttons max each // 3: 8 groups maximum of 16 buttons max each // 4: 16 groups maximum of8 buttons max each // 5: 32 groups maximum of 4 buttons max each // 6: 64 pairs of buttons maximum
We can change the number 3 in red above to change the default values. Be careful this only indicates the maximums. If we only need 2 buttons in all (so in the same group) we can replace number 3 in red with any number between 0 and 6. In this case, we do not touch anything. Note that as much as possible you can have 128 buttons, but that you can only have 70 pins with a MEGA. Nevertheless if a button was to belong to several groups, it would be necessary to define it several times with the same pin number, but with different group numbers.
For the first 4 groups, instead of using a number, we can use:
RADIO_GROUP_0 RADIO_GROUP_1 RADIO_GROUP_2 RADIO_GROUP_3
And for the first 10 buttons, we can use:
RADIO_BUTTON_0 RADIO_BUTTON_1 RADIO_BUTTON_2 RADIO_BUTTON_3 RADIO_BUTTON_4 RADIO_BUTTON_5 RADIO_BUTTON_6 RADIO_BUTTON_7 RADIO_BUTTON_8 RADIO_BUTTON_9
It is equivalent but it automatically comments on the number. Instead of writing:
MTradioButton button(0, one, two, 0, 1, 2);
It is better to write:
MTradioButton Button(pinOn, lightOn, lightOff, LOW if_not_pressed, RADIO_BUTTON_1, RADIO_GROUP_2);
By default when you omit to specify the group, it is group 0 which is implemented. In the case we have only one group, we may not specify the group number.
Include to add
#include <MTobjects.h> // See http://arduino.dansetrad.fr/en/MTobjects
I strongly advise to leave the comment, if another person wanted to try the program, they would have a download link directly. This is valid for all the libraries you use.
Useful comment
if_not_pressed
This comment is a word that can be added anywhere and which is ignored by the compiler.
Constructor
MTradioButton(pin, onSelectFunction = NO_ACTION, onUnselectFunction = NO_ACTION, rest = HIGH, valeur = 0, groupe = 0);
- pin (uint8_t): pin to which the button is connected
- onSelectFunction: what happens when you activate the button
- NO_ACTION -> nothing happens
- myFunction → call a function "void myFunction(void) {...}"
- onUnselectFunction: what happens when the button is desactived
- NO_ACTION -> nothing happens
- myFunction → call a function "void myFunction(void) {...}"
- rest (boolean): pin state if the button is release
- HIGH -> if it is wired between GND and pin (recommended). The pullup resistor is automatically added
- LOW -> if it is wired between VCC and pin, with a pulldown resistance between pin and GND
- valeur (byte): arbitrary number associated with the button. This allows you to differentiate it in a group; 0 to 15
- groupe (byte): group number 0..7
Useful functions
void select(void); void unselect(void); boolean getSelect(void); virtual void onSelect(void); virtual void onUnselect(void);
select: select the button
unselect: unselect the button
getSelect: return true if the button is selected
onSelect: overload function called when pressing the button
onUnselect: overload function called when the button is unselected
Useful global functions
(not belonging to the object)
void unselectMTradioButton(char groupe = 0); void unselectMTradioButton(); byte getMTradioButtonValeur(char groupe = 0); byte getMTradioButtonValeur();
unselectMTradioButton: deselects all the radios buttons of a group
getMTradioButtonValue: Returns the value of active control of the group
Bonus functions
byte getValeur(); byte getGroupe(); void setOnSelectFunction(onSelectFunction); void setOnSelectFunction(); void setOnUnselectFunction(onUnselectFunction); void setOnUnselectFunction();
getValeur: return the value of the button
getGroupe: return the button group number
setOnSelectFunction: changes the function called when pressing the button
setOnSelectFunction: without parameters removes the function called when pressing the button
setOnUnselectFunction: changes the function called when the button is unselected
setOnUnselectFunction: without parameters removes the function called when the button is unselected
Examples