Peter Varga
Published

The Joystick Game

A simple led based game.

IntermediateFull instructions provided5
The Joystick Game

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Analog joystick (Generic)
×1
LED (generic)
LED (generic)
×4
Resistor 330 ohm
Resistor 330 ohm
×4
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

The diagram

Code

Code

C/C++
#define joyX A0
#define joyY A1
long randNumber;
int button=2;
int buttonState = 0;
int buttonState1 = 0;
#define led1  1;
#define led2  2;
#define led3  3;
#define led4  4;
#define led5  5;
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() {

 // Makes a random number from 0 to 3
 randNumber = random(4);
  // If the random number is 0 say up and make the light turn on
 if (randNumber == 0){
   Serial.println("Up"); 
   Serial.println("  ");
   digitalWrite(10, HIGH);
   delay(1500);
 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
 if (xValue>=500 && yValue<=10) // If the player pushes the joystick up say good job
  {
    digitalWrite(10, LOW);
    Serial.println("Good Job");
    Serial.println("  ");
    digitalWrite(7, HIGH);
    delay(450);
    digitalWrite(7, LOW);    
  }
  else {
    // If the random numbe is not 0 turn the light off
  digitalWrite(10, LOW);
  }
 }
  else {
        
   if (randNumber == 1){
   Serial.println("Left"); 
   Serial.println("  ");
   digitalWrite(11, HIGH);
   delay(1500);
 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
 if (xValue<=10 && yValue>=500)
  {
    digitalWrite(11, LOW);
    Serial.println("Good Job");
    Serial.println("  ");
    digitalWrite(7, HIGH);
    delay(450);
    digitalWrite(7, LOW);
  }
  else {
  digitalWrite(11, LOW);
  }
 }
  else {
    
   if (randNumber == 2){
   Serial.println("Down"); 
   Serial.println("  ");
   digitalWrite(8, HIGH);
   delay(1500);
 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
 if (xValue>=500 && yValue>=1020)
  {  
    digitalWrite(8, LOW);
    Serial.println("Good Job");
    Serial.println("  ");
    digitalWrite(7, HIGH);
    delay(450);
    digitalWrite(7, LOW);
  } 
  else {
  digitalWrite(8, LOW);
  }
 }  
   else {
    
    if (randNumber == 3)  {
    Serial.println("Right"); 
    Serial.println("  ");
    digitalWrite(9, HIGH);
    delay(1500);
 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
 if (xValue>=1020 && yValue>=500)
  {
    digitalWrite(9, LOW);
    Serial.println("Good Job");
    Serial.println("  ");
    digitalWrite(7, HIGH);
    delay(450);
    digitalWrite(7, LOW);
  }    
  else {
  digitalWrite(9, LOW);
      }
     }
    }
   }
  } 
 }

Credits

Peter Varga

Peter Varga

0 projects • 0 followers

Comments