Project tutorial
Joystick Game

Joystick Game © GPL3+

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

  • 7,258 views
  • 5 comments
  • 16 respects

Components and supplies

About this project

This is a project that uses a generic joystick and using the Arduino reads the +y and -y values and the +x and -x values and using a series of if and else statements will tell the 5 leds which led to turn on for ex: If you rock the joystick to the left your left led will light up. Another feature is that if your joystick has a button (most will) it will light up your button for your press down feature.

Code

The codeArduino
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);
}

Schematics

The schematic
This will help you to connect all your components together as the wiring in my image was a mess.
Untitled sketch bb2 srdnozeu62

Comments

Similar projects you might like

Arduino LCD Video Game

Project tutorial by xXarduino_11Xx

  • 5,090 views
  • 2 comments
  • 20 respects

Arduino Game By LCD

Project tutorial by Mohammed Magdy

  • 61,831 views
  • 57 comments
  • 194 respects

Make an Arduino Memory Game

Project tutorial by Jeremie

  • 21,602 views
  • 39 comments
  • 46 respects

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 11,035 views
  • 8 comments
  • 28 respects

Ninja Dollar - LCD game

Project tutorial by anime

  • 5,622 views
  • 8 comments
  • 9 respects

Breadboard to PCB Part 1 - Making the Simon Says Game

Project tutorial by Katie Kristoff and Monica Houston

  • 8,789 views
  • 11 comments
  • 31 respects
Add projectSign up / Login