#include <slowServo.h>
#include <mapper.h>
#include <mechButton.h>
// THIS CODE RELIES ON THE LC_baseTools & LC_slowServo libraries.
// those should be installed using the Arduino library manager.
slowServo aServo(2); // Create a slow servo given a pin number.
mechButton theBtn(4); // Create a mechanical button given a pin number.
int savedDir; // We'll need to save where we were heading.
void setup() {
aServo.begin(); // You need to call begin for each servo.
aServo.setMsPerDeg(5); // We want to go at least a tiny bit slower than WOT.
theBtn.setCallback(btnClick); // When the button changes state, call this function.
savedDir = 180; // We'll start off heading to 180 deg.
}
// When button changes state, this is called.
void btnClick(void) {
if (!theBtn.getState()) { // Button clicked down.
aServo.setDeg(savedDir); // Set the servo direction.
} else { // Button clicked up.
savedDir = aServo.desiredDeg; // Save what we were doing..
aServo.stop(); // Stop the servo.
}
}
void loop() {
idle(); // idle(); Runs everything.
if (!theBtn.getState()) { // Button held down!
if (aServo.desiredDeg==180) { // We heading to 180?
if (aServo.currentDeg==180) { // We already got there?
aServo.setDeg(0); // reverse to 0.
}
} else { // Oh, we're heading to zero?
if (aServo.currentDeg==0) { // And we already arrived?
aServo.setDeg(180); // Then head back to 180.
}
}
}
}
Another example of the slowServo class.