hi all,
I am trying to create a device that is controlled by servo motors and reads input from photosensors.
There are two photosensors A and B
There is one Servo (Continuous rotation) Motor
There are 4 Positions for the motor to go to (N,W,S,E)
I am having troubles writing this code. SO far I have been able to tell the motor to turn from the starting position (North) when Photosensor A is shadowed to go 90degree clockwise to East. Then if Photosensor A is triggered again it will no longer move Clockwise. If Photosensor B is triggered it will turn 90degree counterclockwise.
My problem is expanding this code any further than this basic code. Essentially, Sensor A tells the motor to turn CLOCKWISE and Sensor B tells it to go CounterCLockwise but it can never move beyond a 360degree turn. So, for instance, if the motor is in South position, I want it to turn clockwise or counterclockwise depending on the sensor triggered. How can I acheive this?
ANy help would be appreciated
regards,
Below is what I have so far:
int averageA;
int averageB;
int PointACovered;
int PointBCovered;
int previousA;
int previousB;
int pos=1;
void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
for(int i=0;i<10;i++) //Calibrate the first sensor
averageA+=analogRead(5);
averageA/=10;
previousA=averageA;
for(int j=0;j<10;j++) //Calibrate the second sensor
averageB+=analogRead(4);
averageB/=10;
previousB=averageB;
Serial.println("System Ready");
Serial.println(averageA);
Serial.println(averageB);
}
void loop(){
int A = analogRead(5);
int B = analogRead(4);
if (A<(averageA-75)) //if point A is Covered
if (pos==1) // Initial Start Point
{for(int a=0; a<200; a++){
digitalWrite(2,HIGH); // turn on servomotor to 180 degrees
delayMicroseconds(1600); // length of pulse sets the motor position
digitalWrite(2,LOW); // turn off motor
delay(10);
pos=2;
Serial.println(pos);}
}
if (B<(averageB-75)) //if point B is Covered
if (pos==2)
{for(int a=0; a<100; a++){
digitalWrite(2,HIGH); // turn on servomotor to 180 degrees
delayMicroseconds (800); // length of pulse sets the motor position
digitalWrite(2,LOW); // turn off motor
delay(10);
pos=1;
Serial.println(pos);}
}
}