-
-
Save sankarcheppali/da76e49f699c6b42c2f47b7468134c91 to your computer and use it in GitHub Desktop.
Control multiple servos with ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#include <Servo.h> | |
#define NUMBER_OF_SERVOS_ATTACHED 2 | |
int servoIoPins[NUMBER_OF_SERVOS_ATTACHED] = {12,22}; | |
Servo serialServos[NUMBER_OF_SERVOS_ATTACHED]; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("Sweep Servo"); | |
for(int i=0;i<NUMBER_OF_SERVOS_ATTACHED;i++){ | |
if(!serialServos[i].attach(servoIoPins[i])){ | |
Serial.print("Attach error "); | |
Serial.println(servoIoPins[i]); | |
} | |
} | |
} | |
void setServoAngle(Servo serialServo,int angle){ | |
serialServo.write(angle); | |
} | |
void sweep(){ | |
for (int angle = 0; angle <= 180; angle++) { | |
for(int i=0;i<NUMBER_OF_SERVOS_ATTACHED;i++){ | |
serialServos[i].write(angle); | |
} | |
delay(8); | |
} | |
for (int angle = 180; angle >= 0; angle--) { | |
for(int i=0;i<NUMBER_OF_SERVOS_ATTACHED;i++){ | |
serialServos[i].write(angle); | |
} | |
delay(8); | |
} | |
} | |
void loop() | |
{ | |
sweep(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment