IR distance sensor with relay and repeater not working
-
Hi,
i cant make my IR distance-repeater-relay sensor to work. It doesnt go into the repeatermode and i cant switch the relays. the distancesensor does work.. anyone any idea?// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h>#include <DistanceGP2Y0A21YK.h>
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay#define CHILD_ID_DISTANCE 100
DistanceGP2Y0A21YK Dist;
// unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)MySensor gw; int numSensors=0;boolean receivedConfig = true;
boolean metric = true;
int distance;
int lastDistance=0;
int loopCounter=0;
MyMessage msg(1,V_LIGHT);
MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);void setup() {Dist.begin(0);
// Initialize library and add callback for incoming messages
gw.begin(NULL, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("RelayDistanceFloris", "1.0");
gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
// Fetch relay status
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
boolean savedState = gw.loadState(sensor);
digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
gw.send(msg.set(savedState? 1 : 0));
}
}void loop() { // Alway process incoming messages whenever possible gw.process(); distance = Dist.getDistanceCentimeter();if (distance != lastDistance) {
lastDistance = distance;
gw.send(msgdist.set(distance));
gw.sleep(50000);
}
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_LIGHT) {
boolean relayState = message.getBool();
digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
gw.saveState(message.sensor, relayState);
gw.send(msg.set(relayState? 1 : 0));
}
} -
Hi,
i cant make my IR distance-repeater-relay sensor to work. It doesnt go into the repeatermode and i cant switch the relays. the distancesensor does work.. anyone any idea?// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h>#include <DistanceGP2Y0A21YK.h>
#define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay#define CHILD_ID_DISTANCE 100
DistanceGP2Y0A21YK Dist;
// unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)MySensor gw; int numSensors=0;boolean receivedConfig = true;
boolean metric = true;
int distance;
int lastDistance=0;
int loopCounter=0;
MyMessage msg(1,V_LIGHT);
MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);void setup() {Dist.begin(0);
// Initialize library and add callback for incoming messages
gw.begin(NULL, AUTO, true);
// Send the sketch version information to the gateway and Controller
gw.sendSketchInfo("RelayDistanceFloris", "1.0");
gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
// Fetch relay status
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
boolean savedState = gw.loadState(sensor);
digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF);
gw.send(msg.set(savedState? 1 : 0));
}
}void loop() { // Alway process incoming messages whenever possible gw.process(); distance = Dist.getDistanceCentimeter();if (distance != lastDistance) {
lastDistance = distance;
gw.send(msgdist.set(distance));
gw.sleep(50000);
}
}
void incomingMessage(const MyMessage &message) {
if (message.type==V_LIGHT) {
boolean relayState = message.getBool();
digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF);
gw.saveState(message.sensor, relayState);
gw.send(msg.set(relayState? 1 : 0));
}
}@floris said:
gw.sleep(50000);
it is sleeping, so it won't respond to repeater
gw.sleep(50000);you need to keep the sensor awake if you plan on using the sensor as a repeater.
think about using a millis( ) timer to check the distances every so many seconds....
-
Hi, thanks. if i disable that sleep, than i get a flipstorm from the distanceinfo. (and i still cant control my relay) i thought i would let send the distanceinfo each 5 seconds (for testing) and for the rest a no sleep mode for repeating and receiving for the relay...
-
Hi, thanks. if i disable that sleep, than i get a flipstorm from the distanceinfo. (and i still cant control my relay) i thought i would let send the distanceinfo each 5 seconds (for testing) and for the rest a no sleep mode for repeating and receiving for the relay...
post your code between three 'backticks' (reverse apostrophe like this:
<<<< three back ticks here
code
code
etc
code
<<<< three more back ticks hereso it can be more easily read...
try to edit your loop( ) function to look like this:
void loop() { // Alway process incoming messages whenever possible gw.process(); static unsigned long lastTime = millis(); if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds { distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } lastTime = millis(); } } -
Hi, ok. now i have changed it to this: But my relays doest react...
// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h> #include <DistanceGP2Y0A21YK.h> #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay #define CHILD_ID_DISTANCE 100 DistanceGP2Y0A21YK Dist; // unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) MySensor gw; int numSensors=0; boolean receivedConfig = true; boolean metric = true; int distance; int lastDistance=0; int loopCounter=0; MyMessage msg(1,V_LIGHT); MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE); void setup() { Dist.begin(0); // Initialize library and add callback for incoming messages gw.begin(NULL, AUTO, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistanceFloris", "1.0"); gw.present(CHILD_ID_DISTANCE,S_DISTANCE); // Fetch relay status for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) gw.present(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) boolean savedState = gw.loadState(sensor); digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF); gw.send(msg.set(savedState? 1 : 0)); } } void loop() { // Alway process incoming messages whenever possible gw.process(); static unsigned long lastTime = millis(); if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds { distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } lastTime = millis(); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { boolean relayState = message.getBool(); digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, relayState); gw.send(msg.set(relayState? 1 : 0)); } } -
Hi, ok. now i have changed it to this: But my relays doest react...
// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h> #include <DistanceGP2Y0A21YK.h> #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay #define CHILD_ID_DISTANCE 100 DistanceGP2Y0A21YK Dist; // unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) MySensor gw; int numSensors=0; boolean receivedConfig = true; boolean metric = true; int distance; int lastDistance=0; int loopCounter=0; MyMessage msg(1,V_LIGHT); MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE); void setup() { Dist.begin(0); // Initialize library and add callback for incoming messages gw.begin(NULL, AUTO, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistanceFloris", "1.0"); gw.present(CHILD_ID_DISTANCE,S_DISTANCE); // Fetch relay status for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) gw.present(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) boolean savedState = gw.loadState(sensor); digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF); gw.send(msg.set(savedState? 1 : 0)); } } void loop() { // Alway process incoming messages whenever possible gw.process(); static unsigned long lastTime = millis(); if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds { distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } lastTime = millis(); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { boolean relayState = message.getBool(); digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, relayState); gw.send(msg.set(relayState? 1 : 0)); } }Yessss. i guesss i have got it working!!!! thanks for the help! Floris
// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h> #include <DistanceGP2Y0A21YK.h> #define CHILD_ID_DISTANCE 100 DistanceGP2Y0A21YK Dist; #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay MySensor gw; int numSensors=0; boolean receivedConfig = true; boolean metric = true; int distance; int lastDistance=0; int loopCounter=0; MyMessage msg(1,V_LIGHT); MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE); void setup() { Dist.begin(0); // Initialize library and add callback for incoming messages gw.begin(incomingMessage, AUTO, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistanceRepeaterFloris", "1.0"); // Fetch relay status for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_DISTANCE,S_DISTANCE); gw.present(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) boolean savedState = gw.loadState(sensor); digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF); gw.send(msg.set(savedState? 1 : 0)); } } void loop() { // Alway process incoming messages whenever possible gw.process(); static unsigned long lastTime = millis(); if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds { distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } lastTime = millis(); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { boolean relayState = message.getBool(); digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, relayState); gw.send(msg.set(relayState? 1 : 0)); } } -
Yessss. i guesss i have got it working!!!! thanks for the help! Floris
// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h> #include <DistanceGP2Y0A21YK.h> #define CHILD_ID_DISTANCE 100 DistanceGP2Y0A21YK Dist; #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay MySensor gw; int numSensors=0; boolean receivedConfig = true; boolean metric = true; int distance; int lastDistance=0; int loopCounter=0; MyMessage msg(1,V_LIGHT); MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE); void setup() { Dist.begin(0); // Initialize library and add callback for incoming messages gw.begin(incomingMessage, AUTO, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistanceRepeaterFloris", "1.0"); // Fetch relay status for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_DISTANCE,S_DISTANCE); gw.present(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) boolean savedState = gw.loadState(sensor); digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF); gw.send(msg.set(savedState? 1 : 0)); } } void loop() { // Alway process incoming messages whenever possible gw.process(); static unsigned long lastTime = millis(); if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds { distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } lastTime = millis(); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { boolean relayState = message.getBool(); digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, relayState); gw.send(msg.set(relayState? 1 : 0)); } } -
well.. i guess not..... It seems that its working for a short while. after that 1 of the relays does not respond anymore, when i set on relay on, both of the relays are going on? any help where my sketch is wrong?
Floris
// Example sketch showing how to control physical relays. // This example will remember relay state even after power failure. #include <MySensor.h> #include <SPI.h> #include <DistanceGP2Y0A21YK.h> #define CHILD_ID_DISTANCE 100 DistanceGP2Y0A21YK Dist; #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 2 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay MySensor gw; int numSensors=0; boolean receivedConfig = true; boolean metric = true; int distance; int lastDistance=0; int loopCounter=0; MyMessage msg(1,V_LIGHT); MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE); void setup() { Dist.begin(0); // Initialize library and add callback for incoming messages gw.begin(incomingMessage, AUTO, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistanceRepeaterFloris", "1.0"); // Fetch relay status for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_DISTANCE,S_DISTANCE); gw.present(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) boolean savedState = gw.loadState(sensor); digitalWrite(pin, savedState?RELAY_ON:RELAY_OFF); gw.send(msg.set(savedState? 1 : 0)); } } void loop() { // Alway process incoming messages whenever possible gw.process(); static unsigned long lastTime = millis(); if (millis() - lastTime >= 5000UL) // update the distance every 5 seconds { distance = Dist.getDistanceCentimeter(); if (distance != lastDistance) { lastDistance = distance; gw.send(msgdist.set(distance)); } lastTime = millis(); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { boolean relayState = message.getBool(); digitalWrite(message.sensor-1+RELAY_1, relayState?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, relayState); gw.send(msg.set(relayState? 1 : 0)); } }
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login