How to wire this same code but make it non blocking run in the back ground
#include <Servo.h>
Servo myservo; // create Servo object to control a servo
// twelve Servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the Servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
Ok so pwm is non blocking it's the delay I guess causing the problem see i have added a reed switch that it sweeps back and forth over but it miss reads
#include <Servo.h>
Servo myservo; // create Servo object to control a servo
const unsigned long MsecPeriod = 15;
unsigned long msec0;
int pos = 0; // variable to store the servo position
int dir = 1;
// -----------------------------------------------------------------------------
void loop( )
{
unsigned long msec = millis ();
if (msec - msec0 >= MsecPeriod) {
msec += MsecPeriod;
pos += dir;
if (0 == pos || 180 == pos)
dir = -dir;
myservo.write(pos);
}
}
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the Servo object
}
Well I'm not getting anywhere fast just the servo it sweeps way to fast
here what i tried
#include <Servo.h>
Servo myservo; // create Servo object to control a servo
// twelve Servo objects can be created on most boards
int pos = 0; // variable to store the servo position
unsigned long previousMillis = 0;
const long interval = 1000; // interval in milliseconds
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the Servo object
}
void loop() {
unsigned long currentMillis = millis();
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
}
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
}
}
}
you still have the for loop... and currentMillis does not even update so...
try to write code going only one direction as a start and don't use a for() loop, just a if
it should look like this
void loop() {
// if it's time to take a step
if (millis() - lastStepTime >= 15) {
// then go to the new position
myservo.write(pos);
// prepare the future position
pos++;
// ensure the future position is OK
if (pos > 180) {
...
}
// make a note of the time you took the step
lastStepTime = millis();
}
}
when you reach 180 you should update a flag that should be taken into account to not execute this again
void loop() {
//unsigned long lastStepTime = millis();
// if it's time to take a step
if (millis() - lastStepTime >= 15) {
// then go to the new position
myservo.write(pos);
// prepare the future position
pos++;
// ensure the future position is OK
if (pos > 180) {
}
// make a note of the time you took the step
lastStepTime = millis();
}
if (millis() - lastStepTime >= 15) {
// then go to the new position
myservo.write(pos);
// prepare the future position
pos--;
// ensure the future position is OK
if (pos < 180) {
}
// make a note of the time you took the step
lastStepTime = millis();
}
This is a near-exact copy of post #5 with some descriptions.
#include <Servo.h>
byte servoPin = 9; // define pin to control servo
Servo myservo; // create Servo object to control
unsigned long timer; // keep track of time between steps
unsigned long nextStep = 100; // 100 ms between steps ADJUST THIS FOR FASTER/SLOWER STEPS
int dir = 1; // direction of sweep
int pos = 0; // initial position of horn
void setup() {
myservo.attach(servoPin); // arm object with servo control pin
}
void loop() {
if (millis() - timer > nextStep) { // is it time to step again?
timer = millis(); // yes, and set new timer for next step
pos += dir; // change the postion of the horn
if (pos >= 180 || pos <= 0) // is the horn at the end of the sweep?
dir = -dir; // yes, change sweep direction
myservo.write(pos); // MOVE THAT HORN
delay(15); // time to allow horn to move from one position to the next
}
}