I wrote the following code the allow a line follower robot to make a gradual turn for a certain amount of time. the gradual turn works without a delay but as soon as I add it in it no longer works and the robot goes straight................ any advice?
/*
PWM with delay
*/
int speed = 1;
void setup() {
pinMode(11, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
speed = digitalRead(5);
speed = speed*(55/255);
digitalWrite(5, speed);
digitalWrite(5, HIGH);
digitalWrite(11, HIGH);
delay(3000);
Try walking around by closing your eyes for three seconds at a time, and then opening them for about 300 microseconds.
Let is know how that works, should you survive long enough.
pinMode(5, OUTPUT);
}
void loop() {
speed = digitalRead(5);
Please explain.
Let's run your code on paper:
//first time through loop()
speed = digitalRead(5); // output pin 5 defaults to LOW after pinMode
speed = speed*(55/255); // speed is now 0 (LOW) (0*(55/255))
digitalWrite(5, speed); // write 5 LOW
digitalWrite(5, HIGH); // write 5 HIGH
digitalWrite(11, HIGH); // write 11 HIGH
delay(3000); // wait 3 sec.
//next time through loop()
speed = digitalRead(5); // speed = HIGH
speed = speed*(55/255); // speed = 1*(55/255)
digitalWrite(5, speed); // since speed is non zero write 5 HIGH
digitalWrite(5, HIGH); // write 5 HIGH
digitalWrite(11, HIGH); // write 11 HIGH
delay(3000); // wait 3 sec.
// third time through loop()
speed = digitalRead(5); // speed = HIGH
speed = speed*(55/255); // speed = 1*(55/255)
digitalWrite(5, speed); // since speed is non zero write 5 HIGH
digitalWrite(5, HIGH); // write 5 HIGH
digitalWrite(11, HIGH); // write 11 HIGH
delay(3000); // wait 3 sec.
// and so on. Pin 5 is never low after the first time through loop
speed = speed*(55/255);
digitalWrite(5, speed);
The analogWrite() function is for PWM.