Rayan kiwan
Created October 11, 2022 © CERN-OHL

Simple Joystick Control With Leds

Shows in which direction the Joystick is positioned

BeginnerFull instructions provided15
Simple Joystick Control With Leds

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Analog joystick (Generic)
×1
5 mm LED: Red
5 mm LED: Red
×4
Resistor 330 ohm
Resistor 330 ohm
×4
Jumper wires (generic)
Jumper wires (generic)
×10
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

joystick control led

Code

joystick control led

Arduino
int led1 = 5;
int led2 = 6;
int led3 = 9;
int led4 = 10;

// define joystick pins
int joystick_x = A0;
int joystick_y = A1;

//read values from the analog pin
int joystick_xvalue = 0;
int joystick_yvalue = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(joystick_x, INPUT);
  pinMode(joystick_y, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:

  joystick_xvalue = 0;
  joystick_yvalue = 0;
  
  //read values from joystick_x
  joystick_xvalue = analogRead(joystick_x);
  joystick_xvalue = map (joystick_xvalue, 0, 1023, -90, 90);
  
  //read values from joystick_y
  joystick_yvalue = analogRead(joystick_y);
  joystick_yvalue = map (joystick_yvalue, 0, 1023, -90, 90);

  if(joystick_xvalue < -5){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  else if(joystick_xvalue > 5){
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  else if(joystick_yvalue < -5){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);
  }
  else if(joystick_yvalue > 5){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }
  else{
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  
  delay(60);
}

Credits

Rayan kiwan
36 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.