I'm trying to use my phone (iOS) to control a servo but the input is coming in as J0: ___, ___. How would I be able to take the first value as it ranges from 0-360 and make move the servo. I'm still very new to coding.
Good question. What is the rest of the story?
Welcome to the forum
Have you got control over exactly what the 'phone sends ?
If not, then, yes, you could parse out the numeric value near the start of the message for use in the sketch by using Serial.parseInt(). I am confused by your mention of a value of 0-360 to control a servo. Are you expecting the servo to move between 0 and 360 degrees ?
I'm trying to code a Boe-bot to make it move through my phone using a joystick. I'm also using an Hc-05 module if that helps. I can do the code I just don't know how to separate the value from the statement my phone sends.
Will the value always be in the same position ?
A small sketch for you to experiment with by inputting data into the Serial monitor
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available())
{
int x = Serial.parseInt();
Serial.println(x);
}
}
So you get text over the Serial line that looks like J0: XXX, YYY and you want to extract XXX and YYY ?
I would suggest to study Serial Input Basics to handle receiving the text into a buffer
and once it's in the buffer, you could do
int x, y;
char buffer[50]; // where the command gets into
...
// read the command
...
// parse the command
if (sscanf(buffer, "J0: %d, %d", &x, &y) == 2) { // could we extract 2 values with that format?
// Yes — here x and y are initialized with the 2 values
...
} else {
// No — buffer was not containing something matching the format `J0: XXX, YYY`
...
}
thanks so much I figured it out now.
I am glad you have got it working. What approach did you take ?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.