tylerpeppy
Published © GPL3+

Joystick Game

A small tutorial project that tells you what direction your joystick is pointing in when you push it.

IntermediateFull instructions provided14,891
Joystick Game

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×1
Arduino UNO
Arduino UNO
×1
USB-A to B Cable
USB-A to B Cable
×1
Analog joystick (Generic)
×1

Story

Read more

Schematics

The schematic

This will help you to connect all your components together as the wiring in my image was a mess.

Code

The code

Arduino
This is the code that you will paste into your editor in order to make this project work.
#define joyX A0
#define joyY A1

int button=2;
int buttonState = 0;
int buttonState1 = 0;

void setup() {
  pinMode(7,OUTPUT);
  pinMode(button,INPUT);
  digitalWrite(button, HIGH);
  Serial.begin(9600);

  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
}
 
void loop() {

 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
  Serial.print(xValue);
  Serial.print("\t");
  Serial.println(yValue);
  
  buttonState = digitalRead(button);
  Serial.println(buttonState);
  if (xValue>=0 && yValue<=10)
  {
    digitalWrite(10, HIGH);
  }
  else{digitalWrite(10, LOW);}

  if (xValue<=10 && yValue>=500)
  {
    digitalWrite(11, HIGH);
  }
  else{digitalWrite(11, LOW);}

  if (xValue>=1020 && yValue>=500)
  {
    digitalWrite(9, HIGH);
  }
  else{digitalWrite(9, LOW);}

  if (xValue>=500 && yValue>=1020)
  {
    digitalWrite(8, HIGH);
  }
  else{digitalWrite(8, LOW);}

  if (xValue>=1020 && yValue>=1020)
  {
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
  }

  if (buttonState == LOW)
  {
    Serial.println("Switch = High");
    digitalWrite(7, HIGH);
  }
  else{digitalWrite(7, LOW);}
  buttonState1 = digitalRead(7);
  Serial.println(buttonState1);
  delay(100);
}

Credits

tylerpeppy

tylerpeppy

2 projects • 42 followers
Love Arduino and IoT projects!

Comments