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.

  • 3,671 views
  • 5 comments
  • 13 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

  • 1,595 views
  • 0 comments
  • 8 respects

Arduino Game By LCD

Project tutorial by Mohammed Magdy

  • 32,859 views
  • 31 comments
  • 108 respects

Arduino Pocket Game Console + A-Maze - Maze Game

Project tutorial by Alojz Jakob

  • 5,315 views
  • 7 comments
  • 18 respects

Ninja Dollar - LCD game

Project tutorial by anime

  • 2,353 views
  • 2 comments
  • 4 respects

Breadboard to PCB Part 1 - Making the Simon Says Game

Project tutorial by Katie Kristoff and Monica Houston

  • 5,456 views
  • 10 comments
  • 24 respects

Memory game

Project showcase by Anton Nazarenko

  • 2,425 views
  • 1 comment
  • 8 respects
Add projectSign up / Login