<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[relays do not work using button]]></title><description><![CDATA[<p dir="auto">I have a working temperature compare node and I want to add2 relays with button. I am searching already for of few hours e why the relays are not working after using the buttons.<br />
Can anyone give me a tip ?</p>
<pre><code>#include &lt;MySensor.h&gt;
#include &lt;SPI.h&gt;
#include &lt;Bounce2.h&gt;
#include &lt;DallasTemperature.h&gt;
#include &lt;OneWire.h&gt;

// Partie Température
#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No

#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&amp;oneWire); // Pass the oneWire reference to Dallas Temperature. 
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
boolean receivedConfig = false;
boolean metric = true; 
// Initialize temperature message
MyMessage msg(0,V_TEMP);

// Partie Relay
#define RELAY_PIN  A1  // Arduino Digital I/O pinnumber for relay 
#define BUTTON_PIN  A3   // Arduino Digital I/O pin number for button 
#define CHILD_ID 17   // Id of the sensor child
#define RELAY_PIN_1  A2  // Arduino Digital I/O pinnumber for relay 
#define BUTTON_PIN_1  A4   // Arduino Digital I/O pin number for button 
#define CHILD_ID_1 18   // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0

Bounce debouncer = Bounce(); 
int oldValue=0;
bool state;


unsigned long previousMillis = 0;

MySensor gw;
MyMessage msg2(CHILD_ID,V_LIGHT);//relay_pin A1
MyMessage msg3(CHILD_ID,V_LIGHT);//relay_pin A2

void setup()  
{  
  // Startup up the OneWire library
  sensors.begin();
  // requestTemperatures() will not block current thread
  sensors.setWaitForConversion(false);

  gw.begin(incomingMessage, AUTO, true);

  // Send the sketch version information to the gateway and Controller
  gw.sendSketchInfo("Relay &amp; Button", "1.0");
  gw.sendSketchInfo("Temperature Sensor", "1.1");
  
    // Fetch the number of attached temperature sensors  
  numSensors = sensors.getDeviceCount();
  Serial.print(numSensors);
  Serial.println(" temperature sensors detected");
  
 // Setup the button
  pinMode(BUTTON_PIN,INPUT);
  // Activate internal pull-up
  digitalWrite(BUTTON_PIN,HIGH);

  pinMode(BUTTON_PIN_1,INPUT);
  // Activate internal pull-up
  digitalWrite(BUTTON_PIN_1,HIGH);
  
  // After setting up the button, setup debouncer
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5);

  debouncer.attach(BUTTON_PIN_1);
  debouncer.interval(5);

  // Present all sensors to controller
  for (int i=0; i&lt;numSensors &amp;&amp; i&lt;MAX_ATTACHED_DS18B20; i++) {   
     gw.present(i, S_TEMP);
      }
  // Register all sensors to gw (they will be created as child devices)
  gw.present(CHILD_ID, S_LIGHT);
  gw.present(CHILD_ID_1, S_LIGHT);

  // Make sure relays are off when starting up
  digitalWrite(RELAY_PIN, RELAY_OFF);
  // Then set relay pins in output mode
  pinMode(RELAY_PIN, OUTPUT);   

      digitalWrite(RELAY_PIN_1, RELAY_OFF);
  // Then set relay pins in output mode
  pinMode(RELAY_PIN_1, OUTPUT);   
      
  // Set relay to last known state (using eeprom storage) 
  state = gw.loadState(CHILD_ID);
  digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);

  state = gw.loadState(CHILD_ID);
  digitalWrite(RELAY_PIN_1, state?RELAY_ON:RELAY_OFF);
}


