Hi - I'm brand new to this, and have a problem that is stumping me - I have a project that has the board reading the signals from up to 3 motion detectors, and when motion is detected, it activates a servo routine. My problem is that the system works fine as long as my logic only uses one motion detector, but as soon as I monitor more than 1 detector, the program behaves as if there is always motion detected - it doesn't matter which detectors I monitor - works fine as long as it is only one. Any help would be greatly appreciated.
My code sample:
#include <Servo.h>
int loopwait = 5000; // delay time for main loop
int ms1 = 2; //motion detect, sensor 1
int ms2 = 4; //motion detect, sensor 2
int ms3 = 7; //motion detect, sensor 3
//Servo variables for Pin and Servo objects
int servo1 = 3;
Servo S1;
int servo_close = 0; // integer corresponding to closed PWM for servos
int servo_open = 10; // integer corresponding to open PWM for servos
int delay_servo_time = 30000; // servo delay time after opening shutters - in msec
boolean ms1_active;
boolean ms2_active;
boolean ms3_active;
void setup()
{
//Motion Sensor Setup
pinMode(ms1, INPUT_PULLUP); // set the pins and pull them high
pinMode(ms2, INPUT_PULLUP);
pinMode(ms3, INPUT_PULLUP);
}
void loop()
{
delay(loopwait); // just wait a bit before cycling through
checkSensors();
}
void checkSensors() {
ms1_active = false;
ms2_active = false;
ms3_active = false;
if (digitalRead(ms1)==LOW)
{
ms1_active = true;
}
if (digitalRead(ms2)==LOW)
{
ms2_active = true;
}
if (digitalRead(ms3)==LOW)
{
ms3_active = true;
}
//if((ms1_active==true)||(ms2_active==true)||(ms3_active==true))
//if((ms2_active==true)||(ms3_active==true))
//if(ms3_active==true)
if(ms2_active==true)
//if((ms1_active==true)||(ms3_active==true))
//if(ms1_active==true)
{
//we have a guest, activate the servos.
S1.attach(servo1); //re-attach each servor at start of detect loop, as we detach at end
S1.write(servo_open);//Open SHUTTER
delay(delay_servo_time);
S1.write(servo_close);//Close SHUTTER
S1.detach(); //detach each servo when off to stop drift
}
}