// Controlling multiple servo motors,
// by updating them every 10ms or 20ms.
//
// Version 1, July 25, 2021 by Koepel, Public Domain.
// Version 2, October 7, 2023 by Koepel, Public Domain.
// Added pinMode() to setup() for the two buttons.
#include <Servo.h>
unsigned long previousMillis;
const unsigned long interval = 10; // 10 or 20 milliseconds are common values
Servo servo2;
int count700ms = 0; // count to 700ms for first servo motor
float r = 0.0; // the angle for the sinus in radian for second servo motor
void setup()
{
servo2.attach(7);
}
void loop()
{
unsigned long currentMillis = millis();
if( currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
// ------------------------------------------
// Servo motor
// A smooth sinus sweep
// The speed is controlled by the first potentiometer
// ------------------------------------------
float s = sin( r); // The sine from the angle in radian
float t = (90.0 * s) + 90.0; // convert -1...1 to 0...180
int angle2 = int( t); // convert it to integer for the servo angle
servo2.write( angle2);
int potentiometer1 = analogRead( A0);
float r_increment = 0.001 + (0.00005 * float(potentiometer1));
r += r_increment;
if( r >= 2.0 * M_PI)
{
r -= 2.0 * M_PI;
}
}
}