Kub_Luk
Published

Using Serial Monitor to Control Servo Motor

In this project, you can learn how to control servo motor using serial monitor.

BeginnerFull instructions provided63,756
Using Serial Monitor to Control Servo Motor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Servo Module (Generic)
×1
Male/Male Jumper Wires
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Servo schematics

Code

Code

C/C++
#include <Servo.h>
Servo myservo;
int pos = 0;



void setup()

{

Serial.begin(9600);
while (!Serial);
Serial.println("-------------------------");
Serial.println("ARos is loading....");
delay(1000);
Serial.println("ARos loaded succesfully");
Serial.println("-------------------------");
myservo.attach(9);
Serial.println("calibrating servo...");
for(pos = 0; pos <= 180; pos += 1)
myservo.write(0);
delay(1000);
myservo.write(180);
delay(1000);
myservo.write(90);
delay(1000);
Serial.println("servo calibrated");
Serial.println("-------------------------");
Serial.println("Comand input online, write command to perform action");
Serial.println("-------------------------");

}

void loop() {
  
for(pos = 0; pos <= 180; pos += 1)
if (Serial.available())


{
  int state = Serial.parseInt();
    
if (state < 10)

{
Serial.print(">");
Serial.println(state);
Serial.println("cannost execute command, too low number");

}

if (state >= 10 && state < 170)
{
  Serial.print(">");
  Serial.println(state);
  Serial.print("turning servo to ");
  Serial.print(state);
  Serial.println(" degrees");
  myservo.write(state);
  
}

}

}



  

Credits

Kub_Luk

Kub_Luk

1 project • 9 followers

Comments