MTobjets: Example 4
Event management for Uno, Nano, Mega
Lighting with a derivative class
Problem posed
See on a forum:
I want to pilot 8 static relay with 8 push buttons via an Arduino Mega card. The push buttons are provided with an LED. When I press on a button and release it, the corresponding relay closes, and the LED of the button lights up. When I push the same button, the relay opens and the LED of the button goes out. A ninth push button is used to extinguish all the relays that have been on.
Simulation
We will see how to realize:
I want to pilot N LEDs with N push buttons via a UNO, Nano or Mega card. When I press on a button and release it, the LED corresponding to the button lights up. When I press the same button, the corresponding LED goes out. A seventh push button is used to turn off all the LEDs that have been on.
If we define the buttons in a static way as it is done on the workshop Workshop lighting, we have a complete definition of a button + LED which was:
const uint8_t PIN_BUTTON_0 = A0; // Button wired between A0 and GND const uint8_t PIN_LED_0 = 3; // Led wired between 3 and GND void lightOn0(void) { digitalWrite(PIN_LED_0, HIGH); } void lightOff0(void) { digitalWrite(PIN_LED_0, LOW); } MTcheckButton Button0(PIN_BUTTON_0, lightOn0, lightOff0);
For N buttons, you will have to repeat this code N times. To lighten the code, a Zone object can be defined corresponding to a lighting Zone including the button and the LED. The Zones being defined during execution, you can use a Zone table and loops for implementation.
Complete program recommended
This program is completely under interruption, and releases loop which can be used to do something else.
// I want to pilot N LEDs with N push buttons via a UNO, Nano or Mega // card. When I press on a button and release it, the LED corresponding // to the button lights up. When I press the same button, the corresponding // LED goes out. A seventh push button is used to turn off all the LEDs that // have been on. #include <MTobjects.h> // V1.0.6 See http://arduino.dansetrad.fr/en/MTobjects const uint8_t N = 6; // Number of buttons const uint8_t PIN_BUTTON_ALL_OFF = 2; // Button wired between GND and 2 const uint8_t FIRST_BUTTON = 3; const uint8_t FIRST_LED = N+3; // With N=6, the last LED is 14 (we can also say A0) // For N=6 couples (button, LED) are: // (3,9) (4,10) (5,11) (6,12) (7,13) (8,A0) // The different zones (a button and an LED) // Creation of a specific object class Zone: public MTcheckButton { public: Zone(uint8_t pinButton, uint8_t pinLed) // New constructor :MTcheckButton(pinButton), led(pinLed) { pinMode(pinLed, OUTPUT); }; virtual void onSelect(void); // Overload function which informs the selection virtual void onUnselect(void); // Function to be overloaded which informs deselection private: uint8_t led; // LED pin corresponding to the button }; void Zone::onSelect(void) // Light on the led { digitalWrite(led, HIGH); } void Zone::onUnselect(void) // Light off the led { digitalWrite(led, LOW); } // Use of the pointers to be able to make an initialization loop Zone *Zones[N]; // Zone is therefore a table on Zone pointers // The button that turns off everything void lightOffAll(void) { for (byte i = 0; i < N; i++) Zones[i]->unselect(); } MTbutton ButtonlightOffAll(PIN_BUTTON_ALL_OFF, lightOffAll); void setup() { // Initialization of different zones: for (byte i = 0; i < N; i++) Zones[i] = new Zone(FIRST_BUTTON + i, FIRST_LED + i); } void loop(){}