#include <Servo.h>
Servo servo;
byte servoPin = 2, speakPin = 3;
int direction = 1, degree, count;
unsigned long timer, timeout = 1000 / 180; // 180 makes 100 "ticks" take 110 seconds
void setup() {
Serial.begin(115200);
servo.attach(servoPin);
pinMode(speakPin, OUTPUT);
}
void loop() {
if (millis() - timer > timeout) {
timer = millis();
degree += direction;
if (degree == 0 || degree == 180) {
direction = -direction;
}
servo.write(degree);
if (degree == 90) {
Serial.print(++count);
Serial.print(" ");
tone(3, 220, 10); // pin, Hz, duration
delay(10);
digitalWrite(speakPin, LOW);
}
}
}