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);
}