Go Down

Topic: Servo Control via Serial USB (Read 729 times) previous topic - next topic

GEPerr

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: [Select]
#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();
          }   
      }
  }

GEPerr

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

Code: [Select]

           AngValA = map(AngValA, 0, 180, 0, 180);


PaulS

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.

Go Up