Hackster is hosting Hackster Holidays, Ep. 5: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 5 on Friday!
Hack star
Published © MIT

Arduino Simulator - Control Servo motor angle Serial Monitor

Learn how to control the servo motor angle using the serial terminal of Arduino on the Wokwi Arduino simulator!

BeginnerProtip1 hour8,365

Things used in this project

Story

Read more

Schematics

Servo motor and Arduino connection - Arduino Simulator

Code

Control servo motor using Serial monitor - Arduino simulator projects

Arduino
Control servo motor using Serial monitor - Arduino simulator projects - user will enter the angle needed in serial monitor window and then press ENTER key.
#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("Cannot execute command, too low number");
        Serial.read();
        Serial.read();

      }

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


      }

    }

}

Credits

Hack star

Hack star

75 projects • 123 followers
an Arduino enthusiast and an electronic hobbyist

Comments