0
int RlightPin=0;  //pin for right Photoresistor 
int LlightPin=1;  //pin for left Photoresistor
int TranslateAnalogR=0;
int TranslateAnalogL= 1; 
#include <Servo.h> //Servo library. 
int pos=90; //Declare and initialize the variable pos.
int servoPin=9; //Servo connected to pin 9
int servoDelay=10; //Time motor waits before working again.
Servo myArms; //Servo object called myArms


void setup() {
Serial.begin(9600); //Start serial communication.
pinMode(7,OUTPUT);
pinMode(servoPin,OUTPUT);
myArms.attach(servoPin);

}

void loop() {

Serial.println(analogRead(RlightPin));   //Write the value of the right photoresistor to the serial monitor.
TranslateAnalogR=analogRead(RlightPin); //Read the right photoresistor value         

Serial.println(analogRead(LlightPin));    //Write the value of the left photoresistor to the serial monitor.
TranslateAnalogL=analogRead(LlightPin);  //Read the left photoresistor value

if(TranslateAnalogR=TranslateAnalogL){        //If Photoresistor Value is above 355 the motor will stop          
myArms.write(pos); // sets the servo position according to the scaled value
delay(servoDelay);// waits for it to get to the position

}

if(TranslateAnalogR=!TranslateAnalogL && TranslateAnalogR>TranslateAnalogL) { //If Photoreistor R is not equal to L and higher than L move until equal
for (pos=0; pos>=40; pos=pos+1)  {
  myArms.attach(servoPin);
  myArms.write(pos); //send servo to pos
  if(TranslateAnalogR=TranslateAnalogL){
  myArms.detach();}
}
}

if(TranslateAnalogL=!TranslateAnalogR && TranslateAnalogL>TranslateAnalogR){    //If Photoresistor L is not equal to R and higher than R move until equal       
for (pos=0; pos>=40; pos=pos-1){
   myArms.attach(servoPin);
   myArms.write(pos); //send sevo to pos
   if(TranslateAnalogL=TranslateAnalogR){
   myArms.detach(); }

}
}




}
  • Does it compile? Does it work? – Chad G Jan 7 at 17:10
  • 2
    TranslateAnalogR=TranslateAnalogL should be TranslateAnalogR==TranslateAnalogL (== is equals; = is assignment). In TranslateAnalogR=!TranslateAnalogL && TranslateAnalogR>TranslateAnalogL you don't need the first part. If one value is bigger than the other they can't be the same. So the first part is superfluous. – Gerben Jan 7 at 17:11
  • for (pos=0; pos>=40; pos=pos-1){ doesn't do anything (yet). – Gerben Jan 7 at 17:14
  • run it and test using a flashlight (torch) – jsotola Jan 7 at 20:55
3

There are many issues with this code:

pinMode(7,OUTPUT);

What is this pin for? Why do you set it to OUTPUT and then never actually output anything? If the pin is in use, you should declare a constant for it, like

const int fooPin = 7;

and then use the constant instead of the bare number 7.

Serial.println(analogRead(RlightPin));   //Write the value of the right photoresistor to the serial monitor.
TranslateAnalogR=analogRead(RlightPin); //Read the right photoresistor value

analogRead() is a slow function, and it is quite inefficient to do the analog to digital conversion twice. You can instead

TranslateAnalogR = analogRead(RlightPin);
Serial.println(TranslateAnalogR);

As a matter of style, I would remove the comments. Commenting code is generally considered a good practice, as long as the comments tell something non obvious, like giving the “big picture” or the rationale for doing something in a particular way. Paraphrasing in English what the code already says in C++ is useless.

Serial.println(analogRead(RlightPin));   //Write the value of the right photoresistor to the serial monitor.
//...
Serial.println(analogRead(LlightPin));    //Write the value of the left photoresistor to the serial monitor.

You won't know what value corresponds to the left and what value corresponds to the right. I would instead do something like:

Serial.print("L: ");
Serial.print(TranslateAnalogL);
Serial.print(", R: ");
Serial.println(TranslateAnalogR);

Then:

if(TranslateAnalogR=TranslateAnalogL){        //If Photoresistor Value is above 355 the motor will stop

You mean ==.

if(TranslateAnalogR=!TranslateAnalogL && TranslateAnalogR>TranslateAnalogL) { //If Photoreistor R is not equal to L and higher than L move until equal

The first test is redundant. If R>L, then obviously R≠L.

    for (pos=0; pos>=40; pos=pos+1)  {
        myArms.attach(servoPin);
        myArms.write(pos); //send servo to pos
        if(TranslateAnalogR=TranslateAnalogL){
            myArms.detach();
        }
    }

This is a very contrived way of searching the optimum. Normally you attach the servo once and keep it attached all the time. Then you adjust the position until you see the same illumination on both LDRs. This way you keep track of the correct position and you need not redo the whole angle scan every time. Also, this will not work for three reasons:

  1. You mean TranslateAnalogR==TranslateAnalogL rather than TranslateAnalogR=TranslateAnalogL
  2. TranslateAnalogR and TranslateAnalogL are not updated within the loop, you need to explicitly perform an analog read.
  3. The loop will run too fast for the servos to follow the requested positions.

Later:

    for (pos=0; pos>=40; pos=pos-1){

The loop is backwards. You probably mean

    for (pos=40; pos>=0; pos=pos-1){

Here is my take at this, completely untested:

#include <Servo.h>

// Pinout.
const int PIN_LDR_R = 0;  // right photoresistor 
const int PIN_LDR_L = 1;  // left photoresistor
const int PIN_SERVO = 9;  // servo

const int SERVO_DELAY = 20; // time motor waits before working again

int pos = 90; // servo position in degrees
Servo servo;  // servo object

void setup() {
    Serial.begin(9600);
    pinMode(PIN_SERVO, OUTPUT);
    servo.attach(PIN_SERVO);
}

void loop() {
    // Read and report light levels.
    int level_L = analogRead(PIN_LDR_L);
    int level_R = analogRead(PIN_LDR_R);
    Serial.print("L: ");
    Serial.print(level_L);
    Serial.print(", R: ");
    Serial.println(level_R);

    // Adjust the servo position.
    if (level_R > level_L && pos < 180)
        ++pos;
    if (level_R < level_L && pos > 0)
        --pos;

    // Update the servo.
    servo.write(pos);
    delay(SERVO_DELAY);
}

A few things worth noting:

  • Constants are qualified as const (it helps the compiler optimize and catch bugs) and spelled in full caps (a common stylistic convention).
  • The servo position is adjusted step by step, one degree per loop iteration. No need to perform a full scan every time.
  • The program does not do anything special when the light levels are equal, because in that case there isn't actually much to do.
  • I increased the delay between servo updates to 20 ms, as the serial communication alone can take close to 19 ms per iteration.
  • You may want to add some hysteresis in order to avoid the servo jitter when it has found the optimum.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.