Skip to main content
3 of 4
Fixed the indent and made loop start at 150 like OP has. + set angle values
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(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.

user3704293
  • 471
  • 7
  • 20