# Distance Sensors are spontaneously triggering.

The code works if I am very precise with the sensors. Yet, without being extremely careful to trigger one after the other, the sensors will begin to trigger spontaneously without anything being within the distance range that would normally trigger the sensors. This causes the code to run out of order and at a random sequence.

I had a tip that setting up resistors in series might help to control the sensors and prevent them from spontaneously triggering. What do you guys think? If so, how would I go about doing this? I am suing an outlet with 5V output to power every sensor and motor with a breadboard. Using 28-BYJ48 motors and Sharp GP2Y0D810Z0F Digital Distance Sensors. Any advice would be much appreciated.

``````//Starting with 3 sensors and 3 motors.
// each motor is connected to a gate within a maze. Gates 1 and 2 are open at the start of the experiment. Gate three is closed.
//A proximity sensor is placed after each gate. Sensor 1, when triggered, while cause two things:
//motor 1 to rotate 90 degrees counterclockwise (to close gate 1),
// and motor 3 to rotate 90 degrees clockwise (to open gate 3).
//The remaining two sensors will open and close the other two gates in a similar fashion.

#include <Stepper.h>
const int stepsPerRevolution = 200;

//declaring pins for motors 1, 2, and 3 (motors will be configured to rotate clockwise 90 degrees (open position) or rotate counter clockwise 90 degrees (closed position))
Stepper motor1(stepsPerRevolution, 1, 3, 2, 4);
Stepper motor2(stepsPerRevolution, 5, 7, 6, 8);
Stepper motor3(stepsPerRevolution, 9, 11, 10, 12);

// declaring pins for proximity sensors 1, 2, and 3
int sensor1 = A5;
int sensor2 = A4;
int sensor3 = A3;

//seting up variables to read sensor values (low when triggered, high when not triggered)
int sensor1value;
int sensor2value;
int sensor3value;

//defining motor states
int counter1 = 0; //open
int counter2 = 0; //open
int counter3 = 1; //closed

void setup() {
pinMode(A5, INPUT);
pinMode(A4, INPUT);
pinMode(A3, INPUT);

motor1.setSpeed(60);
motor2.setSpeed(60);
motor3.setSpeed(60);
}

void loop()
{

if (sensor1value == LOW) //if sensor 1 is triggered
{
if (counter1 == 0) //and if gate 1 is open
{
motor1.step(-500); //rotate motor 1 counterclockwise 90 degrees (close gate 1)
counter1 = 1; //note that gate 1 is now closed
motor3.step(500); //rotate motor 3 clockwise 90 degrees (open gate 3)
counter3 = 0; //note that gate 3 is now open
}
}

else if (sensor2value == LOW)
{
if (counter2 == 0)
{
motor2.step(-500);
counter2 = 1;
motor1.step(500);
counter1 = 0;
}
}

else if (sensor3value == LOW)
{
if (counter3 == 0)
{
motor3.step(-500);
counter3 = 1;
motor2.step(500);
counter2 = 0;
}
}
}
``````

Please post a hand drawn wiring diagram (not a Fritzing abomination!). Include links to the motors and a description of the motor power supply. Use separate power supplies for the motors and sensors.

I had a tip that setting up resistors in series might help to control the sensors

The code works if I am very precise with the sensors.

What does that mean?

What I mean is that if I time it perfectly so that I trigger each sensor one after the other without any time gap then it works exactly how I want it to work and each sensor triggers the correct motor in the correct sequence. I included a photo of the pins and how they are designed. Nothing is being powered from the arduino and it is all being power from an outlet with connections to the breadboard.

You have no diodes across the motors so the interference from a motor is enough to reset the Arduino. You also have no decoupling capacitors so I am not surprised you are having problems.

It seems one motor is using the TX pin.
That pin is already used by the USB<>serial chip.

Analogue pins can be used as normal digital pins.
Leo…

Hi,
Can you post a picture of your project so we can see your component layout?

What are you using to drive the stepper motors?

Have you got the gnd of the driver board connected to the gnd of the UNO?

Have written code JUST to control ONE stepper?
Have you written code JUST to get ONE sensor working?
OR
Did you write ALL the code then try and get it to work?

Thanks.. Tom..