I started project with a servo and LDR sensor. I need direction please.

Hello,

I am confused. I bought a book entitled Absolute Beginner’s Guide to C(2nd Edition). So far I have grasped a better understanding of the language. On the other hand I am not up to par with the Arduino. I wrote the following test code.

When the program runs the servo moves very slowly(simulating the lowest possible speed I know how to program), the LDR is working properly however I would like the following to occur.

When the program begins the servo will continue to rotate until in encounters a low light source i.e. it is approaching a wall. I would then like the servo to stop and the LED to light. Once I can get this down I have the start of a decent robot of course with additional functions and sensors.

Could someone please tell me why this program is not doing what I would like it to do ? Thank you for your time. I really do appreciate all of the help I have received on the forum.

#include <Servo.h>
Servo myservo;


int led = 13;
int sensor = 7;
int lightMe = analogRead(sensor);
int motorRight = 0;




void MotorCtrl()
{
  motorRight = analogRead(sensor);
  if (lightMe < 80) {myservo.write(90); delay(3000);}
  else {myservo.write(180);}
}
  



void setup()
{
  delay(5000);
  myservo.attach(8);
  
  
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  Serial.begin(9600);
}


void loop()
{
 
  lightMe = analogRead(sensor);
  if(lightMe < 120){ digitalWrite(led, HIGH);}
  if(lightMe > 80){ digitalWrite(led, LOW);}

  Serial.print(lightMe);
  Serial.println();
  delay(100);

}

I couldn't get the moment at which you call the function "MotorCtrl".

Hi,

I can't believe I forgot to call the function. Thank you, I was reading the program over and over thinking that maybe one of my values was incorrect. It is working now I just have to master that Ping sensor. Thanks again.