This lesson will introduce if/else statements, mechanical structures, servo motors, and potentiometers.
If/else statements are more refined versions of if statements, which allow the user to choose what happens if the test condition is true and what happens if the condition is false. This allows for more streamlined decision making, which is important considering how vital decision making is for program.
When using them, however, they’re nearly the same as normal if statements. The only difference is that there’s now an else slot in addition to the “test” and “then” slots. There, you’ll put what you want the program to do whenever the test condition is false.
Project 1: The Servo Motor
A servo is a rotary motor that can accurately control the angles at which it positions itself. The servos we will use can turn from 0 to 180 degrees.
- Connect the Servo to pin O0 using a wire. Remember, a servo is getting information from the board, so it’s an output.
- Make sure your code has a loop block. Then, go into the red CAROBOT SwissCHEESE bin, find a Servo block, and put it into the loop. Make sure the servo pin is set to O0.
- Change the servo angle to 1.
- From the control bin, put a 1000 ms delay after the Servo block.
- Add another servo and delay underneath the first ones. However, this time, set the servo angle to 180.
- Plug in your board and upload the program. Your servo should be turning back and forth.
- Try making the servo move from 45 degrees to 120 degrees.
Project 2: Controlling a Servo using a Potentiometer
A potentiometer is an input device that you can slide or rotate. It’s seen in volume dials, light dimmers, radio knobs, and anything that controls an analog variable (analog meaning that it doesn’t only have “on” and “off” state). Every position on the dial corresponds to a different value, ranging from 0 to 1024.
Since the servo’s position is analog, we can use the potentiometer to control it. However, the servo has an angle range of 0-180, while the potentiometer’s value range is from 0-1024. To solve this problem, we’ll use a map function that will line up the two value ranges, allowing the servo and potentiometer to match each other. For example, 1024 on the potentiometer will become 180 on the servo, 512 on the potentiometer will become 90, etc.
- Connect a wire from IO to the potentiometer.
- Put a servo block into your loop, but remove the angle value.
- Go into the green Math Operators bin and get a map block. Put the map block in the servo angle slot, where the angle value used to be. Make sure the values in the “from” section are from 0 to 1023, and the values in the “to” section are from 0 to 180.
- Go into the CAROBOT SwissCHEESE bin and get a potentiometer block. Drag it into the value slot of the map block.
- Add a short delay (50 ms) to the end of the loop. This is just to make sure we don’t accidentally overload the board with signals.
- Plug your board in and hit upload to Arduino. You servo should now turn whenever you turn your potentiometer!
We will be using mechanical design materials for the remainder of this course, such as metal brackets, nuts, and bolts. To connect pieces, put bolts in their holes and use nuts to tighten the screws. Remember that if you want to secure two pieces together, make sure they’re connected at two points!
Again, if you don’t have any nuts or bolts, just follow along!
Combine all of what you’ve learned today and build a mechanical goalkeeper to be mounted on a goal frame. You should be able to control your goalkeeper using the potentiometer so you can use it to block incoming shots and score points.
That’s all for this tutorial! Look forward to Class 3!