#include <Servo.h>
const int servopin[] = {8, 9, 10}; // array of servo pins
const int servonum = sizeof(servopin) / sizeof(servopin[0]); // number of servos/PWM pins
Servo myservo[servonum]; // declare number of Servo objects/instances
const int maxD = 175; // limits the maximum range of the servo's movement to 175
const int minD = 5; // limits the minimum range of the servo's movement to 5
const int move = 2; // distance to move servo
int pos[] = {85, 85, 85}; // array of servo angles
const int buttonpin[] = {2, 3, 4, 5, 6, 7};
const int buttonnum = sizeof(buttonpin) / sizeof(buttonpin[0]);
unsigned long buttonTimer, buttonTimeout = 50; // used in readButton() debounce timer
bool currentButtonState, lastButtonRead; // record button states
void setup() {
for (int i = 0; i < servonum; i++) { // count servos
myservo[i].attach(servopin[i]); // attach servos to pins
myservo[i].write(pos[i]); // start position
}
for (int i = 0; i < buttonnum; i++) { // count pins
pinMode(buttonpin[i], INPUT_PULLUP); // sets the state of the pins to input mode
}
}
void loop()
{
for (int i = 0; i < buttonnum; i++)
debounceButton(i);
// The following routine handles what happens if the first set of push buttons are pressed
if (leftPressed) {
if (pos1 < maxD)
pos1 += move;
myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’
}
if (rightPressed) {
if (pos1 > minD)
pos1 -= move;
myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’
}
// The following routine handles what happens if the second pair of push buttons are pressed
if (leftPressed2) {
if (pos2 < maxD)
pos2 += move;
myservo2.write(pos2);
}
if (rightPressed2) {
if (pos2 > minD)
pos2 -= move;
myservo2.write(pos2);
}
// The following routine handles what happens if the third pair of push buttons are pressed
if (leftPressed3) {
if (pos3 < maxD)
pos3 += move;
myservo3.write(pos3);
}
if (rightPressed3) {
if (pos3 > minD)
pos3 -= move;
myservo3.write(pos3);
}
}
void debounceButton(int buttonPin) {
bool currentButtonRead = digitalRead(buttonPin); // read button pin
if (currentButtonRead != lastButtonRead) { // if THIS read is not the same as LAST read...
buttonTimer = millis(); // ...start a timer by storing millis()
lastButtonRead = currentButtonRead; // ... and store current state
}
if ((millis() - buttonTimer) > buttonTimeout) { // if button change was longer than debounce timeout (NOT NOISE)
if (currentButtonState == HIGH && lastButtonRead == LOW) { // ... and State is "NOT pressed" while Button "IS PRESSED"
//================================================== // Button WAS pressed... Do something.
Serial.print(++buttonCount); // ... an action...
milli = millis();
Serial.print(". this press = ");
Serial.print(milli);
Serial.print(", last press = ");
Serial.print(oldmilli);
Serial.print(", diff = ");
Serial.println(milli - oldmilli);
digitalWrite(LED_BUILTIN, LOW); // ... or call functions
oldmilli = milli;
//==================================================
}
currentButtonState = currentButtonRead; // update button state for first if() condition test
}
}