Joysticks are analogue devices that change their internal resistance depending on the position of the stick, and we can use the Analog Inputs of the Breadboard block to pick up the variations in voltage and accurately calculate the position of the stick. In this guide you will learn how to capture and process the readings from a joystick to easily trigger actions in other blocks.
- First, connect your Breadboard to your WiFi and Power Station Blocks:
- Add the Breadboard block to the Canvas and from its Properties Panel click on ‘Visual Map’. Since we need to read 2 analogue pings (one for each axis) we will use the pins at the back of the block, so go ahead and uncheck the ‘Front View’ checkbox from the Pin Configuration Assistant:
- Configure the following pins, making sure that the ‘Trigger Mode’ for the AnaloagIn pins is selected as ‘Always’. This instructs the breadboard to constantly send information to your computer, regardless of the value of the analogue pin:
IO5 - AnalogIn
IO6 - AnalogIn
IO7 - Vcc
IO8 - Gnd
- Connect the pins to the breadboard as shown in the picture below:
Checking the labels next to the Joystick's pins, make sure your connections match the configuration of the Breadboard block:
Joystick
Breadboard
GND
Gnd
+5V
Vcc
VRx
IO6 - AnalogIn
VRy
IO5 - AnalogIn
- If you want to visualize the readings coming from the Joystick, you can switch to the Dashboard and use a Data Graph Widget to graph the values in real time:
- You can also visualize these values from within the Logic Maker; go back to the ‘Workspace’ and go inside the Breadboard’s Logic Maker. Add the following gates:
INPUT (x2) -> DISPLAY Gate (x2)
Once again, move the Joystick to see the values change in real time. You will notice that the values go from 0 to near 65535, which is the maximum value Analog inputs can produce.
You can then use this information to filter those events and trigger actions. For instance, we can detect when the Joystick moves to the left or right and trigger a sound:
Driving A Servo Motor
A more realistic (and interesting) use of a Joystick is to control other physical devices. We will now use it to set the angle of rotation of a Servo Motor. Make sure you have read the article on Servos Motors before continuing.
- Snap a Motor Driver to your stack of blocks and add it to the canvas (you can delete the sound player from the previous exercise):
- Connect the MotorDriver as a ‘child’ of the Breadboard and go inside the Breadboard’s Logic Maker:
- Add a MAP Gate and connect it to one of the INPUT Gates:
- The MAP Gate is designed to change (or map) the input values to a different range, and we will use it to map the values produced by the Joystick (0 to 65535) to the 0 to 180 degrees that the Servo motor is expecting. So, fill in the MAP Gate with the values shown below and also add an ACTION Gate:
- From the ACTION Gate, select the ‘RotateToAngle’ action, making sure it is the one for the output of the Motor Driver you configured as ‘Servo’. In other words, if your servo is connected to output 2, the action you need to select is ‘M2:RotateToAngle’.
You can alternatively attach another DISPLAY Gate to the output port of the MAP Gate to see what values it produces.
Finally, connect the ACTION Gate to the MAP Gate and click on ‘Apply’:
If everything was connected properly, you should now see the Servo motor rotating as you move the Joystick, and the values in the DISPLAY Gate attached to the MAP Gate change between 0 and 180.
Keep in mind that the Joystick is a versatile instrument that can be used in a variety of applications, so you are not limited to using just a servo motor, you could, for instance, control the direction of rotation and speed of a brushed motor. It is up to you and your imagination how to use it!
Did you like the article? Have any question? Please rate and comment below!