I've created a Arduino sketch using an HW-201 IR Sensor and a Micro Servo Motor. I'm able to control the speed of the servo to go from 0 to 90 degrees once the Sensor detects and object. I can also control the amount of time it the servo stays at 90 degrees before returning to 0 degrees.
But I can't seem to control the speed for the return of the Servo back to 0 degrees. Can anyone tell me where I've gone wrong with this sketch? I get no compiling errors and it uploads to the Arduino Uno board with no issues. I've tried for a week changing and altering different parts but still no luck. HELP Please.
#include <Servo.h>
Servo servo;
const int analogInPin = A0;
int val=0;
int servoPin=9;
int angle=90;
void setup() {
servo.attach(servoPin);
}
void loop() {
val = analogRead(analogInPin); // This reads the value of the sensor to see if an object is detected. Then saved in the val variable.
if(val < 600)
{
for (angle = 90; angle < 180; angle += 1)
{
servo.write(angle);
delay(6); // This controls the speed of the Servo (gate) opening. Lower# lower speed.
}
delay(3000); // Sets the delay time. 3000 means the Servo (gate) stays open for 3 seconds before returning.
for (angle = 0; angle < 90; angle += 1)
{
servo.write(angle); // If object detected the Servo rotates to 90 degrees.
delay(1); // This controls the speed of the Servo (gate) returning. Lower# lower speed.
}
}
}
this seems like the right approach. have you tried larger delay values? 90 6 msec steps is on ~half a second. and don't you want to used the same delay in both cases?
The delay value for the Servo to go from 0 to 90 works fine with any value I type.
The issue seems to be the delay in the last part of the sketch. This number messes up the servo return and or no speed control.
you mean in the above "part of the sketch"?
delay(1) seems kinda fast and may not be guaranteed to even be 1 if the resolution is +/-1.
what does is do with the same value in the other "part"?
The delay(6) section operates the servo correctly. A lower number apparently speeds up the rotation.
The delay(1) section has no effect on the servo speed but if I change the number from to 6 to be the same as both sections, the servo over rotates before returning to original position and still no speed control. I used to have a lot of hair.... but now I'm almost bald from pulling my hair out. I know there is an error in there I just don't know where.
you have a 3 sec delay "after" moving the servo to 90 deg but no delay "after" rotating it to zero, so it immediately repeats the upper part of the code
Greg, the 3 second delay works perfectly and after 3 seconds the servo will return to 0 degrees again but I still have no control of the returning speed. Even though the sketch code line says ......
delay(2) it doesn't seem to have any effect. If I change it to delay(1); no speed changes but still returns to original position after the 3 second delay. If I change the delay(2); to a 5 then the servo over rotates a few degrees before coming back to 0 its original position. I believe the error is somewhere in else other than in these delay settings. I just can't figure it out.
Greg... I appreciate your help so very much but if you want to leave it for now I understand.
i didn't notice this before but you first rotate the servo from 90 to 180 and the lower part from 0 to 90. i had assumed the lower part just moves form 180 back to 90.
is the movement to zero what you mean by over-rotates?
Then change the loop to go from 180 to 90. You will have to count backwards:
for (angle = 180; angle > 90; angle -= 1)
{
servo.write(angle); // If object detected the Servo rotates to 90 degrees.
delay(6); // This controls the speed of the Servo (gate) returning. Lower# lower speed.
}
This should go back from 180 to 90 at the same speed as the move from 90 to 180.
for (angle; angle > 90; angle -= 1)
{
servo.write(angle); // If object detected the Servo rotates to 90 degrees.
delay(6); // This controls the speed of the Servo (gate) returning. Lower# lower speed.
}
Would the for loop not start at the current value of angle? I don't have a servo handy at the moment to try.
Hi Greg, Roy here again. I live in Hong Kong so that is the reason for my response time being off a little. So far I've tried everything in this list and when I alter the value of angles as suggested above the servo does not rotate. I've copied and pasted your code but plenty of it is a little beyond my level of experience. some of it I have no idea of the syntax. But when I run the Arduino sketch to verify I get the error message as below:
/private/var/folders/zk/1_yd0rj946l4sg6xdl7611ww0000gn/T/.arduinoIDE-unsaved20221011-85098-13ig3yj.fcfrj/sketch_nov11a/sketch_nov11a.ino:39:11: error: two or more data types in declaration of 'loop'
/private/var/folders/zk/1_yd0rj946l4sg6xdl7611ww0000gn/T/.arduinoIDE-unsaved20221011-85098-13ig3yj.fcfrj/sketch_nov11a/sketch_nov11a.ino:50:1: error: expected declaration before '}' token
exit status 1
Compilation error: two or more data types in declaration of 'loop'
I have copied your sketch code and got this error message when verifying:
/private/var/folders/zk/1_yd0rj946l4sg6xdl7611ww0000gn/T/.arduinoIDE-unsaved20221011-85098-13ig3yj.fcfrj/sketch_nov11a/sketch_nov11a.ino:40:11: error: two or more data types in declaration of 'loop'
/private/var/folders/zk/1_yd0rj946l4sg6xdl7611ww0000gn/T/.arduinoIDE-unsaved20221011-85098-13ig3yj.fcfrj/sketch_nov11a/sketch_nov11a.ino:51:1: error: expected declaration before '}' token
exit status 1
Compilation error: two or more data types in declaration of 'loop'