-
-
Save sankarcheppali/63c0c93270846370d780980cb90a69e8 to your computer and use it in GitHub Desktop.
Arduino ESP32 servo control from serial terminal
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 SERVO_SIGNAL_PIN 12 | |
Servo serialServo; | |
void setup() | |
{ | |
Serial.begin(115200); | |
Serial.println("Serial servo"); | |
serialServo.attach(SERVO_SIGNAL_PIN); | |
} | |
void setServoAngle(int angle){ | |
serialServo.write(angle); | |
} | |
void sweep(){ | |
for (int angle = 0; angle <= 180; angle++) { | |
setServoAngle(angle); | |
delay(16); | |
} | |
for (int angle = 180; angle >= 0; angle--) { | |
setServoAngle(angle); | |
delay(16); | |
} | |
} | |
void loop() | |
{ | |
if (Serial.available()) { | |
String inputString = Serial.readStringUntil('\n'); | |
if(inputString.startsWith(":")){ | |
String angleStr = inputString.substring(1); // extract the angle | |
int angle = strtol(angleStr.c_str(), NULL, 10); // convert str to int | |
Serial.print("Received angle set cmd "); | |
Serial.println(angle); | |
if(angle>=0 && angle<=180){ | |
setServoAngle(angle); | |
} else { | |
Serial.println("recevied invalid angle"); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment