Beginners: commented code for controlling a servo's position


#1

Here’s some more beginner code, this time from learning how to control servos. It’s in part adapted from Makeblock code in this case, but with more commenting to make it useful for people like me who are just figuring out how to write arduino code.

include (Makeblock.h) //A bunch of libraries you'll need to call. include (Arduino.h) //At this point, I see which libraries I need by process include (SoftwareSerial.h) //of elimination after writing code. Not very elegant. include (Wire.h) include (Servo.h) //With the following code, you can dial a servo around like a steering wheel.

MePort servoport(PORT_4); //Plug the MEPORT RJ-25 adapter into port 4.
Servo minianalogservo; //MBlock’s version of an MG-90 mini-servo
int servo1pin = servoport.pin2(); //hook up the servo to the second pin on the MEPORT
MePotentiometer knob(PORT_8); //initializes the potentiometer as KNOB
int val; //Val is the variable we’ll use for reading from KBOB and
//then for outputting the servo’s position.

void setup() //the SETUP function: void means it doesn’t return a value
{
minianalogservo.attach(servo1pin); //attaches the servo to what we’ve defined as pin 2 on the daughter board
}

void loop() //the main program LOOP function
{

val = knob.read(); //Reads knob (the potentiometer’s) present position into VAL.

val = map(val, 0, 1023, 0, 179); //MAP is a handy function that remaps one range of values to a different range
//Here we simply remapped VAL from the 0-1023 range of the potentiometer to the
//0-179 range accepted by this servo (it’s a 180 degree servo, so these are actual
//degrees).

minianalogservo.write(val); //writes the remapped VAL variable to the servo, which makes it turn to that position
delay(15); //waits 15 milliseconds to allow the servo to catch up.

}

the includes need pound signs before them and carets instead of parentheses to actually be compiled.

And here is similar code for repositioning the servo with the joystick instead. Only works in one axis, since I only have one servo.

include (Makeblock.h) //A bunch of libraries you'll need to call. include (Arduino.h) //At this point, I see which libraries I need by process include (SoftwareSerial.h) //of elimination after writing code. Not very elegant. include (Wire.h) include (Servo.h) //With the following code, you can swing a servo around with the joystick.

int8_t JoystickX=0; //X axis for the joystick defined and set to zero; Note integer type
int8_t YoystickY=0; //extraneous if you only have one servo, as here.
MeJoystick joystick(PORT_7); //Constructs the joystick on port 7; it’ll be called with function JOYSTICK
MePort servoport(PORT_4); //Constructs the MePort daughter card/shield on port 4.
Servo servoa; //Contstructs our servo as servoa.
int servo1pin=servoport.pin2(); //it’s plugged into the second set of pins on the Meport.
int val; //val will read the joystick’s x position, and then be used to set
//the servo position accordingly.

void setup() {
servoa.attach(servo1pin);

}

void loop() {

val=joystick.readX(); //reads the x axis postion of the joystick into VAL
val=map(val, 0, 985, 0, 169); //as in previous example, remaps VAL to the range permitted by the servo (mine craps out at 169)
servoa.write(val); //writes the (relativized) position of the joystick into the servo, thereby moving it.
delay(15); //waits for the servo to catch up.

}


#2

I meant to ask anyone who has played with the makeblock servos a bit: is there a reason that the servo seems to be working itself when it’s set to the top of its theoretical range and left there? When it’s set to anywhere else on the dial and left alone, it’s pretty much inert. But when the max range is set over about 169, as in this sketch, and you dial it all the way to the end, the servo sits there working and pushing (though not moving). Is the range actually less than 180 degrees?


#3

Here’s a version for two servos, which rotate independently depending on the joystick axis you move. I’ve also dumped the joystick polling and servo positioning into a pair of functions.

Clearly once your code starts doing more complex things with a robot, you’ll want to start moving behavior out of the main loop into functions.

The next step to shrink and neaten this code will be to eliminate one of the two polling/positioning functions and reusing it sequentially for the x and then the y axis…

int8_t JoystickX=0; //X axis for the joystick defined and set to zero; Note integer type
int8_t YoystickY=0; //extraneous if you only have one servo, as here.
MeJoystick joystick(PORT_7); //Constructs the joystick on port 7; it’ll be called with function JOYSTICK
MePort servoport(PORT_4); //Constructs the MePort daughter card/shield on port 4.

Servo servoa; //Contstructs our 1st servo as servoa.
int servo1pin=servoport.pin1(); //it’s plugged into the first set of pins on the Meport.

Servo servob; //Contstructs our 2nd servo as servob.
int servo2pin=servoport.pin2(); //it’s plugged into the second set of pins on the Meport.

int valx; //val will read the joystick’s x or y position, and then be used to set
int valy; //the servo position accordingly.

void setup() {
servoa.attach(servo1pin); //attaches the servos the the appropriate pins on the meport
servob.attach(servo2pin);

}

void loop() {

PollX(); //calls the function that polls joystick x axis and writes it to the x servo
PollY(); //calls the function that polls joystick y axis and writes it to the y servo

delay(2); //waits for the servo to catch up.

}

void PollX() {

valx=joystick.readX(); //reads the x axis postion of the joystick into VAL
valx=map(valx, 0, 985, 0, 169); //as in previous example, remaps VAL to the range permitted by the servo (mine craps out at 169)
servoa.write(valx); //writes the (relativized) position of the joystick into the servo, thereby moving it.

}

void PollY() {

valy=joystick.readY(); //reads the x axis postion of the joystick into VAL
valy=map(valy, 0, 985, 0, 169); //as in previous example, remaps VAL to the range permitted by the servo (mine craps out at 169)
servob.write(valy); //writes the (relativized) position of the joystick into the servo, thereby moving it.

}


#4