Arduino: control LED matrix with joystick
In this practical tip you will learn how to control an LED matrix of your Arduino with a joystick.
Arduino: control LED matrix with joystick
Of course you need an Arduino, an LED matrix and an analog joystick.
- Start by connecting the VCC pin of your joystick to a 5V pin and the GND pin to a ground. The pin of the joystick marked "VRX" is connected to analog 0 and the pin "VRY" is connected to analog 1. In addition, the pin labeled "SW" is connected to pin 2.
- Now connect the VCC pin of the LED matrix to a 5V pin on the Arduino and the GND pin to the ground. The pin with the inscription "DIN" is connected to pin 12. Also connect the CS pin to pin 11 and the CLK pin to pin 10.
The code for the control
Here we have programmed a small code for you, which you are welcome to load onto your Arduino:
- int pos1 = 0;
- int pos2 = 0;
- #include "LedControl.h"
- LedControl lc = LedControl (12, 10, 11, 1);
- void setup () {
- lc.shutdown (0, false);
- lc.setIntensity (0.8);
- lc.clearDisplay (0);
- pinMode (7, INPUT_PULLUP);
- }
- void loop () {
- if (digitalRead (7)! = HIGH) {
- lc.clearDisplay (0);
- for (int i = 0; i <8; i ++) {
- for (int x = 0; x <8; x ++) {
- lc.setLed (0, i, x, true);
- }
- }
- delay (500);
- } else {
- pos1 = analogRead (A0);
- pos2 = analogRead (A1);
- char char1 = map (pos2, 1021, 0, 7, 0);
- char char2 = map (pos1, 1021, 0, 0, 7);
- lc.clearDisplay (0);
- lc.setLed (0, char1, char2, true);
- delay (150);
- }
- }
In the next practical tip, we will show you how to control a servo motor with a potentiometer.