/*
*  Example on how to asynchronously check for new messages from gw
*/
void loop() 
{
  gw.process();

  // Relay button
  debouncer.update();
  // Get the update value
  int value = debouncer.read();
  if (value != oldValue &amp;&amp; value==0) {
      gw.send(msg2.set(state?false:true), true); // Send new state and request ack back
      gw.send(msg3.set(state?false:true), true);
  }
  oldValue = value;

 
  
  // Fetch temperatures from Dallas sensors
  sensors.requestTemperatures();

  // query conversion time and sleep until conversion completed
  //int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  unsigned long currentMillis = millis();
  // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  // gw.wait(conversionTime);

// Read temperatures and send them to controller 
  if (currentMillis - previousMillis &gt;= SLEEP_TIME) {
    // save the last time you read temperature
    previousMillis = currentMillis;
    for (int i=0; i&lt;numSensors &amp;&amp; i&lt;MAX_ATTACHED_DS18B20; i++) {
 
      // Fetch and round temperature to one decimal
      float temperature = static_cast&lt;float&gt;(static_cast&lt;int&gt;((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
   
      // Only send data if temperature has changed and no error
      #if COMPARE_TEMP == 1
      if (lastTemperature[i] != temperature &amp;&amp; temperature != -127.00 &amp;&amp; temperature != 85.00) {
      #else
      if (temperature != -127.00 &amp;&amp; temperature != 85.00) {
      #endif
   
        // Send in the new temperature
        gw.send(msg.setSensor(i).set(temperature,1));
        // Save new temperatures for next compare
        lastTemperature[i]=temperature;
        }
     }
  }
} 
 
void incomingMessage(const MyMessage &amp;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
     state = message.getBool();
     digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
     digitalWrite(RELAY_PIN_1, state?RELAY_ON:RELAY_OFF);
     // Store state in eeprom
     gw.saveState(CHILD_ID, state);
     gw.saveState(CHILD_ID_1, state);
    
     // Write some debug info
     Serial.print("Incoming change for sensor:");
     Serial.print(message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
   } 
   
}


Insert Code Here
</code></pre>
]]></description><link>https://forum.mysensors.org/topic/7086/relays-do-not-work-using-button</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 01:05:56 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/7086.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 30 Jun 2017 18:18:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to relays do not work using button on Fri, 30 Jun 2017 19:09:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mfalkvidd" aria-label="Profile: mfalkvidd">@<bdi>mfalkvidd</bdi></a><br />
you are right, i try to make one relay working.</p>
]]></description><link>https://forum.mysensors.org/post/70167</link><guid isPermaLink="true">https://forum.mysensors.org/post/70167</guid><dc:creator><![CDATA[Dick]]></dc:creator><pubDate>Fri, 30 Jun 2017 19:09:19 GMT</pubDate></item><item><title><![CDATA[Reply to relays do not work using button on Fri, 30 Jun 2017 19:07:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dick" aria-label="Profile: Dick">@<bdi>Dick</bdi></a> there are lots of more places that need update. Maybe you should try first to get one relay with one button working? Doing everything at once might be a too steep learning curve.</p>
]]></description><link>https://forum.mysensors.org/post/70166</link><guid isPermaLink="true">https://forum.mysensors.org/post/70166</guid><dc:creator><![CDATA[mfalkvidd]]></dc:creator><pubDate>Fri, 30 Jun 2017 19:07:33 GMT</pubDate></item><item><title><![CDATA[Reply to relays do not work using button on Fri, 30 Jun 2017 18:47:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mfalkvidd" aria-label="Profile: mfalkvidd">@<bdi>mfalkvidd</bdi></a><br />
Thanks for the info. I adjusted what you mentioned but still no reaction. this is what I have changed.</p>
<pre><code>InBounce debouncer = Bounce(); 
int oldValue=0;
bool state;

Bounce debouncer_1 = Bounce(); 
int oldValue_1=0;
bool state_1;


unsigned long previousMillis = 0;

MySensor gw;
MyMessage msg2(CHILD_ID,V_LIGHT);//relay_pin A1
MyMessage msg3(CHILD_ID_1,V_LIGHT);//relay_pin A2`</code></pre>
]]></description><link>https://forum.mysensors.org/post/70165</link><guid isPermaLink="true">https://forum.mysensors.org/post/70165</guid><dc:creator><![CDATA[Dick]]></dc:creator><pubDate>Fri, 30 Jun 2017 18:47:08 GMT</pubDate></item><item><title><![CDATA[Reply to relays do not work using button on Fri, 30 Jun 2017 18:40:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dick" aria-label="Profile: Dick">@<bdi>Dick</bdi></a> the same debouncer instance can't be used for two buttons. You need to create another instance.</p>
<p dir="auto">To stay with the naming convention in your sketch, create</p>
<pre><code>Bounce debouncer_1 = Bounce();
</code></pre>
<p dir="auto">and change all calls for the second button to use debouncer_1. You'll also need to use different child ids for the two messages.</p>
]]></description><link>https://forum.mysensors.org/post/70164</link><guid isPermaLink="true">https://forum.mysensors.org/post/70164</guid><dc:creator><![CDATA[mfalkvidd]]></dc:creator><pubDate>Fri, 30 Jun 2017 18:40:33 GMT</pubDate></item></channel></rss>