top of page

Game Controller

Week 3

Tried using the Gyroscope from the Arduino to create a game controller. I started by asking a few people on the floor how they would rotate the breadboard if they wanted to control the paddle on the screen for all for directions

I started by setting up the wifi connections

Screenshot 2024-09-26 at 5.03.56 AM.png

I completed the circuit for the game controller

Screenshot 2024-09-26 at 5.17.18 AM.png

I added the code for controlling the paddle with the gyroscope and tested it

Screenshot 2024-09-26 at 5.18.42 AM.png

When I tested the controller, the up and down directions seemed to move at the right speed to control. But the left and right were too fast. I tried looking at the source code and realized that the left and right had larger steps than up and down directions for the paddle

I tried a few ways to slow down the left and right movement for better control but unfortunately, I couldn't find a way

Arduino Code:

/*

Joystick client

Language: Arduino

This program enables an Arduino to control one paddle

in a networked Pong game via a TCP socket. Re-written to work with

either WiFi101 library (MKR1000)

or WiFiNINA library (Nano 33 IoT, MKR1010)

note: WiFi SSID and password are stored in arduino_secrets.h file.

Make updates to that file with your network credentials.

created 20 Jun 2012

modified 8 Aug 2024

by Tom Igoe

*/

 

#include <Arduino_LSM6DS3.h>

float xSmoothed;

float ySmoothed;

float xPrev;

float yPrev;



 

#include <SPI.h>

#include <WiFiNINA.h> // use this for Nano 33 IoT or MKR1010 boards

#include "arduino_secrets.h"

 

// Initialize the Wifi client library

WiFiClient client;

 

const char server[] = "10.18.244.32";

const char yourName[] = "myname";

const int connectButton = 2; // the pushbutton for connecting/disconnecting

const int connectionLED = 3; // this LED indicates whether you're connected

const int leftLED = 4; // this LED indicates that you're moving left

const int rightLED = 5; // this LED indicates that you're moving right

const int upLED = 6; // this LED indicates that you're moving uo

const int downLED = 7; // this LED indicates that you're moving down

 

const int sendInterval = 30; // minimum time between messages to the server

const int debounceInterval = 5; // used to smooth out pushbutton readings

 

int lastButtonState = HIGH; // previous state of the pushbutton

long lastTimeSent = 0; // timestamp of the last server message

 

int lastConnectState = false;

 

void setup() {

//Initialize serial

Serial.begin(9600);

while (!Serial)

;

if (!IMU.begin()) {

Serial.println("Failed to initialize IMU!");

while (1)

;

}



 

// if serial monitor's not open, wait 3 seconds:

if (!Serial) delay(3000);

 

// attempt to connect to Wifi network:

while (WiFi.status() != WL_CONNECTED) {

Serial.print("Attempting to connect to SSID: ");

Serial.println(SECRET_SSID);

// Connect to WPA/WPA2 network.

WiFi.begin(SECRET_SSID, SECRET_PASS);

 

// wait 3 seconds for connection:

delay(3000);

}

 

// you're connected now, so print out the status:

printWifiStatus();

// initialize digital inputs and outputs:

pinMode(connectButton, INPUT_PULLUP);

pinMode(connectionLED, OUTPUT);

pinMode(leftLED, OUTPUT);

pinMode(rightLED, OUTPUT);

pinMode(upLED, OUTPUT);

pinMode(downLED, OUTPUT);

}

 

void loop() {

 

float x, y, z;

 

if (IMU.accelerationAvailable()) {

IMU.readAcceleration(x, y, z);

 

xSmoothed = (x * 0.03) + (xPrev * 0.97);

xPrev = xSmoothed;

 

ySmoothed = (y * 0.03) + (yPrev * 0.97);

yPrev = ySmoothed;

}

 

// check to see if the pushbutton's pressed:

int buttonState = digitalRead(connectButton);

 

// if the button changes state:

if (buttonState != lastButtonState) {

// delay for the debounce interval:

delay(debounceInterval);

if (buttonState == LOW) {

// if the client's not connected, connect:

if (!client.connected()) {

Serial.println("connecting");

client.connect(server, 8080);

} else { // else disconnect:

Serial.println("disconnecting");

client.print("x");

client.stop();

}

}

// save current button state for comparison next time:

lastButtonState = buttonState;

}

 

// if the client's connected, and the send interval has elapased:

if (client.connected() && (millis() - lastTimeSent > sendInterval)) {

// read the joystick and send messages as appropriate:

int xSensor;

int ySensor;

 

xSensor = (xSmoothed * 10) + 10;

ySensor = (ySmoothed * 10) + 10;

 

xSensor = map(xSensor, 0, 20, 0, 3) - 1;

ySensor = map(ySensor, 0, 20, 0, 3) - 1;

 

switch (ySensor) {

case -1: //left

client.print("a");

Serial.println("a");

break;

case 0: // center

 

break;

case 1: // right

client.print("d");

Serial.println("d");

break;

}


 

switch (xSensor) {

case -1: //down

client.print("s");

Serial.print("s");

break;

case 0: // center

 

break;

case 1: // up

client.print("w");

Serial.print("w");

break;

}


 

//save this moment as last time you sent a message:

lastTimeSent = millis();

}

 

// set the connection LED based on the connection state:

digitalWrite(connectionLED, client.connected());

 

// if there's incoming data from the client, print it serially:

if (client.available()) {

char c = client.read();

Serial.write(c);

}

 

// if connection state has changed:

if (lastConnectState != client.connected()) {

if (lastConnectState == false) {

// send your name, and change to name display ("i");

client.print("=");

client.println(yourName);

}

lastConnectState = client.connected();

}

}

 

void printWifiStatus() {

// print the SSID of the network you're attached to:

Serial.print("SSID: ");

Serial.println(WiFi.SSID());

 

// print your WiFi shield's IP address:

IPAddress ip = WiFi.localIP();

Serial.print("IP Address: ");

Serial.println(ip);

 

// print the received signal strength:

long rssi = WiFi.RSSI();

Serial.print("signal strength (RSSI):");

Serial.print(rssi);

Serial.println(" dBm");

}

bottom of page