This small circuit uses a two resistor joystick to control the 'cursor' on a 8x8 LED matrix. Moving the joystick moves the cursor around, pressing down on the joystick moves the cursor back to the middle.
Parts list:
Arduino UNO board
HW-504 Joystick
8x8 LED Matrix with a driver
10 male to female jumper wires
2 male to male jumper wires
Its usually a good idea to power the 8x8 LED matrix using an external power source. However since it's only a single LED on at one time, I just used the arduino directly.
The circuit is finicky, and you may need to restart the UNO board or unplug the board and let it is uncharge for a while.
#include <LedControl.h>
// Define the pins for the joystick connections
const int xAxisPin = A0; // Analog input pin for X-axis
const int yAxisPin = A1; // Analog input pin for Y-axis
const int buttonPin = 7; // Digital input pin for pushbutton
// the position of the joystick is read in a number between ~0 and ~800
int xPosition = 400; // x position variable
int yPosition = 400; // y position variable
int buttonState = 1; // push button .... default is '1' when pushed, it goes to '0'
// Define the pins for the LED matrix controller
const int DIN = 8; //
const int CS = 9; //
const int CLK = 10; //
int xLED = 3; // variable to hold the LED position of the 'cursor' across x
int yLED = 3; // variable to hold the LED position of the 'cursor' across y
LedControl lc = LedControl(DIN,CLK,CS,1);
void setup() {
// Initialize the button pin as an input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Begin serial communication at a baud rate of 9600
//Serial.begin(9600);
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0,0); // Set the brightness to maximum value
lc.clearDisplay(0); // and clear the display
}
void loop() {
// Read the joystick position values
xPosition = analogRead(xAxisPin);
yPosition = analogRead(yAxisPin);
// Read the button state (LOW when pressed due to pull-up resistor)
buttonState = digitalRead(buttonPin);
//turn off previously lit LED
lc.setLed(0, xLED, yLED, false);
if(buttonState==0) {
xLED=3; yLED=3;
for ( int i = 0; i < 8; i++) {
lc.setRow(0,i,0) ;
delay(10);
}
delay(200);
}
if(xPosition<300 && xLED>0){ xLED = xLED -1 ;}
if(xPosition>500 && xLED<7){ xLED = xLED +1 ;}
if(yPosition<300 && yLED>0){ yLED = yLED -1 ;}
if(yPosition>500 && yLED<7){ yLED = yLED +1 ;}
// Print the joystick position values to the serial monitor
/*Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.print(buttonState);
Serial.print(" | xLED: ");
Serial.print(xLED);
Serial.print(" | yLED: ");
Serial.print(yLED);
Serial.println();*/
lc.setLed(0, xLED, yLED, true);
delay(150);
}