Changing servo direction using 2 different sensors

Hi everyone,

I have hooked up two servo motors to two photocells so that they both rotate when activated … and the code works great except i would like the servos to rotate in direction with analogue 4 and the opposite way with analogue 5.

Here is my code … does anyone know how to do this?

//Sensor A is Connected to Analog pin 5 and is controlling the SERVO connected to Digital Pin 2
//Sensor B is Connected to Analog pin 4 and is controlling the SERVO connected to Digital Pin 3

int averageA;
int averageB;
void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);

//Calibrate Photocells
for(int i=0;i<10;i++)
averageA+=analogRead(5);
averageA/=10;

for(int i=0;i<10;i++)
averageB+=analogRead(4);
averageB/=10;
Serial.println(“System Ready”);
}
//
void loop(){
int A = analogRead(5);
int B = analogRead(4);

//ENTER = analogue 5
if (A<averageA/1.5)
digitalWrite(2,HIGH);
delayMicroseconds(1800);
digitalWrite(2,LOW);
delay(50);
Serial.println(“Someone has entered the ball”);

//EXIT = analogue 4
if (B<averageB/1.5)
digitalWrite(3,HIGH);
delayMicroseconds(2000);
digitalWrite(3,LOW);
delay(50);
Serial.println(“Someone has exited the ball”);
}

Thanks Di :wink:

Servos need to be continuously pulsed in order to move, its not clear if your code will do this (It will depend on how your sensors react). Why not use the Arduino Servo library to drive your servos, this will handle the servo pulsing for you.

It should not be difficult to add your sensor code to one of the Servo example sketches and if it still does not do what you want then if you describe what is and is not working then I am sure you will get lots of help.

Hi Mem,

Everything in the code is working well right now ... both sensors are controlling both servos. The problem is that whether sensor A or sensor B is activiated, the servos will rotate in the same direction with both.

What i would like help with is to have Sensor A rotate the servos in one direction (clockwise) and have Sensor B rotate counter clockwise.

Hope this sounds a little more clear. Thanks!

Perhaps if you say what the sensors are sensing and the servos are controlling it will be clearer.

The two sensors are photocells and are calibrating well.

When either photocell is shadowed, it activates both servo motors to move clockwise and this part of the code is also working well.

What i would like to do is have photocell A rotate both servos clockwise BUT have photocell B rotate servos counterclockwise when shadowed.

:o

This is how I would do it using the arduino servo library. It untested but I hope this gets you going in the right direction.

If you do use the Arduino library, note that the servos must be connected to pins 9 and 10. This sketch should also work with the MegaServo library if you want to use pins 2 and 3. see: http://www.arduino.cc/playground/Code/MegaServo

if you do use this code you may need to adjust the angles to get the movement you want.

#include <Servo.h>

//Sensor A is Connected to Analog pin 5 and is controlling the SERVO connected to Digital Pin 9
//Sensor B is Connected to Analog pin 4 and is controlling the SERVO connected to Digital Pin 10

const int  sensorA = 5;
const int  sensorB = 4;

Servo servoA;
Servo servoB;

const int deflection = 10; // the number of degrees each way of center to move the servos
const int center     = 90;  

int averageA; 
int averageB;


void calibrate()
{
  //Calibrate Photocells
  for(int i=0;i<10;i++)       
    averageA+=analogRead(5);  
  averageA/=10; 

  for(int i=0;i<10;i++)      
    averageB+=analogRead(4); 
  averageB/=10;
  Serial.println("System Ready"); 
}

void setup(){
  Serial.begin(9600);
  servoA.attach(9);
  servoB.attach(10);
  calibrate();
}


void loop(){

  if( analogRead(sensorA) < averageA/1.5 ) {
    servoA.write(center + deflection);  
    servoB.write(center - deflection); 
    Serial.println("Someone has entered the ball");  
  }
  else{
    // not sure if you want to do anything here?
  }

  if( analogRead(sensorB) < averageB/1.5 ) {
    servoA.write(center - deflection); //send fully to one direction
    servoB.write(center + deflection); //send fully to one direction
    Serial.println("Someone has exited the ball");  
  }
  else{
     // not sure if you want to do anything here?
  }

  delay(20);
}

Edit: not sure what you want to hapen when the sensor is un-shadowed - you may want to add some code to move the servos back to their normal positons

Thanks Mem … i tested the code and the servos just rotate non stop and a single direction but i think i understand the coding language of the MegaServo library. Ill just keep playing with it

Thanks.

I just ran the code and it works as expected for me. I used two switches instead of photo-detectors to trigger the logic - when one button is pushed the servos rotate in opposite directions for 20 degrees and then stop. Pressing the other switch is similar with rotation in the opposite direction.

If you are not getting that behaviour make sure you are running the code currently posted (I removed the code in the else statements a few minutes after my posting the code above – check the code you uploaded is the same as on the site now)

If you are running the same code, try it using two switches instead of your sensors to make sure they are not the problem.

This version uses two pushbuttons, one connected from analog pin 5 and Gnd , the other from analog 6 and gnd

#include <Servo.h>

//Sensor A is Connected to Analog pin 5 and is controlling the SERVO connected to Digital Pin 9
//Sensor B is Connected to Analog pin 4 and is controlling the SERVO connected to Digital Pin 10

const int  sensorA = 5;
const int  sensorB = 4;

Servo servoA;
Servo servoB;

const int deflection = 10; // the number of degrees each way of center to move the servos
const int center     = 90;  

int averageA;
int averageB;


void calibrate()
{
  //Calibrate Photocells
  for(int i=0;i<10;i++)
    averageA+=analogRead(5);  
  averageA/=10;

  for(int i=0;i<10;i++)
    averageB+=analogRead(4);
  averageB/=10;
  Serial.println("System Ready");
}

void setup(){
  Serial.begin(9600);
  servoA.attach(9);
  servoB.attach(10);
// these lines added to enable pull-ups on analog pins 4 and 5
  pinMode(19, INPUT);
  pinMode(18, INPUT);
  digitalWrite(19, HIGH);
  digitalWrite(18, HIGH);
//  calibrate();
}


void loop(){

 // if( analogRead(sensorA) < averageA/1.5 ) {
  if( digitalRead(19) == LOW) {
    servoA.write(center + deflection);  
    servoB.write(center - deflection);
    Serial.println("Someone has entered the ball");  
  }
  else{
    // not sure if you want to do anything here?
  }

//  if( analogRead(sensorB) < averageB/1.5 ) {
  if( digitalRead(18) == LOW ){  
    servoA.write(center - deflection); //send fully to one direction
    servoB.write(center + deflection); //send fully to one direction
    Serial.println("Someone has exited the ball");  
  }
  else{
     // not sure if you want to do anything here?
  }

  delay(20);
}

Thanks Mem ... i really appreciate your help ... i will have to try it again and hopefully it will work for me this time.