Help troubleshooting code
-
I want to use a PIR to enable two ultrasonic sensors for 30 seconds like this:
1 PIR detects movement
2 PIR is disabled for 30 seconds
3 Both ultrasonic sensors is enbabled for 30 seconds
4 Ultrasonic sensors OFF PIR ON
5 --> Step oneBut I cant get my code to complie.
// 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); //} } reply -
In this line
#define INTERRUPT DIGITAL_INPUT_SENSOR-2DIGITAL_INPUT_SENSOR does not exist, Im assuming its the pin from the PIR so replace it with
#define INTERRUPT MOTION_A_CHILD_ID-2Also you have an extra } in your code
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); //} }Replace with
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); //} }Cheers
-
In this line
#define INTERRUPT DIGITAL_INPUT_SENSOR-2DIGITAL_INPUT_SENSOR does not exist, Im assuming its the pin from the PIR so replace it with
#define INTERRUPT MOTION_A_CHILD_ID-2Also you have an extra } in your code
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); //} }Replace with
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); //} }Cheers
Don't confuse MOTION_A_TRIGGER_PIN with MOTION_A_CHILD_ID.
-
Don't confuse MOTION_A_TRIGGER_PIN with MOTION_A_CHILD_ID.
You're right it should be MOTION_A_TRIGGER_PIN. And it should be -1 not -2 confirm?
-
You're right it should be MOTION_A_TRIGGER_PIN. And it should be -1 not -2 confirm?
The interrupt number is usually pin - 2 on our boards. But I would rather use the function provided by arduino to get the correct interrupt number for a pin, digitalPinToInterrupt.
-
Thanks for the help, the sketch complies and runs now. But where and how do I specify the timing in the sketch. This is what I want.
1 PIR detects movement
2 PIR is disabled for 30 seconds
3 Both ultrasonic sensors is enbabled for 30 seconds
4 Ultrasonic sensors OFF PIR ON
5 --> Step one -
I don't really understand how the sketch that I posted works. After motionsensor detects movement the loop under
if (tripped)runs, but for how long are the ultrasonic sensors active? And how do I make it so that they are active for 30 sec before they are disabled and the PIR starts again.
-
I don't really understand how the sketch that I posted works. After motionsensor detects movement the loop under
if (tripped)runs, but for how long are the ultrasonic sensors active? And how do I make it so that they are active for 30 sec before they are disabled and the PIR starts again.
Should the node sleep in between pinging during the 30 secs that the sonars should be active?
-
@Cliff-Karlsson said:
I don't really understand how the sketch that I posted works. After motionsensor detects movement the loop under
No I only want the sensor to sleep until it gets interrupted from the PIR. Then I want the ultrasonic sensors to ping continuously during the 30 secs.
-
Right now there's a call to wait method in between A and B pings, waiting 1 second. Is that intended? For 30 secs, ping A and B alternatively every 1 second?
-
Right now there's a call to wait method in between A and B pings, waiting 1 second. Is that intended? For 30 secs, ping A and B alternatively every 1 second?
Well I thing the sketch was created mostly using copy/paste so that bit is not exactly what I wanted.
For 30secs, ping A and B alternatively every 100ms would probably be better.
-
I don't understand where and how I should specify that the ultrasonic part should run for 30 sec.
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);``` -
You have to add a timer, and an if statement. Start the timer after motion is tripped. If time is less than 30 s, ping. Else reset timer and sleep with motion interrupt.
I can post some example code later perhaps.