Parking lot / College Capstone Project

What I have are two servo motors, and two photo-electric switches. I want (switch_1) to activate (servo_1) and (switch_2) to activate (servo_2). Each of these need to go from 0-90 degrees when HIGH. I think my problem is in the "if statement " but I may be wrong. If I do this with only one of the servos and switches it works fine. But with both it doesn’t. The switches are powered by external 12V and the Servos are powered by external 5V.

Thanks in Advance!!

Code below

#include <Servo.h> 
 
Servo servo1;  // create servo object to control a servo at the entrance

Servo servo2;  // create servo object to control a servo at the exit



const int switch1Pin = 7;     // the number of the Photoelectric switch pin at the entrance

int switch1State = 0;         // variable for reading Photoelectric switch at entrance


const int switch2Pin = 3;     // the number of the Photoelectric switch pin at the exit

int switch2State = 0;         // variable for reading Photoelectric switch at exit

                
void setup() 

{
  
  pinMode(switch1Pin, INPUT);  
  
  pinMode(switch2Pin, INPUT);

  servo1.attach(9);  // attaches the servo on pin 9 to the output of the photoelectric switch at the entrance
  
  servo2.attach(11);  // attaches the servo on pin 11 to the output of the photoelectric switch at the exit
} 
 
void loop(){
  // read the state of the photoelectric switch value:
  switch1State = digitalRead(switch1Pin);
  switch2State = digitalRead(switch2Pin);
  
 

  // check if the 1st switch is pressed.
  // if it is, the 1st switch is HIGH:
  if (switch1State == HIGH)
  
  
 {       
    servo1.write(90);              // tell servo go to upper position   
   
  } 
  else {
  
    servo1.write(0);             //delay 5 seconds and go back to zero
    delay(5000);
   
  }
  if (switch2State == HIGH)
  
  
 {       
    servo2.write(90);              // tell servo go to upper position   
   
  } 
  else {
  
    servo2.write(0);             //delay 5 seconds and go back to zero
    delay(5000);

Here are the problems that I see:

1) You need to learn to post your code between code tags. These are generated by the "#" button. 2) You did not set a starting position for the servos. Maybe Servo.attach(...) does this for you, I don't really know. 3) Your comments say "delay 5 seconds and go back to starting position" but you actually go back to position 0 (the starting position?) and then delay 5 seconds. 4) delay(...) is a blocking operation. That means that nothing else can happen while the delay is in progress. Thus, delay(5000) means that for 5 seconds the switches are not read, the other servo cannot be handled, etc. What is usually suggested at this point is to avoid delay(...) statements and embrace the "Blink without Delay" example.

Maybe what you have works but you are not being sufficiently patient (5 seconds worth!) during your testing?

1) Fixed the code, sorry about that. 2) Servo.attach tells the servo which pin it is tied to on the Arduino. 3) The starting position is 0. When I run this code with one servo and switch, I close the switch and the servo goes to 90 degrees and holds for 5 seconds then goes back to 0. But like I said when I try to use two servos and switches, neither servo1 nor servo2 works. 4) In this case the delay is fine, The two servos will not need to work within 5 seconds of each other.

Also, I have given the program plenty of time to clear the delay.

So, the next questions involve your wiring. How are your switches wired? You do have (uselessly complex) external pulldown resistors, right?

How are your servos powered? Not by the Arduino, right?

Hi, how are you powering your servos? The arduino 5V pin will not have enough to get both working. Please a picture of your project and a CAD or picture of a hand drawn circuit diagram. (I must make a template)

Thanks Tom...... :)

Your description doesn't match your code. You say

I want (servo_1) to activate (switch_1) and (servo_2) to activate (switch_2).

but in your code the switches activate the servos.

Assuming the code is correct (and not the description) what causes the photo-electric switches to trigger? In particular do they stay in the on or off state for long periods (i.e. long enough for the servo to move all the way) or are they only in one state for a very brief moment when something passes between them.

If they are only in a state for a brief moment the value of switch1state will be reset in the next pass through loop and you only have delay() in the ELSE condition.

It is probably a good idea to rewrite the code so it doesn't need any delay() functions. Study the concept in the Blink Without Delay example sketch.

(The other comments on wiring are also very relevant).

...R