Binary switch not working
-
Can anybody help me out. I have a working combination node with 2 temp sens0rs, button and relais. it works fine but I needed a binary switch in it. I added the switch to the code but it will not work. I think the problem is in the "LOOP" part. I am always strugling with the {}. Perhaps somebode can take a look in the code and what I added to the working code has the comment "//radar". pls let me know.
#include <MySensor.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> // 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 = 900000; // 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(&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_ON 1 #define RELAY_OFF 0 #define CHILD_ID1 5 //radar #define BUTTON_PIN1 A5 //radar Bounce debouncerA3 = Bounce(); int oldValueA3 = 0; bool state; unsigned long previousMillis = 0; MySensor gw; MyMessage msg2(CHILD_ID, V_LIGHT); Bounce debouncerA5 = Bounce(); //radar int oldValueA5 = -1; //radar MyMessage msgA5(CHILD_ID, V_TRIPPED); void setup() { // Startup up the OneWire library sensors.begin(); // requestTemperatures() will not block current thread sensors.setWaitForConversion(false); pinMode(BUTTON_PIN1, INPUT); //radar digitalWrite(BUTTON_PIN1, HIGH); //radar debouncerA5.attach(BUTTON_PIN1); //radar debouncerA5.interval(5); //radar gw.present(CHILD_ID1, S_DOOR); //radar gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Relay & 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); // After setting up the button, setup debouncer debouncerA3.attach(BUTTON_PIN); debouncerA3.interval(5); // Present all sensors to controller for (int i = 0; i < numSensors && i < 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); // Make sure relays are off when starting up digitalWrite(RELAY_PIN, RELAY_OFF); // Then set relay pins in output mode pinMode(RELAY_PIN, OUTPUT); // Set relay to last known state (using eeprom storage) state = gw.loadState(CHILD_ID); digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); } } void loop() { gw.process(); // Relay button debouncerA3.update(); // Get the update value int value = debouncerA3.read(); if (value != oldValueA3 && value == 0) { gw.send(msg2.set(state ? false : true), true); // Send new state and request ack back } oldValueA3 = 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 >= SLEEP_TIME) { // save the last time you read temperature previousMillis = currentMillis; for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((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 && temperature != -127.00 && temperature != 85.00) { #else if (temperature != -127.00 && 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; } debouncerA5.update(); //radar // Get the update value int value = debouncerA5.read(); //radar if (value != oldValueA5) //radar { // Send in the new value gw.send(msg.set(value == HIGH ? 1 : 0)); //radar oldValueA5 = value; //radar } } } } void incomingMessage(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 state = message.getBool(); digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); // Store state in eeprom gw.saveState(CHILD_ID, state); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }
-
@Dick You are checking the button only after the sensor is read. Put it before or after temperature reading... i.e.
// put you button (//radar) code here if (currentMillis - previousMillis >= SLEEP_TIME) {
-
@AWI I changed it and compiling worked good. But what I noticed that if i connect the A5 (Binary pin) to - I see no Send led on my nano. Perhaps another issue? al the other sensors are working still but only the binary switch Not.
Any idea?#include <MySensor.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> // 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 = 900000; // 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(&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_ON 1 #define RELAY_OFF 0 #define CHILD_ID1 5 //radar #define BUTTON_PIN1 A5 //radar Bounce debouncerA3 = Bounce(); int oldValueA3 = 0; bool state; unsigned long previousMillis = 0; MySensor gw; MyMessage msg2(CHILD_ID, V_LIGHT); Bounce debouncerA5 = Bounce(); //radar int oldValueA5 = -1; //radar MyMessage msgA5(CHILD_ID, V_TRIPPED); void setup() { // Startup up the OneWire library sensors.begin(); // requestTemperatures() will not block current thread sensors.setWaitForConversion(false); pinMode(BUTTON_PIN1, INPUT); //radar digitalWrite(BUTTON_PIN1, HIGH); //radar debouncerA5.attach(BUTTON_PIN1); //radar debouncerA5.interval(5); //radar gw.present(CHILD_ID1, S_DOOR); //radar gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("Relay & 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); // After setting up the button, setup debouncer debouncerA3.attach(BUTTON_PIN); debouncerA3.interval(5); // Present all sensors to controller for (int i = 0; i < numSensors && i < 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); // Make sure relays are off when starting up digitalWrite(RELAY_PIN, RELAY_OFF); // Then set relay pins in output mode pinMode(RELAY_PIN, OUTPUT); // Set relay to last known state (using eeprom storage) state = gw.loadState(CHILD_ID); digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); } } /* Example on how to asynchronously check for new messages from gw */ void loop() { gw.process(); debouncerA5.update(); //radar // Get the update value int value = debouncerA5.read(); //radar if (value != oldValueA5) //radar { // Send in the new value gw.send(msg.set(value == HIGH ? 1 : 0)); //radar oldValueA5 = value; //radar // Relay button debouncerA3.update(); // Get the update value int value = debouncerA3.read(); if (value != oldValueA3 && value == 0) { gw.send(msg2.set(state ? false : true), true); // Send new state and request ack back } oldValueA3 = 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 >= SLEEP_TIME) { // save the last time you read temperature previousMillis = currentMillis; for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((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 && temperature != -127.00 && temperature != 85.00) { #else if (temperature != -127.00 && 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 &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); // Store state in eeprom gw.saveState(CHILD_ID, state); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }
-
enable debug and put some print() statements in to see what happens.