Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: herehere).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; switch (angle) { case 150: case 65: angle = 15; myservo.write(angle); break; case 15: angle = 35; myservo.write(angle); break; case 35: angle = 65; myservo.write(angle); break; } } } 

EDIT: Add variable assignment angle in conditional expression.

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; switch (angle) { case 150: case 65: angle = 15; myservo.write(angle); break; case 15: angle = 35; myservo.write(angle); break; case 35: angle = 65; myservo.write(angle); break; } } } 

EDIT: Add variable assignment angle in conditional expression.

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; switch (angle) { case 150: case 65: angle = 15; myservo.write(angle); break; case 15: angle = 35; myservo.write(angle); break; case 35: angle = 65; myservo.write(angle); break; } } } 

EDIT: Add variable assignment angle in conditional expression.

Fixed the indent and made loop start at 150 like OP has. + set angle values
Source Link
user3704293
  • 471
  • 7
  • 20

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; switch (angle) { case 150: case 65: angle = 15; myservo.write(15angle); break; case 15:  angle = 35;  myservo.write(35angle); break; case 35: angle = 65;  myservo.write(65angle); break; } } } 

EDIT: Add variable assignment angle in conditional expression.

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; switch (angle) { case 150: case 65: myservo.write(15); break; case 15: myservo.write(35); break; case 35: myservo.write(65); break; } } } 

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; switch (angle) { case 150: case 65: angle = 15; myservo.write(angle); break; case 15:  angle = 35;  myservo.write(angle); break; case 35: angle = 65;  myservo.write(angle); break; } } } 

EDIT: Add variable assignment angle in conditional expression.

Fixed the indent and made loop start at 150 like OP has.
Source Link

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 65;150;  void setup() {   myservo.attach(9);   myservo.write(angle);  }  void loop() {   unsigned long currentMillis = millis();     if (currentMillis - previousMillis >= interval) {   previousMillis = currentMillis;     switch (angle) {   case 150: case 65:   myservo.write(15);   break;   case 15:   myservo.write(35);   break;   case 35:   myservo.write(65);   break; }   } } 

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 65;  void setup() {   myservo.attach(9);   myservo.write(angle);  }  void loop() {   unsigned long currentMillis = millis();   if (currentMillis - previousMillis >= interval) {   previousMillis = currentMillis; switch (angle) { case 65: myservo.write(15); break; case 15: myservo.write(35); break; case 35: myservo.write(65); break; }   } } 

If you want to change the servo every second, try to use a conditional expression in the timing structure. If you want to use more parallel structure with multiple intervals use different counters (example see: here).

Here is an example of the every second version:

#include <Servo.h> Servo myservo; unsigned long previousMillis = 0; const long interval = 1000; int angle = 150; void setup() { myservo.attach(9); myservo.write(angle); } void loop() { unsigned long currentMillis = millis();   if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis;     switch (angle) {   case 150: case 65:   myservo.write(15);   break;   case 15:   myservo.write(35);   break;   case 35:   myservo.write(65);   break; } } } 
Source Link
user3704293
  • 471
  • 7
  • 20
Loading