/*
Forum: https://forum.arduino.cc/t/mehrere-servos-mit-taster/1402415/4
Wokwi: https://wokwi.com/projects/439369305035539457
2025/08/15
Sketch von my_xy_projekt
umgesetzt und (nur leicht) angepasst durch ec2021
- Konstante shortWaitTime und longWaitTime eingeführt
- shortWaitTime von 100 auf 500 ms hochgesetzt, damit die Zeit für die Servos genügt
- Grüne Led eingeführt, die anzeigt, wann der Tastendruck (wieder) akzeptiert wird
*/
// Forensketch
// https://forum.arduino.cc/
#include <Streaming.h> // https://github.com/janelia-arduino/Streaming
#include <Servo.h>
Servo servoOne;
Servo servoTwo;
Servo servoThree;
Servo servoFour;
constexpr uint8_t ledPin {3};
constexpr uint32_t shortWaitTime {500};
constexpr uint32_t longWaitTime {10000};
constexpr uint8_t sNums {4};
Servo myServo[sNums] = {servoOne, servoTwo, servoThree, servoFour};
constexpr uint8_t servoPin[sNums] {9, 10, 11, 12}; // angeschlossene Servo Pins
constexpr uint8_t tasterPin {2};
uint8_t schritt = 0;;
uint32_t stepStart; // Merker für Start
uint32_t stepTime; // Laufzeit des jeweiligen Step
void setup()
{
Serial.begin(115200);
delay(300);
Serial << (F("\r\nStart...\r\n")) << endl;
for (byte b = 0; b < sNums; b++) // zählt durch alle Servos
{
myServo[b].attach(servoPin[b]); // Zuweisung der Pins
}
pinMode(tasterPin, INPUT_PULLUP); // Taste schaltet nach GND
pinMode(LED_BUILTIN, OUTPUT); // Kontrollleuchte
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
}
void loop()
{
schrittKette(); // Abaufsteuerung
heartBeat(500); // lässt die onBoardLed blinken
}
void setServo(const uint8_t sNum, const uint8_t winkel, const uint32_t myTime)
{
Serial << F("Servo: ") << sNum + 1 << F(" Winkel: ") << winkel << "°" << F(" mit Laufzeit: ") << myTime << "ms" << endl;
myServo[sNum].write(winkel);
stepStart = millis();
stepTime = myTime;
}
bool timeIsEnd()
{
if (millis() - stepStart >= stepTime)
{
Serial << F("Schrittzeit abgelaufen") << endl;
return 1;
}
return 0;
}
void schrittKette()
{
switch (schritt)
{
case 0: // Warten auf Tastendruck
if (!digitalRead(tasterPin)) // Taste ausgelöst
{
digitalWrite(ledPin, LOW);
setServo(0, 180, shortWaitTime); // Servonummer, Winkelstellung, Haltezeit
schritt++; // nächster Schritt
}
break;
case 1:
if (timeIsEnd())
{
setServo(0, 0, longWaitTime);
schritt++; // nächster Schritt
}
break;
case 2:
if (timeIsEnd())
{
setServo(1, 180, shortWaitTime);
schritt++; // nächster Schritt
}
break;
case 3:
if (timeIsEnd())
{
setServo(1, 0, longWaitTime);
schritt++; // nächster Schritt
}
break;
case 4:
if (timeIsEnd())
{
setServo(2, 180, shortWaitTime);
schritt++; // nächster Schritt
}
break;
case 5:
if (timeIsEnd())
{
setServo(2, 0, longWaitTime);
schritt++; // nächster Schritt
}
break;
case 6:
if (timeIsEnd())
{
setServo(3, 180, shortWaitTime);
schritt++; // nächster Schritt
}
break;
case 7:
if (timeIsEnd())
{
setServo(3, 0, longWaitTime);
schritt++; // nächster Schritt
}
break;
case 8:
if (timeIsEnd())
{
setServo(3, 180, shortWaitTime);
schritt++; // nächster Schritt
}
break;
case 9:
if (timeIsEnd())
{
setServo(3, 0, longWaitTime);
schritt++; // nächster Schritt
}
break;
case 10:
if (digitalRead(tasterPin))
{
schritt = 0;
digitalWrite(ledPin, HIGH);
}
break;
}
}
void heartBeat(const uint32_t switchTime)
{
static uint32_t lastTik = 0;
if (millis() - lastTik >= switchTime)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
lastTik = millis();
}
}