Hello to everybody
Does anyone know how to write a code for servo motor with arduino that rotates a certain amount every minute, for example, it rotates 2 degrees every 5 minutes. Like a clock
Start by picking up just any example code exercising a servo. Then modify that code to do more and more of what You want..
The Servo library (included with the IDE) for controlling the servo.
Some tutorials on non blocking timing with millis() for timing.
Several things at a time.
Beginner's guide to millis().
Blink without delay().
What part of a clock rotates 2° every five minutes?
Please remember that the "angle" specified in a Servo write may vary from servo to servo, and servos rarely rotate 360°
Are you using SG-90 Servo Motor like this:
If you want to rotate continually like a clock then a servo is the wrong device. Most servos only have a finite range of rotation, 120, 180, 270 degrees then they hit an end stop.
There are things called 360 degree or continuous rotation servos but they have no positional control. You can make them move faster or slower but NOT move a specific number of degrees.
So what EXACTLY is it that you are trying to achieve? It sounds like a stepper motor would be much nearer to what you want.
Steve
Or, again depending on the actual use, a DC gear motor with an encoder.
yse.SG90
Thank you so much
Try the following test sketch; where the shaft moves by 2 degrees in every second.
#include <Servo.h>
Servo myservo;
byte n = 0;
void setup()
{
myservo.attach(9); //signal line of Servi is with DPin-9
myservo.write(0); //initial position of Servo at 0 degrees.
}
void loop()
{
unsigned long prMillis = millis();
while(millis() - prMillis<1000UL)
{
;
}
n = n+2; //1-sec has elapsed; turn servo by 2 degrees
myservo.write(n);
if(n == 180) //Servo has reached at 180 degrees position; reset to 0 position
{
myservo.write(0);
n = 0;
}
}
That's the same as
delay(1000);
Why making it such complicated? Or use millis() the right way.
In the immortal words of ex-PFC Wintergreen, "Too prolix"
I agree.
You could have provided a modified blink-without-delay; it would have been more helpful.
I had difficulties to asses @hg1001's level of programming and understanding; so, I have just presented a solution, in line with his request of Post-1, which he could follow. However, codes for time delay using millis() function was totally inappropriate. As pointed out in Post-11, it could had been done by delay(1000) function.
And even shorter
#include <Servo.h>
Servo myservo;
byte n = 0;
unsigned long prMillis;
void setup()
{
myservo.attach(2); //signal line of Servi is with DPin-9
myservo.write(0); //initial position of Servo at 0 degrees.
}
void loop()
{
if (millis() - prMillis > 1000UL)
{
prMillis += 1000UL;
n = n + 2; //1-sec has elapsed; turn servo by 2 degrees
myservo.write(n);
if (n >= 180) //Servo has reached at 180 degrees position; reset to 0 position
{
myservo.write(0);
n = 0;
}
}
}
Wether @hg1001 does understand it ... ? But he should give it a try - understandig how to write non blocking code is one of the most important thing with arduino programming.
Close
In UART learning of string reception with end mark, I had to start with blocking code to get the confidence level of the pupils that the UART Port indeed works and then I assigned them the job of creating a sketch using non-blocking code.
So, which one is important that depends on the situation.
While the @hg1001 has hardly any knowledge on programming, we are enganged in competitions among ourselves as to which one is optimum.
Why not asking the @hg1001 to cretae a sketch that turns the Servo by 30 degress?
I totally agree.
My version is based on yours with millis(). I think, if you're going to use millis(), than use it properly. If your version had worked with delay() from the beginning, my millis() version would probably not have been created here at all.
But we had clarified this millis() <-> delay() problem in the meantime