123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #include <ESP32Servo.h> //Require ESP32Servo
- #define STP_DATA_PIN 4
- #define STP_CLOCK_PIN 16
- #define STP_LATCH_PIN 17
- #define SERVO_SWITCH 27
- #define SERVO_COVER 14
- #define TOGGLE_SWITCH 13
- #define SERVO_ALIGNMENT_OFFSET 3
- Servo servoSwitchPusher;
- Servo servoCoverPusher;
- bool getSwitchState() {
- int switchState = digitalRead(TOGGLE_SWITCH);
- return (switchState == 1);
- }
- void pushSwitchDelayed(int coverDelay, int pusherDelay) {
- servoCoverPusher.write(90);
- delay(coverDelay);
- servoSwitchPusher.write(130 + SERVO_ALIGNMENT_OFFSET);
- delay(pusherDelay);
- servoCoverPusher.write(0);
- servoSwitchPusher.write(0);
- }
- void writeStep(byte stepByte) {
- shiftOut(STP_DATA_PIN, STP_CLOCK_PIN, MSBFIRST, stepByte);
- digitalWrite(STP_LATCH_PIN, HIGH);
- digitalWrite(STP_LATCH_PIN, LOW);
- delayMicroseconds(840);
- }
- void standbySteppers() {
- writeStep(0b00000000);
- }
- void setup() {
- Serial.begin(115200);
-
- ESP32PWM::allocateTimer(0);
- ESP32PWM::allocateTimer(1);
- ESP32PWM::allocateTimer(2);
- ESP32PWM::allocateTimer(3);
-
-
- servoSwitchPusher.setPeriodHertz(50);
- servoCoverPusher.setPeriodHertz(50);
- servoSwitchPusher.attach(SERVO_SWITCH);
- servoCoverPusher.attach(SERVO_COVER);
- servoCoverPusher.write(0);
- servoSwitchPusher.write(0);
-
- delay(1000);
- servoSwitchPusher.write(125);
-
- pinMode(STP_DATA_PIN, OUTPUT);
- pinMode(STP_CLOCK_PIN, OUTPUT);
- pinMode(STP_LATCH_PIN, OUTPUT);
- standbySteppers();
- }
- void loop() {
-
-
- bool switchPushed = getSwitchState();
- if (switchPushed) {
- servoSwitchPusher.write(0);
- Serial.println("Switch pushed");
- delay(1000);
- pushSwitchDelayed(300,300);
- delay(3000);
- servoSwitchPusher.write(125);
- } else {
- Serial.println("Switch idle");
- }
- delay(500);
- }
|