hi all
I'm new to Arduino and I'm trying to make a robot controlled by three servos that is light sensitive ( walks toward light when light is shined on but still move when there is no light) and stop its motion (servos and photcells) after 40 seconds. My servos work but do not stop after 40 seconds. I was wondering any one could help me the motion stop after 40 seconds and will my code work for the photocells.
thanks
#include <Servo.h>
Servo FrontServo; // create servo object to control a servo
Servo MiddleServo; // create servo object to control a servo
Servo RearServo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int val; // Variable to store the photocell reading
int val2; // Variable to store the photocell2 reading
int startTime = 0; // starting time = 0 seconds
int time; // variable time
void setup()
{
FrontServo.attach(3); // attaches the servo on pin 9 to the servo object
MiddleServo.attach(5); // attaches the servo on pin 9 to the servo object
RearServo.attach(12); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(0); // Reading of photocellPin
val = map(val, 0, 1023, 0, 179); // Maps the value of the photocell from 0-1023 then from 0 to 179
FrontServo.write(val); // Tell FrontServo to go to position in variable 'val'
delay(15); // Waits 15ms for the photocell to read the value (.015 seconds)
val2 = analogRead(1); // Reading of photocellPin2
val2 = map(val, 0, 1023, 0, 179);// Maps the value of the photocell2 from 0-1023 then from 0 to 179
RearServo.write(val2); // Tell RearServo to go to position in variable 'val2'
delay(15); // Waits 15ms for the photocell2 to read the value (.015 seconds)
for(pos = 60; pos <= 120; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
FrontServo.write(pos); // tell servo to go to position in variable 'pos'
MiddleServo.write(pos); // tell servo to go to position in variable 'pos'
RearServo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 30ms for the servo to reach the position
time = millis(); // this gives current time
Serial.print("time: "); // print the time
Serial.println(time); // print ln of time
if (time >= (40000 + startTime)) // if the time is greater then 40 seconds (40000 ms)
{
void end(); // then end
FrontServo.attach(3); // front servo attached to digital pin 3
MiddleServo.attach(5); // front servo attached to digital pin 5
RearServo.attach(12); // front servo attached to digital pin 12
val = analogRead(0); // photocell atached to anaolog pin 0
val2 = analogRead(1); // photocell atached to anaolog pin 1
}
}
}