Servo Rotation Problem

Hi everyone,

I’m a totally newbie in programming and as well as any other electronics.
I’m currently trying to program the arduino (based on Daniel’s servo code) to have it react to my photoresistor in a certain manner.
The Logic for my code is this:

  1. Photoresistor captures light intensity and transform them into analog signal
  2. Status Mark will set the servo status as 0
  3. If the analog signal is greater than 64 and the status mark is 0, turn the servo by 180 degree
  4. Once the servo reaches 180 degree, it will stop. Status mark is set to 1
  5. Back to step 1
  6. If analog signal is less than 64 and status mark is 1, rotate the servo back to 0 degree
  7. Servo reaches 0 degree, servo stop. Status mark set back to 0
  8. back to step 1)

As you guys can see, the logic is a loop circle with if statements in it.
Here is the code:


int servoPin = 9; // R/C Servo connected to digital pin 13
int myAngle; // angle of the servo (roughly in degrees) 0 - 180
int pulseWidth; // function variable
int statusS = 0;
int analogValueA= 0;
int analogValueB= 0;
int analogValueC= 0;
int photoCellA = 0;
int photoCellB = 1;
int photoCellC = 2;
int ledPin = 13;
int time;
long randNumber;

void setup()
{
beginSerial(9600);
pinMode(1, INPUT); // set pin A1 as input
pinMode(0, INPUT); // set pin A1 as input
pinMode(2, INPUT); // set pin A1 as input
pinMode(9, OUTPUT); // sets pin D9 as output
pinMode(13, OUTPUT); // sets pin D13 as output
}

void servoPulse(int servoPin, int myAngle) // servo function
{ // this is a function for determining our pulsewidth for the servo
pulseWidth = (myAngle * 6) + 320; // this determines our delay below (for a standard pot)
digitalWrite(servoPin, HIGH); // set servo high
delayMicroseconds(pulseWidth); // wait a very small amount (determined by pulsewidth)
digitalWrite(servoPin, LOW); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}

void loop()
{
time = millis();
randNumber = random(1,4); // return a random number from 1 - 3
Serial.print(randNumber);
Serial.print(" “);
digitalWrite(ledPin, HIGH); // sets the LED on
analogValueA = analogRead (photoCellA);
analogValueB = analogRead (photoCellB);
analogValueC = analogRead (photoCellC);
Serial.print(analogValueA, DEC); //Prints Analog Value in Serial Message
Serial.print(” “);
Serial.print(analogValueB, DEC); //Prints Analog Value in Serial Message
Serial.print(” “);
Serial.print(analogValueC, DEC); //Prints Analog Value in Serial Message
Serial.print(” “);
Serial.print(randNumber);
Serial.print(” “);
Serial.print(statusS);
Serial.print(” ");
if ((analogValueA >= 64) and (statusS == 0)){
for (myAngle=0; myAngle<=180; myAngle++) { // cycle through every angle (rotate the servo 180 slowly)
servoPulse(servoPin, myAngle);
statusS = 1;
//digitalWrite(servoPin, LOW); // set servo low
//delay(20); // refresh cycle of typical servos (20 ms)
}
}
if ((analogValueA <= 64) and (statusS == 1)){
for (myAngle=180; myAngle>=0; myAngle–) { // cycle through every angle (rotate the servo 180 slowly)
servoPulse(servoPin, myAngle);
statusS = 0;
// digitalWrite(servoPin, LOW); // set servo low
// delay(20); // refresh cycle of typical servos (20 ms)
}
}
delay(1000);
}


The photoresistors and random value works perfectly fine. The problem I’m having is that once the servo motor rotates to 180 degrees and setting the status mark to 1, instead of rotating back to 0 degree and set the status mark to 0, the servo motor tries to continue going forward (180degree +).

The reason I say the servo tries to go forward is because the servo was jerking to the 180 degrees side and not jerking backward at all.

Can someone please have a look at my code and give me some suggestion on how to fix this problem?

Thank you

does anyone have any suggestion? my problem has gotten from bad to worst, I've implemented Daniel's simple servo code to my Arduino and it still doesn't work. For some reason, my servo does not go back to 0 degree and re-perform another 180 degree turn. Note that I did not change anything on Daniel's code, am I missing something in the coding part?

I have finally realized what my mistake was, and it was rather a stupid mistake. The problem itself has nothing to do with the coding at all (which I THOUGHT where my problem was). In fact, the problem acutally has to do with hardware wiring. As I mentioned in my first post, I connected the power and ground wire of the servo directly to the batteries and the signal line to the arduino PWM9 (digital 9). The problem was actually because I did not connect the another wire from the ground wire to the GND slot on the arduino.

I will also post up my result with my codes later.