Controlling 2 continuous rotation Servos using Values of 2 Photo resistors

Hi guys

Earlier this week I posted a question about controlling the direction of a continuous rotation SERVOs using the values of 2 LDRs. Basically when the input value of one LDR is higher it would turn clockwise and similarly when the value of the other is higher it would turn the other way. This code works fine.

#include <Servo.h>

Servo servoFront;          
int LDR;
int LDR2;

void setup()
{
  servoFront.attach(12);
  Serial.begin(9600);
}
void loop()
{
  if (analogRead(0) > analogRead(1))

  {
  LDR = analogRead(0);
  LDR = map(LDR, 0, 1023, 90, 0);
  servoFront.write(LDR);
  delay(100);
  }

  else if (analogRead(0) < analogRead(1))

  {
  LDR2 = analogRead(1);
  LDR2 = map(LDR2, 0, 1023, 90, 180);
  servoFront.write(LDR2); 
  delay(100);
  }

  else
  
    servoFront.write(90);
    delay(100);

}

Ive tried to use a slightly modified code to use this on 2 SERVOS but this results in only one SERVO rotating again. I need this to control a small light sensing robot

#include <Servo.h>

Servo servoFront;          
Servo servoBack;        

int val;
int val2;

void setup()
{
  servoFront.attach(12);
  servoBack.attach(13);
}
void loop()
{
  val = analogRead(0);
  //Serial.println(val); 
  val2 = analogRead(1);
  //Serial.println(val2); 
{
if (val > val2)

  
  {
  val = map(val, 0, 1023, 90, 0);
  servoFront.write(val);
  val = map(val, 0, 1023, 90, 180);
  servoBack.write(val);
  }
  
else if (val < val2)
  
   {
  val2 = map(val2, 0, 1023, 90, 180);
  servoFront.write(val2);
  val2 = map(val2, 0, 1023, 90, 0);
  servoBack.write(val2);
   }
  
else
  servoFront.write(90);
  servoBack.write(90);
}
}

Would be grateful for any help.

{
if (val > val2)

Why is there a useless curly brace there?

  val = map(val, 0, 1023, 90, 0);
  servoFront.write(val);
  val = map(val, 0, 1023, 90, 180);
  servoBack.write(val);

Once map gets done, val is NOT in the range 0 to 1023. If the idea is to turn the other servo the other way, dump the second map() call, and simply use 180-val as the value for the second servo's position.

Hey Paul

I made the changes to the coding but I have the same problem :confused:

#include <Servo.h>

Servo servoFront;          
Servo servoBack;        

int val;
int val2;

void setup()
{
  servoFront.attach(12);
  servoBack.attach(13);
}
void loop()
{
  val = analogRead(0);
  //Serial.println(val); 
  val2 = analogRead(1);
  //Serial.println(val2); 

if (val > val2)

  
  {
  val = map(val, 0, 1023, 90, 0);
  servoFront.write(val);
  servoBack.write(180-val);
  }
  
else if (val < val2)
  
   {
  val2 = map(val2, 0, 1023, 90, 180);
  servoFront.write(val2);
  servoBack.write(val2+180);
   }
  
else
  servoFront.write(90);
  servoBack.write(90);

}

Manodha92:
Hey Paul

I made the changes to the coding but I have the same problem :confused:

Because actually, you didn't make the requested changes..."dump the second map()" you didn't do.

else
  servoFront.write(90);
  servoBack.write(90);

Maybe this is a case where a pair of braces is required.

Thanks so much guys. Finally got it to work. :smiley:

#include <Servo.h>

Servo servoFront;          
Servo servoBack;        

int val;
int val2;

void setup()
{
  servoFront.attach(12);
  servoBack.attach(13);
}
void loop()
{
  val = analogRead(0);
  //Serial.println(val); 
  val2 = analogRead(1);
  //Serial.println(val2); 

if (val > val2)

  
  {
  val = map(val, 0, 1023, 90, 0);
  servoFront.write(val);
  servoBack.write(180+val);
  }
  
else if (val < val2)
  
   {
  val2 = map(val2, 0, 1023, 90, 180);
  servoFront.write(val2);
  servoBack.write(180-val2);
   }
  
else
{
  servoFront.write(90);
  servoBack.write(90);
}

}