Go Down

Topic: Servo comunication problem (Read 523 times) previous topic - next topic

Daniel_DSO

I have this code:

#include <Servo.h>

Servo motor;

char readInf;

void setup(){
  Serial.begin(9600);
  motor.attach(8);
}

void loop(){ 
   if(Serial.available() > 0) {
   
   readInf = Serial.read();
   
   motor.write(readInf);
   delay(550);
   motor.write(120);
   }
} ;

From Android, I send how many degrees the Servo have to work, but when it's read, the value comes like 'char' and accept only int values. The question is: How can I set a String value to "motor.write()"? Or, is there another way to do it?

Thanks!

zoomkat

Very basic servo test code that you can try.

Code: [Select]

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String
    int n = readString.toInt();  //convert readString into a number
    myservo.write(n);
    readString="";
  }
}


Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

JimboZA

It's so much easier to read code if you use the # tag just above the  ;) :smiley-sweat: smilies.

Without the code tags, some of the code gets interpreted as formatting so you get this:

  Serial.begin(9600);
  motor.attach(8);
}

..... not this:

Code: [Select]
  Serial.begin(9600);
  motor.attach(8);
}

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Go Up