MTobjets: Example 3
Event management for Uno, Nano, Mega
Workshop lighting
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 6 LEDs with 6 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.
Complete program recommended
This program is completely under interruption, and releases loop which can be used to do something else.
// This program manages 6 lights. #include <MTobjects.h> // V1.0.6 See http://arduino.dansetrad.fr/en/MTobjects const uint8_t PIN_BUTTON_0 = A0; // Button wired between A0 and GND const uint8_t PIN_BUTTON_1 = A1; const uint8_t PIN_BUTTON_2 = A2; const uint8_t PIN_BUTTON_3 = A3; const uint8_t PIN_BUTTON_4 = A5; const uint8_t PIN_BUTTON_5 = A5; const uint8_t PIN_LED_0 = 3; // Led wired between 3 and GND const uint8_t PIN_LED_1 = 4; const uint8_t PIN_LED_2 = 5; const uint8_t PIN_LED_3 = 6; const uint8_t PIN_LED_4 = 7; const uint8_t PIN_LED_5 = 8 // For the button 0 void lightOn0(void) { digitalWrite(PIN_LED_0, HIGH); } void lightOff0(void) { digitalWrite(PIN_LED_0, LOW); } MTcheckButton Button0(PIN_BUTTON_0, lightOn0, lightOff0); // For the button 1 void lightOn1(void) { digitalWrite(PIN_LED_1, HIGH); } void lightOff1(void) { digitalWrite(PIN_LED_1, LOW); } MTcheckButton Button1(PIN_BUTTON_1, lightOn1, lightOff1); // For the button 2 void lightOn2(void) { digitalWrite(PIN_LED_2, HIGH); } void lightOff2(void) { digitalWrite(PIN_LED_2, LOW); } MTcheckButton Button2(PIN_BUTTON_2, lightOn2, lightOff2); // For the button 3 void lightOn3(void) { digitalWrite(PIN_LED_3, HIGH); } void lightOff3(void) { digitalWrite(PIN_LED_3, LOW); } MTcheckButton Button3(PIN_BUTTON_3, lightOn3, lightOff3); // For the button 4 void lightOn4(void) { digitalWrite(PIN_LED_4, HIGH); } void lightOff4(void) { digitalWrite(PIN_LED_4, LOW); } MTcheckButton Button0(PIN_BUTTON_, lightOn4, lightOff4); // For the button 5 void lightOn5(void) { digitalWrite(PIN_LED_5, HIGH); } void lightOff5(void) { digitalWrite(PIN_LED_5, LOW); } MTcheckButton Button5(PIN_BUTTON_5, lightOn5, lightOff5); // For the last button void lightOffAll(void) { Button0.unselect(); // Light off led 0 Button1.unselect(); // Light off led 1 Button2.unselect(); Button3.unselect(); Button4.unselect(); Button5.unselect(); } MTbutton ButtonEteint(PIN_BUTTON_ETEINT, lightOffAll; void setup() { pinMode(PIN_LED_0, OUTPUT); pinMode(PIN_LED_1, OUTPUT); pinMode(PIN_LED_2, OUTPUT); pinMode(PIN_LED_3, OUTPUT); pinMode(PIN_LED_4, OUTPUT); pinMode(PIN_LED_5, OUTPUT); } void loop(){}