Add a second distance sensor to the same arduino?
-
Absolutley none offence taken, I really suck at coding and my main methods are copy/paste and click on verify to check if it worked.
I am using the developer branch that does not use the gw. stuff if that is what you mean?
This is what I have tried for now but ofcourse it does not work.
// Enable debug prints #define MY_DEBUG #define MY_BAUD_RATE 115200 // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensor.h> #include <NewPing.h> #define SONAR_A_CHILD_ID 1 #define SONAR_B_CHILD_ID 2 #define MOTION_A_CHILD_ID 3 #define SONAR_A_TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_A_ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MOTION_A_TRIGGER_PIN 2 #define SONAR_B_TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_B_ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 200; // Sleep time between reads (in milliseconds) unsigned long MOTION_SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) NewPing sonar_A(SONAR_A_TRIGGER_PIN, SONAR_A_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar_B(SONAR_B_TRIGGER_PIN, SONAR_B_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage SONAR_A_msg(SONAR_A_CHILD_ID, V_DISTANCE); int SONAR_A_lastDist; MyMessage SONAR_B_msg(SONAR_B_CHILD_ID, V_DISTANCE); int SONAR_B_lastDist; MyMessage msg(MOTION_A_msg, V_TRIPPED); boolean metric = true; void setup() { metric = getConfig().isMetric; pinMode(MOTION_A_TRIGGER_PIN, INPUT); // sets the motion sensor digital pin as input } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Distance+Motion Sensor", "1.0"); // Register all sensors to gw (they will be created as child devices) present(SONAR_A_CHILD_ID, S_DISTANCE); present(SONAR_B_CHILD_ID, S_DISTANCE); present(MOTION_A_TRIGGER_PIN, S_MOTION); } void loop() { boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; Serial.println(tripped); send(msg.set(tripped?"1":"0")); // Send tripped value to gw if(tripped) { int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in(); Serial.print("Ping Sonar A: "); Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_A_dist != SONAR_A_lastDist) { send(SONAR_A_msg.set(SONAR_A_dist)); SONAR_A_lastDist = SONAR_A_dist; } wait(1000); int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in(); Serial.print("Ping Sonar B: "); Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_B_dist != SONAR_B_lastDist) { send(SONAR_B_msg.set(SONAR_B_dist)); SONAR_B_lastDist = SONAR_B_dist; } sleep(SLEEP_TIME); } } else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); } }Error: 'MOTION_A_msg' was not declared in this scope
-
Absolutley none offence taken, I really suck at coding and my main methods are copy/paste and click on verify to check if it worked.
I am using the developer branch that does not use the gw. stuff if that is what you mean?
This is what I have tried for now but ofcourse it does not work.
// Enable debug prints #define MY_DEBUG #define MY_BAUD_RATE 115200 // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensor.h> #include <NewPing.h> #define SONAR_A_CHILD_ID 1 #define SONAR_B_CHILD_ID 2 #define MOTION_A_CHILD_ID 3 #define SONAR_A_TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_A_ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MOTION_A_TRIGGER_PIN 2 #define SONAR_B_TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_B_ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 200; // Sleep time between reads (in milliseconds) unsigned long MOTION_SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) NewPing sonar_A(SONAR_A_TRIGGER_PIN, SONAR_A_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar_B(SONAR_B_TRIGGER_PIN, SONAR_B_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage SONAR_A_msg(SONAR_A_CHILD_ID, V_DISTANCE); int SONAR_A_lastDist; MyMessage SONAR_B_msg(SONAR_B_CHILD_ID, V_DISTANCE); int SONAR_B_lastDist; MyMessage msg(MOTION_A_msg, V_TRIPPED); boolean metric = true; void setup() { metric = getConfig().isMetric; pinMode(MOTION_A_TRIGGER_PIN, INPUT); // sets the motion sensor digital pin as input } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Distance+Motion Sensor", "1.0"); // Register all sensors to gw (they will be created as child devices) present(SONAR_A_CHILD_ID, S_DISTANCE); present(SONAR_B_CHILD_ID, S_DISTANCE); present(MOTION_A_TRIGGER_PIN, S_MOTION); } void loop() { boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; Serial.println(tripped); send(msg.set(tripped?"1":"0")); // Send tripped value to gw if(tripped) { int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in(); Serial.print("Ping Sonar A: "); Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_A_dist != SONAR_A_lastDist) { send(SONAR_A_msg.set(SONAR_A_dist)); SONAR_A_lastDist = SONAR_A_dist; } wait(1000); int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in(); Serial.print("Ping Sonar B: "); Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_B_dist != SONAR_B_lastDist) { send(SONAR_B_msg.set(SONAR_B_dist)); SONAR_B_lastDist = SONAR_B_dist; } sleep(SLEEP_TIME); } } else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); } }Error: 'MOTION_A_msg' was not declared in this scope
@Cliff-Karlsson I'm sorry I haven't played with the development branch. I'm just using 1.5.4. But the part abount interrupts and pin2 and 3 stays the same. It's just how the Arduino works.
-
Absolutley none offence taken, I really suck at coding and my main methods are copy/paste and click on verify to check if it worked.
I am using the developer branch that does not use the gw. stuff if that is what you mean?
This is what I have tried for now but ofcourse it does not work.
// Enable debug prints #define MY_DEBUG #define MY_BAUD_RATE 115200 // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensor.h> #include <NewPing.h> #define SONAR_A_CHILD_ID 1 #define SONAR_B_CHILD_ID 2 #define MOTION_A_CHILD_ID 3 #define SONAR_A_TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_A_ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MOTION_A_TRIGGER_PIN 2 #define SONAR_B_TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_B_ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 200; // Sleep time between reads (in milliseconds) unsigned long MOTION_SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) NewPing sonar_A(SONAR_A_TRIGGER_PIN, SONAR_A_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar_B(SONAR_B_TRIGGER_PIN, SONAR_B_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage SONAR_A_msg(SONAR_A_CHILD_ID, V_DISTANCE); int SONAR_A_lastDist; MyMessage SONAR_B_msg(SONAR_B_CHILD_ID, V_DISTANCE); int SONAR_B_lastDist; MyMessage msg(MOTION_A_msg, V_TRIPPED); boolean metric = true; void setup() { metric = getConfig().isMetric; pinMode(MOTION_A_TRIGGER_PIN, INPUT); // sets the motion sensor digital pin as input } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Distance+Motion Sensor", "1.0"); // Register all sensors to gw (they will be created as child devices) present(SONAR_A_CHILD_ID, S_DISTANCE); present(SONAR_B_CHILD_ID, S_DISTANCE); present(MOTION_A_TRIGGER_PIN, S_MOTION); } void loop() { boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; Serial.println(tripped); send(msg.set(tripped?"1":"0")); // Send tripped value to gw if(tripped) { int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in(); Serial.print("Ping Sonar A: "); Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_A_dist != SONAR_A_lastDist) { send(SONAR_A_msg.set(SONAR_A_dist)); SONAR_A_lastDist = SONAR_A_dist; } wait(1000); int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in(); Serial.print("Ping Sonar B: "); Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_B_dist != SONAR_B_lastDist) { send(SONAR_B_msg.set(SONAR_B_dist)); SONAR_B_lastDist = SONAR_B_dist; } sleep(SLEEP_TIME); } } else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); } }Error: 'MOTION_A_msg' was not declared in this scope
@Cliff-Karlsson I think you want this:
MyMessage msg(MOTION_A_CHILD_ID, V_TRIPPED); boolean metric = true;instead of
MyMessage msg(MOTION_A_msg, V_TRIPPED); boolean metric = true; -
Thanks, sorry fore code-spamming but it feels like I am starting to get close to the first step witch is a succesful verify.
The whole sketch verifies ok until it gets to the last "sleep" then I get this error:"expected constructor, destructor, or type conversion before '(' token"
// Enable debug prints #define MY_DEBUG #define MY_BAUD_RATE 115200 // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensor.h> #include <NewPing.h> #define SONAR_A_CHILD_ID 1 #define SONAR_B_CHILD_ID 2 #define MOTION_A_CHILD_ID 3 #define SONAR_A_TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_A_ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MOTION_A_TRIGGER_PIN 2 #define SONAR_B_TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_B_ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 200; // Sleep time between reads (in milliseconds) unsigned long MOTION_SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) NewPing sonar_A(SONAR_A_TRIGGER_PIN, SONAR_A_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar_B(SONAR_B_TRIGGER_PIN, SONAR_B_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage SONAR_A_msg(SONAR_A_CHILD_ID, V_DISTANCE); int SONAR_A_lastDist; MyMessage SONAR_B_msg(SONAR_B_CHILD_ID, V_DISTANCE); int SONAR_B_lastDist; MyMessage MOTION_A_msg(MOTION_A_CHILD_ID, V_TRIPPED); boolean metric = true; void setup() { metric = getConfig().isMetric; pinMode(MOTION_A_TRIGGER_PIN, INPUT); // sets the motion sensor digital pin as input } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Distance+Motion Sensor", "1.0"); // Register all sensors to gw (they will be created as child devices) present(SONAR_A_CHILD_ID, S_DISTANCE); present(SONAR_B_CHILD_ID, S_DISTANCE); present(MOTION_A_TRIGGER_PIN, S_MOTION); } void loop() { boolean tripped = digitalRead(MOTION_A_TRIGGER_PIN) == HIGH; Serial.println(tripped); send(MOTION_A_msg.set(tripped?"1":"0")); // Send tripped value to gw if(tripped) { int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in(); Serial.print("Ping Sonar A: "); Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_A_dist != SONAR_A_lastDist) { send(SONAR_A_msg.set(SONAR_A_dist)); SONAR_A_lastDist = SONAR_A_dist; } wait(1000); int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in(); Serial.print("Ping Sonar B: "); Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_B_dist != SONAR_B_lastDist) { send(SONAR_B_msg.set(SONAR_B_dist)); SONAR_B_lastDist = SONAR_B_dist; } sleep(SLEEP_TIME); } } //else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); //} } -
Thanks, sorry fore code-spamming but it feels like I am starting to get close to the first step witch is a succesful verify.
The whole sketch verifies ok until it gets to the last "sleep" then I get this error:"expected constructor, destructor, or type conversion before '(' token"
// Enable debug prints #define MY_DEBUG #define MY_BAUD_RATE 115200 // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 #include <SPI.h> #include <MySensor.h> #include <NewPing.h> #define SONAR_A_CHILD_ID 1 #define SONAR_B_CHILD_ID 2 #define MOTION_A_CHILD_ID 3 #define SONAR_A_TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_A_ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MOTION_A_TRIGGER_PIN 2 #define SONAR_B_TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define SONAR_B_ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 200; // Sleep time between reads (in milliseconds) unsigned long MOTION_SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds) #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway) NewPing sonar_A(SONAR_A_TRIGGER_PIN, SONAR_A_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. NewPing sonar_B(SONAR_B_TRIGGER_PIN, SONAR_B_ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage SONAR_A_msg(SONAR_A_CHILD_ID, V_DISTANCE); int SONAR_A_lastDist; MyMessage SONAR_B_msg(SONAR_B_CHILD_ID, V_DISTANCE); int SONAR_B_lastDist; MyMessage MOTION_A_msg(MOTION_A_CHILD_ID, V_TRIPPED); boolean metric = true; void setup() { metric = getConfig().isMetric; pinMode(MOTION_A_TRIGGER_PIN, INPUT); // sets the motion sensor digital pin as input } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Distance+Motion Sensor", "1.0"); // Register all sensors to gw (they will be created as child devices) present(SONAR_A_CHILD_ID, S_DISTANCE); present(SONAR_B_CHILD_ID, S_DISTANCE); present(MOTION_A_TRIGGER_PIN, S_MOTION); } void loop() { boolean tripped = digitalRead(MOTION_A_TRIGGER_PIN) == HIGH; Serial.println(tripped); send(MOTION_A_msg.set(tripped?"1":"0")); // Send tripped value to gw if(tripped) { int SONAR_A_dist = metric?sonar_A.ping_cm():sonar_A.ping_in(); Serial.print("Ping Sonar A: "); Serial.print(SONAR_A_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_A_dist != SONAR_A_lastDist) { send(SONAR_A_msg.set(SONAR_A_dist)); SONAR_A_lastDist = SONAR_A_dist; } wait(1000); int SONAR_B_dist = metric?sonar_B.ping_cm():sonar_B.ping_in(); Serial.print("Ping Sonar B: "); Serial.print(SONAR_B_dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (SONAR_B_dist != SONAR_B_lastDist) { send(SONAR_B_msg.set(SONAR_B_dist)); SONAR_B_lastDist = SONAR_B_dist; } sleep(SLEEP_TIME); } } //else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); //} }@Cliff-Karlsson To me that's what this community is about. I'll have a loop. But no promise, as said before I haven't played with the development branch yet. But I do understand the compiler error
-
@TheoL said:
what this community is about. I'll have a loop. But no promise, as said before I haven't played with the development branch yet. But I do understand the compiler error
Thanks.
And if unclear I am trying to achieve in the end is:When PIR senses motion, activate both distance sensors for 30 seconds and deactivate PIR.
After 30 seconds deactivate distance sensors and activate PIR again. -
@TheoL said:
what this community is about. I'll have a loop. But no promise, as said before I haven't played with the development branch yet. But I do understand the compiler error
Thanks.
And if unclear I am trying to achieve in the end is:When PIR senses motion, activate both distance sensors for 30 seconds and deactivate PIR.
After 30 seconds deactivate distance sensors and activate PIR again.@Cliff-Karlsson Is it giving the error on the first or the second sleep?
I have to compliment @Hek and the other guys. They've made MySensors easier to program. The branch looks really great. Love it.
-
@TheoL said:
what this community is about. I'll have a loop. But no promise, as said before I haven't played with the development branch yet. But I do understand the compiler error
Thanks.
And if unclear I am trying to achieve in the end is:When PIR senses motion, activate both distance sensors for 30 seconds and deactivate PIR.
After 30 seconds deactivate distance sensors and activate PIR again.@Cliff-Karlsson I think I found it.
it's in this part
sleep(SLEEP_TIME); } } // <--------- this ends the loop() method //else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); // <---- so this is not in the method //} } // <-------- int this is also outside of the loop methodChange it to:
sleep(SLEEP_TIME); } // } // comment brace //else{ // Sleep until interrupt comes in on motion sensor. Send update every two minute. sleep(INTERRUPT,CHANGE, MOTION_SLEEP_TIME); //} } -
After commenting the bracket I get : 'DIGITAL_INPUT_SENSOR' was not declared in this scope
-
After commenting the bracket I get : 'DIGITAL_INPUT_SENSOR' was not declared in this scope
@Cliff-Karlsson I'm sorry would love help you further. But it's getting late over here and my better half wants me to turn off the laptop.