#include <Servo.h>
int servoOnePin = 10;
class Scanner {
Servo servo;
int pos = 0;
int increment;
int updateInterval;
unsigned long lastUpdate;
public:
Scanner(int interval) {
updateInterval = interval;
increment = 10;
}
void Attach(int pin) {
servo.attach(servoOnePin);
servo.write(0);
}
void Update() {
if (Serial.available() > 0) {
char inChar = Serial.read();
switch (inChar) {
case 'a':
servo.attach(servoOnePin);
break;
case 'd':
servo.detach();
break;
case 'w': // pause and reverse
increment = -1;
break;
case 's': // pause and forward
increment = 1;
break;
}
}
unsigned long currentMillis = millis();
if (currentMillis - lastUpdate > updateInterval) {
lastUpdate = currentMillis;
pos += increment;
servo.write(pos);
Serial.println(pos);
if ((pos >= 180) || (pos <= 0)) {
increment = -increment;
}
}
}
};
Scanner scanner1(50); //where () is how often it repeats servo sweep
void setup() {
Serial.begin(9600);
Serial.println("booyah");
scanner1.Attach(10);
}
void loop() {
scanner1.Update();
}