I am a student working on building a robot with 10 fingers each controlled by an individual servo. My job in the project is to write an Arduino sketch that takes a string typed into the serial monitor and can interpret different parts of that string and control the corresponding servo. This is for the purpose of later using a GUI to control each servo.
My plan for this was to create an array of strings by splitting one long input and then parse through each individual string for information regarding the specific servo.
For example, a single servo needs the information about the pin we want to attach it to via the GUI and the angle we want it to turn. Thus, I would want to be able to type in the serial monitor "A90" where A is the first pin (pin 0) the servo is plugged into (to digitally attach it) and 90 is the angle we want the servo to turn. The idea is then that we can set all ten fingers to be in this format. Thus, an example string input in the serial monitor could be something like "A90 B90 C30 D90 E30 F30 G90 H30 I90 J90" for all 10 fingers. I was hoping to create an array with 10 boxes where the data for each finger is in each box of the array.
However, after that, is there a way to then parse through each individual substring to collect the information from each spot in the string?
I'm still getting accustomed to the Arduino IDE and C++ in general, so I have a limited amount of code written so far. Any help on how to approach this would be appreciated, and if you think there's a better way to tackle this problem please let me know.
Thank you for the replies. I now realize I am actually having issues even creating an array with the input data in the first place. How does one create an array of strings from input from the serial monitor?
John
What about the case where Serial times out? The user will have to be aware of the consequence.
The OP should also be aware of the physical limitations of his servos - many, while advertised as 180 degree, have usable ranges between 150-170 degrees; hence, commanding 0 or 180 could be problematic for his servos. It's always advisable to establish hard limits that are enforced within the Arduino, so that external commands that violate the limits are ignored.
A simple extension of this, if the OP is interested, would be to use lower case to signify a relative movement. For example, "A45" moves to 45 degrees, but "a-5" moves from where it is, to 5 degrees less.
Yep. You know, I know. I had hoped the OP might think it through without having to learn that the hard way. That's why I rambled on about the usable range, as often "0" is unachievable with cheap servos, resulting in servo lockup or death. Been there, done that, now I guard against it in code(I know, I could just spend more on servos, but it's a hobby for me).
I'm working on a similar project, except I'm using flex sensors to control the servos, I got it working, but one flex sensor controls all of the servos, instead of each being controlled by a different flex sensor. Here's the code: #include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
void setup()
{
pinMode(A0,INPUT);
pinMode(1,OUTPUT);
pinMode(A1,INPUT);
pinMode(2,OUTPUT);
pinMode(A2,INPUT);
pinMode(3,OUTPUT);
pinMode(A3,INPUT);
pinMode(4,OUTPUT);
pinMode(A4,INPUT);
pinMode(5,OUTPUT);
servo1.attach(1);
servo2.attach(2);
servo3.attach(3);
servo4.attach(4);
servo5.attach(5);;
}
void loop()
{
servo1.write(analogRead(A0)/6);
servo2.write(analogRead(A1)/6);
servo3.write(analogRead(A2)/6);
servo4.write(analogRead(A3)/6);
servo5.write(analogRead(A4)/6);
}
Maybe someone can help me understand why it's not working?
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
Loop() loops very quickly, thousands of times per second. I think that you are reading the ADC and updating the servos way too often. Try this code and see if it works better.