#include <Servo.h>
const int LED = 13; // Define LED (pin 13 is on-board led.)
bool LEDstate = LOW;
Servo servoA; // create servo objects to control
Servo servoB;
int posA = 0; // variables to store the servo positions
int incA = 1;
int delayA = 15;
int TimeA, prevTimeA;
//----------------------
int posB = 30;
int incB = 1;
int delayB = 2;
int TimeB, prevTimeB;
long counter = 0;
void setup() {
pinMode(LED, OUTPUT); // Defining LED pin as OUTPUT Pin
servoA.attach(3); // attaches the servoA on pin 3 to the servo object
prevTimeA = millis();
servoB.attach(6); // attaches the servoB on pin 6 to the servo object
prevTimeB = millis();
}
void loop() {
// This coded blinks on-board led to get some estimate of simulation speed.
// 1 million loops takes about 6 seconds. Real Arduino?
counter = counter + 1;
if (counter == 100000) {
LEDstate = not LEDstate;
counter = 0;
digitalWrite(LED, LEDstate);
}
// This block controls servoA
TimeA = millis();
if ((TimeA - prevTimeA) >= delayA) {
if (posA > 179) {
incA = -1;
delayA = 3;
}
if (posA < 1) {
incA = 1;
delayA = 15;
}
posA = posA + incA;
servoA.write(posA);
prevTimeA = millis();
}
// This block controls servoB
TimeB = millis();
if ((TimeB - prevTimeB) >= delayB) {
if (posB > 120) {
incB = -1;
delayB = 8;
}
if (posB < 30) {
incB = 1;
delayB = 2;
}
posB = posB + incB;
servoB.write(posB);
prevTimeB = millis();
}
/* //This is typical loop control of a servo. Blocking code isn't good.
for (posA = 180; posA >= 0; posA -= 1) { // goes from 180 degrees to 0 degrees
servoA.write(posA); // tell servo to go to position in variable 'pos'
delay(3); // waits 15ms for the servo to reach the position
}
*/
}