sanithu_hemage
Published © GPL3+

Simple Joystick Control With Leds

Shows in which direction the Joystick is positioned

BeginnerFull instructions provided10,466
Simple Joystick Control With Leds

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Analog joystick (Generic)
×1
LED (generic)
LED (generic)
×4
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Follow the Wiring in the diagram to set up the circuit. It has some small changes than in mine but it is the same nevertheless.

Code

Upload this code to the Arduino IDE.

Arduino
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output


#define UP_LED 8
#define RIGHT_LED 9 
#define LEFT_LED 10
#define DOWN_LED 11






void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);


  pinMode(UP_LED , OUTPUT);
  pinMode(RIGHT_LED , OUTPUT);
  pinMode(LEFT_LED , OUTPUT);
  pinMode(DOWN_LED , OUTPUT);

}

void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  
  delay(500);


  if(analogRead(X_pin) == 1023){
    digitalWrite(UP_LED , HIGH);

 } else if(analogRead(X_pin) == 0){
    digitalWrite(DOWN_LED , HIGH);
}

else if(analogRead(Y_pin) == 1023){
    digitalWrite(RIGHT_LED , HIGH);
}

else if(analogRead(Y_pin) == 0){
    digitalWrite(LEFT_LED , HIGH);

}

else if(digitalRead(SW_pin) == 0){
digitalWrite(UP_LED , HIGH);
digitalWrite(LEFT_LED , HIGH);
digitalWrite(RIGHT_LED , HIGH);
digitalWrite(DOWN_LED , HIGH);


}





else{
digitalWrite(UP_LED , LOW);
digitalWrite(LEFT_LED , LOW);
digitalWrite(RIGHT_LED , LOW);
digitalWrite(DOWN_LED , LOW);

  
  
}
  }









  

Credits

sanithu_hemage

sanithu_hemage

0 projects • 2 followers

Comments