Motion/actuator sensor
-
Hi
I need some help now. I have been staring at the below code for quiet some time now, but I just can find the error.
The code should be sending a tripped signal to the controller, and the controller should return an ON/OFF to 1 of the 2 actuators. I can control the actuators just fine ON/OFF from the controller, but the motion sensor then start to flip HIGH/LOW at the interval assigned from the sensitivity potmeter.
Here is the code
#include <MySensor.h> #include <SPI.h> #define RELAY_1 6 // 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 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay // Motion sensor defs unsigned long SLEEP_TIME = 10000; // Sleep time between reports (in milliseconds) #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) #define CHILD_ID 3 // Id of the sensor child volatile int state = LOW; boolean lockLow = true; boolean lastTripped = 0; MySensor gw; MyMessage msgMotion(CHILD_ID, V_TRIPPED); //MyMessage msgRelay(CHILD_ID, V_LIGHT); MyMessage msg(CHILD_ID, V_LIGHT); void setup() { // 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("Relay/Motion Sensor", "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(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF); } pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID, S_MOTION); // attachInterrupt(DIGITAL_INPUT_SENSOR, ON, CHANGE); delay(30000); } void loop() { // Alway process incoming messages whenever possible gw.process(); boolean tripped = (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH); if (lastTripped != tripped ) { gw.send(msgMotion.set(tripped?"1":"0")); // Send new state lastTripped=tripped; } } void incomingMessage(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_LIGHT) { // Change relay state digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom gw.saveState(message.sensor, message.getBool()); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }With the above code I get this on the sensor serial:
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1every ~4 second.. PLS help :)
-
Hi
I need some help now. I have been staring at the below code for quiet some time now, but I just can find the error.
The code should be sending a tripped signal to the controller, and the controller should return an ON/OFF to 1 of the 2 actuators. I can control the actuators just fine ON/OFF from the controller, but the motion sensor then start to flip HIGH/LOW at the interval assigned from the sensitivity potmeter.
Here is the code
#include <MySensor.h> #include <SPI.h> #define RELAY_1 6 // 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 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay // Motion sensor defs unsigned long SLEEP_TIME = 10000; // Sleep time between reports (in milliseconds) #define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) #define CHILD_ID 3 // Id of the sensor child volatile int state = LOW; boolean lockLow = true; boolean lastTripped = 0; MySensor gw; MyMessage msgMotion(CHILD_ID, V_TRIPPED); //MyMessage msgRelay(CHILD_ID, V_LIGHT); MyMessage msg(CHILD_ID, V_LIGHT); void setup() { // 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("Relay/Motion Sensor", "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(sensor, S_LIGHT); // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF); } pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID, S_MOTION); // attachInterrupt(DIGITAL_INPUT_SENSOR, ON, CHANGE); delay(30000); } void loop() { // Alway process incoming messages whenever possible gw.process(); boolean tripped = (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH); if (lastTripped != tripped ) { gw.send(msgMotion.set(tripped?"1":"0")); // Send new state lastTripped=tripped; } } void incomingMessage(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_LIGHT) { // Change relay state digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom gw.saveState(message.sensor, message.getBool()); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }With the above code I get this on the sensor serial:
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:0
send: 30-30-0-0 s=3,c=1,t=16,pt=0,l=1,st=ok:1every ~4 second.. PLS help :)
-
@Hausner said:
but the motion sensor then start to flip HIGH/LOW at the interval assigned from the sensitivity potmeter.
so you want to trigger on only one side of the motion sensor's output?
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