Pages: [1]   Go Down
Author Topic: Servo Control via Serial USB  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please forgive my novice coding skills here.  I'm trying to create a servo control that takes in angles input from the Serial Monitor, and turns the servo to that given position.  For some reason, whenever I try to type in a number with two digits, the code reads that as two separate numbers and turns the servo to each digit as an angle (from 0 to 9 degrees).  I suspect this is a simple coding issue, but any help or advice would be appreciated.  Code is appended below.

GEP

Code:

Code:
#include <Servo.h>

Servo ServoA;

void setup()
  {
    Serial.begin(9600);
    ServoA.attach(9);

    Serial.print('Ready for angle prompt!');
    Serial.println();

    Serial.print("Please assign an angle for Servo A [0 to 180 degrees]");
    Serial.println();
  }
 
void loop()
  {
    if (Serial.available() > 00)
      {
          int AngValA = Serial.read();
          if (AngValA >= '0' && AngValA <= '180' )
          {
           AngValA = AngValA - '0';
           AngValA = map(AngValA, 0, 180, 0, 180);
           ServoA.write(AngValA);
           Serial.print("Servo A set to: ");
           Serial.print(AngValA, DEC);
           Serial.println();
          }   
      }
  }
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, just realized i left in a line of junk there.  Ignore the line;

Code:
           AngValA = map(AngValA, 0, 180, 0, 180);
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What you type in the Serial Monitor are not numbers. It is strings of text. Those strings of text get sent to the serial port, and read one character at a time by the Arduino.

I find it difficult to believe, since this questions is asked at least twice a week, that you could not find a solution while searching the forum.
Logged

Pages: [1]   Go Up
Jump to: