18 April 2021

Game Controller


 My controller is a plug and play for controlling Frogger on PC. The controller features the Arduino Uno board encased in little carboard box with USB cord plugged into the PC. The intent of the design is to bring back the times where there was only controllers with plug-ins to play your game and use the buttons that were available on the controller itself. 

Frogger has four basic movements with using three controls; up, down, left and right. During gameplay, the objective is to get 5 frogs on each specific platform in order to move onto the next level within the game and allowing you five lives in order to achieve it. However, once time ran out or you moved slowed to either drown or get squished, you would lose a life and would have to start over again. The controls to move the frog are as follows:

The first potentiometer controls the frog going up and down. Next, the second potentiometer controls the frog going left and right. However, I had to play with the left and right movement mostly because it was doing both at the same time and not as separate movement. 

The layout of the controller is intended to be used like a min hand held game controller and is easily usable. 

When I started this assignment, I honestly didn’t know what game I would use or how the game controller would turn out to look. I wanted to keep what I had planned simple and efficient, which was not an easy task to accomplish. I had to find games that used arrow key movements, which there were a lot and decided to go with Frogger because I’ve played the game before back in high school. However, it was the writing in code for this was challenging for I was getting the Arduino board to do same functions that the arrow keys on a PC would do.

The end results were a functionally small, but efficient method to create a gaming controller. I would say I'm disappointed in myself for not doing better than I hoped for I’m not great when it comes to coding or making game controllers, but I am rather surprised I was able to get it to function at all.

My open would be how could I have done better in order to make it better?



                                                                              





Code
#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>
#include <Keyboard.h>

//Variables
const int potPin1 = A0; //Potentiometer connected to analog pin
const int potPin2 = A1; //Potentiometer connected to anaolg pin
const int ledPin1 = 9; //LED connected to digital PIN 9
const int ledPin2 = 10; //LED connected to digital PIN 10
int potVal1 = 0; //Potentiometer value #1
int potVal2 = 0; //Potentiometer value #2
int outputVal1 = 0; //Value for first output to PWM
int outputVal2 = 0; //Value for second output to PWM

//Setting up game controller before playing game
void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  //Initiate libraries and buttons
  CircuitPlayground.begin();
  Serial.begin(9600);
  Keyboard.begin();
}

//Code to run game movements
void loop()
{
  //Read first analog value:
  potVal1 = analogRead(potPin1); //Reads the value and assigns the name potVal
  outputVal1 = map(potVal1, 0, 1023, 0, 300); //Map it in range of analog output
  //Change value of analog output
  analogWrite(ledPin1, outputVal1);

  //Turning character up and down
  if (potVal1 <= 300)
  {
    Serial.println(potVal1); //Prints out value
    digitalWrite(ledPin1, HIGH); //Turns LED on
    Serial.print("sensor = ");
    Serial.print(potVal1);
    Serial.print("\t output ");
    Serial.print(outputVal1);
    Keyboard.write(38);
  }
  delay(500);
  else if (potVal1 <= 0)
  {
    Serial.println(potVal1);
    digitalWrite(ledPin1, LOW); //Turns LED off
    Serial.print("sensor = ");
    Serial.print(potVal1);
    Serial.print("\t output ");
    Serial.print(outputVal1);
    Keyboard.write(40);
  }

  //Read second analog value:
  potVal2 = analogRead(potPin2); //Reads the value and assigns the name potVal
  outputVal2 = map(potVal2, 0, 1023, 0, 700); //Map it in range of analog output
  //Change value of analog output
  analogWrite(ledPin2, outputVal2);

  //Turning character left and right
  if (potVal2 <= 700)
  {
    Serial.println(potVal2); //Prints out value
    digitalWrite(ledPin2, HIGH); //Turns LED on
    Serial.print("sensor = ");
    Serial.print(potVal2);
    Serial.print("\t output ");
    Serial.print(outputVal2);
    Keyboard.write(37);
  }
  delay(500);
  else if (potVal2 <= 0)
  {
    Serial.println(potVal2);
    digitalWrite(ledPin2, LOW); //Turns LED off
    Serial.print("sensor = ");
    Serial.print(potVal2);
    Serial.print("\t output ");
    Serial.print(outputVal2);
    Keyboard.write(39);
  }

}

                                                                        
                                                    




                                                                    



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.