Bonjour à tous je suis tous fraîchement inscrit sur ce forum car le forum officiel est passer en lecture seul, enfin bref mon soucis, c’est la programmation d’un sketch mysensor : relais avec boutons .
Le but de mon sketch est de faire un multi relais avec bouton poussoir manuel pour chaque relais, j’ai réussi à rajouter les relais et les bouton mais quand j’appuie sur l’un ou l’autre des boutons, c’est les deux relais qui se ferme .
Pourriez-vous m’aider a comprendre ou est mon erreur ?
PS : j’avoue que je n’est pas beaucoup de compétence en programmation arduino.
merci d’avance
Voici le sketch :
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
// Enabled repeater feature for this node
#define MY_REPEATER_FEATURE
#include <MySensors.h>
#include <Bounce2.h>
#define RELAY1_PIN 5 // Arduino Digital I/O pin number for relay
#define RELAY2_PIN 6 // Arduino Digital I/O pin number for relay
#define BUTTON1_PIN 3 // Arduino Digital I/O pin number for button
#define BUTTON2_PIN 4 // Arduino Digital I/O pin number for button
#define CHILD1_ID 1 // Id of the sensor child
#define CHILD2_ID 2 // Id of the sensor child
#define RELAY1_ON 1
#define RELAY2_ON 1
#define RELAY1_OFF 0
#define RELAY2_OFF 0
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
int oldValue1=0;
int oldValue2=0;
bool state1;
bool state2;
MyMessage msg1(CHILD1_ID,V_LIGHT);
MyMessage msg2(CHILD2_ID,V_LIGHT);
void setup()
{
// Setup the button
pinMode(BUTTON1_PIN,INPUT);
pinMode(BUTTON2_PIN,INPUT);
// Activate internal pull-up
digitalWrite(BUTTON1_PIN,HIGH);
digitalWrite(BUTTON2_PIN,HIGH);
// After setting up the button, setup debouncer
debouncer1.attach(BUTTON1_PIN);
debouncer2.attach(BUTTON2_PIN);
debouncer1.interval(5);
debouncer2.interval(5);
// Make sure relays are off when starting up
digitalWrite(RELAY1_PIN, RELAY1_OFF);
digitalWrite(RELAY2_PIN, RELAY2_OFF);
// Then set relay pins in output mode
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
// Set relay to last known state (using eeprom storage)
state1 = loadState(CHILD1_ID);
state2 = loadState(CHILD2_ID);
digitalWrite(RELAY1_PIN, state1?RELAY1_ON:RELAY1_OFF);
digitalWrite(RELAY2_PIN, state2?RELAY2_ON:RELAY2_OFF);
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo(« Relay & Button », « 1.0 »);
// Register all sensors to gw (they will be created as child devices)
present(CHILD1_ID, S_LIGHT);
present(CHILD2_ID, S_LIGHT);
}
/*
- Example on how to asynchronously check for new messages from gw
*/
void loop()
{
debouncer1.update();
debouncer2.update();
// Get the update value
int value1 = debouncer1.read();
int value2 = debouncer2.read();
if (value1 != oldValue1 && value1==0) {
send(msg1.set(state1?false:true), true); // Send new state and request ack back
}
if (value2 != oldValue2 && value2==0)
{
send(msg2.set(state2?false:true), true); // Send new state and request ack back
}
oldValue1 = value1;
oldValue2 = value2;
}
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.isAck()) {
Serial.println("This is an ack from gateway");
}
if (message.type == V_LIGHT) {
// Change relay state
state1 = message.getBool();
state2 = message.getBool();
digitalWrite(RELAY1_PIN, state1?RELAY1_ON:RELAY1_OFF);
digitalWrite(RELAY2_PIN, state2?RELAY2_ON:RELAY2_OFF);
// Store state in eeprom
saveState(CHILD1_ID, state1);
saveState(CHILD2_ID, state2);
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}