I want to operate Servo Motor with millis function.
Servo Motor will rotate by 20deg (Initial Position)
After 5 second
If Servo Motor Angle is less than 90deg then it will rotate to 120deg
if Servo Motor Angle is more than 90 deg then it will rotate to 30deg
loop continues after every 5 sec
I have written following code, but its not working. Please suggest the corrections.
#include <Servo.h>
int ANGLE = 0;
Servo servo_9;
unsigned long previousMillis = 0;
const long interval = 5000;
Sorry, actually couldnt understand.
I updated the code as per following and it gives some result,
But still I want to set some initial position for servo motor and then start this loop.
#include <Servo.h>
Servo servo_9;
unsigned long previousMillis = 0;
const long interval = 5000;
void setup()
{
servo_9.attach(9, 500, 2500);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (servo_9.read() < 90) {
servo_9.write(120);
} else {
servo_9.write(30);
}
delay(5000); // Delay a little bit to improve simulation performance
}
}
#include <Servo.h>
Servo servo_9;
unsigned long previousMillis = 0;
const long interval = 5000;
void setup()
{
servo_9.attach(9, 500, 2500);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
if (servo_9.read() < 90) {
servo_9.write(120);
} else {
servo_9.write(30);
}
previousMillis = currentMillis; //previousMillis needs to be updated. (not in your original code)
delay(1); // <----- not really needed (unless this is really a PC simulation) since you are using millis() already!!!!
}
}