Go Down

Topic: code/board interaction problem has me stumped (Read 643 times) previous topic - next topic

rickxxx

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
    
  }
}

wildbill

I can't see anything wrong with your code, although I'd say you only need a single ms_active variable and let any of the detector ifs set it. It sounds as though one of your detectors is constantly reporting motion for some reason unrelated to the sketch. Put a serial.print inside each of those ifs so that you can verify it. If so, it looks like hardware or wiring.

rickxxx

Thanks for the reply - I first thought it must be a wiring or hardware issue, but I switched the motion detectors around in the wiring harnesses, and tried monitoring different combinations of the inputs on the board - didn't matter which motion detector was plugged into which input to the board - as long as only one was being monitored, it was fine; as soon as I monitored more than one, the system thinks there is always motion.

I'll put some debugging logic in as you suggest.

Thanks,

Rick

RPCoyle

the compiler doesn't like "INPUT_PULLUP" It was not declared. Also it is good to initialize pins as INPUT or OUTPUT or they tend to not do what you want them to do.

PeterH


the compiler doesn't like "INPUT_PULLUP" It was not declared. Also it is good to initialize pins as INPUT or OUTPUT or they tend to not do what you want them to do.


I suspect you're still working with a pre-1.0 IDE whereas the poster is obviously working with 1.0. The syntax he's using is correct for 1.0.
I only provide help via the forum - please do not contact me for private consultancy.

James C4S



the compiler doesn't like "INPUT_PULLUP" It was not declared. Also it is good to initialize pins as INPUT or OUTPUT or they tend to not do what you want them to do.

I suspect you're still working with a pre-1.0 IDE whereas the poster is obviously working with 1.0. The syntax he's using is correct for 1.0.

Pre-1.0.1
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

kf2qd

Okay - what type of signal do the motion detectors put out? Are multiple detectors causing interference that they then detect as motion? I am assuming that the detectors put out a signal, and somehow make use of either the return time, or dopler shift. If they are on slightly different frequencies they could all be interpreting that as dopler shift and calling that detected motion.

Go Up