Arduino Pong Game

Engineer

Benjamin R

Area of Interest

School

Winston Prep

Grade

Incoming Sophomore

Second Milestone

First Milestone

My first milestone for my Arduino Pong Game was to get a desired image on the LED matrix and to read potentiometer values.

The first step was to wire the LED matrix to the Arduino. With a little searching I found the correct way to wire them. 

The original documentation I was following had a link to a library that I could use to test the LEDs. 

If the wiring and the code is correct, the LEDS will display an animated image.

Now that the LED was working, the next step was to wire a working potentiometer. 

A potentiometer is a variable resistor.

By turning the knob, different amounts of voltage are received by the Arduino.

The potentiometer needs to be connected to the 5V, ground, and an analog input

Because the 5V and ground pins were already occupied by the LED, I had to use a breadboard.

A breadboard makes it possible to wire both the LED and the potentiometer to 5V and ground. 

With the potentiometer wired, I had to find a way to read the values. 

I found and modified a code to show the values of the potentiometer in the serial monitor.

With the exception of the very start, there were not any major setbacks to achieving this milestone.

I learned a lot from this part of the project.

I found out more about how potentiometers and breadboards work, in addition to how and when they are used.

My next objective is to wire the second LED matrix, upload the pong code, and to build a controller. 

Figure 1: LED wired to Arduino

chrome_2019-07-16_14-21-34

Figure 2: Working LED display

c4fb856b-fe68-4edb-9119-d41b1e0127e9

Figure 3: Project after first milestone

chrome_2019-07-16_16-23-49
LED Matrix test code

// Source: https://playground.arduino.cc/Main/LedControl/
#include “LedControl.h”

LedControl lc=LedControl(11,13,10,4); // Pins: DIN,CLK,CS, # of Display connected

unsigned long delayTime=200; // Delay between Frames

// Put values in arrays
byte invader1a[] =
{
B00011000, // First frame of invader #1
B00111100,
B01111110,
B11011011,
B11111111,
B00100100,
B01011010,
B10100101
};

byte invader1b[] =
{
B00011000, // Second frame of invader #1
B00111100,
B01111110,
B11011011,
B11111111,
B00100100,
B01011010,
B01000010
};

byte invader2a[] =
{
B00100100, // First frame of invader #2
B00100100,
B01111110,
B11011011,
B11111111,
B11111111,
B10100101,
B00100100
};

byte invader2b[] =
{
B00100100, // Second frame of invader #2
B10100101,
B11111111,
B11011011,
B11111111,
B01111110,
B00100100,
B01000010
};

byte invader3a[] =
{
B00011000, // First frame of invader #1
B00111100,
B01111110,
B11011011,
B11111111,
B00100100,
B01011010,
B10100101
};

byte invader3b[] =
{
B00011000, // Second frame of invader #1
B00111100,
B01111110,
B11011011,
B11111111,
B00100100,
B01011010,
B01000010
};

byte invader4a[] =
{
B00100100, // First frame of invader #2
B00100100,
B01111110,
B11011011,
B11111111,
B11111111,
B10100101,
B00100100
};

byte invader4b[] =
{
B00100100, // Second frame of invader #2
B10100101,
B11111111,
B11011011,
B11111111,
B01111110,
B00100100,
B01000010
};

void setup()
{
lc.shutdown(0,false); // Wake up displays
lc.shutdown(1,false);
lc.shutdown(2,false);
lc.shutdown(3,false);
lc.setIntensity(0,5); // Set intensity levels
lc.setIntensity(1,5);
lc.setIntensity(2,5);
lc.setIntensity(3,5);
lc.clearDisplay(0); // Clear Displays
lc.clearDisplay(1);
lc.clearDisplay(2);
lc.clearDisplay(3);
}

// Take values in Arrays and Display them
void sinvader1a()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(0,i,invader1a[i]);
}
}

void sinvader1b()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(0,i,invader1b[i]);
}
}

void sinvader2a()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(1,i,invader2a[i]);
}
}

void sinvader2b()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(1,i,invader2b[i]);
}
}

void sinvader3a()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(2,i,invader1a[i]);
}
}

void sinvader3b()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(2,i,invader1b[i]);
}
}

void sinvader4a()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(3,i,invader2a[i]);
}
}

void sinvader4b()
{
for (int i = 0; i < 8; i++)
{
lc.setRow(3,i,invader2b[i]);
}
}

void loop()
{
// Put #1 frame on both Display
sinvader1a();
delay(delayTime);
sinvader2a();
delay(delayTime);

// Put #2 frame on both Display
sinvader1b();
delay(delayTime);
sinvader2b();
delay(delayTime);

// Put #3 frame on both Display
sinvader3a();
delay(delayTime);
sinvader4a();
delay(delayTime);

// Put #4 frame on both Display
sinvader3b();
delay(delayTime);
sinvader4b();
delay(delayTime);

}

Code for reading potentiometer values

/*Source: https://www.arduino.cc/en/tutorial/potentiometer
/* Analog Read to LED
* ——————
*
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The amount of time the LED will be on and off depends on
* the value obtained by analogRead(). In the easiest case we connect
* a potentiometer to analog pin 2.
*
* Created 1 December 2005
* copyleft 2005 DojoDave <http://www.0j0.org>
* http://arduino.berlios.de
*
*/

int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT

Serial.begin (9600); // make sure the baud is set to 9600.
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(100); // stop the program for some time. You can change the delay but if the delay is too low it may not work.
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(100); // stop the program for some time

Serial.println(val); // displays the value of the potentiometer periodically in the serial monitor
}

Starter Project

My starter project was the Useless Machine. 

When the switch on the top is flipped, an arm pushes the flap on the top open and turns the switch off. 

The switch supplies power to the motor.

The motor is attached to the arm, which causes the arm to move.

When the arm moves, it removes pressure from the microswitch.

When the arm reaches the top switch, the motor moves in reverse, which moves the arm in the opposite direction

When the arm returns to its starting position, it presses the microswitch again, which turns the machine off.

The first step was to solder all the required parts onto the printed circuit board, or PCB. 

I was able to solder the switch and resistors with little trouble. The screw terminals ended up being slightly out of place, but my first real struggle was the LED, which changes color based on what direction the mechanism that flips the switch is moving.

I originally thought the LED went on the same side of the PCB as the other parts, but it actually did not. 

By the time I noticed it was too late, and I had to desolder. 

This process took a very long time and when it was finally done, I realized the LED would have been fine the way I originally soldered it.

After soldering a few more parts I moved on to screwing acrylic parts to the motor.

This part was very annoying because the acrylic parts moved around a lot. 

This made it very difficult to screw them to the motor. At many points I had to use helping hands and tape. 

WIth those parts attached, the next step was to attach the PCB to it. 

I was able to complete this step by using the holes in the acrylic parts to firmly screw the PCB to the motor. 

The next step was attaching the arm. 

To do this, I put the required 3 AAA batteries and put them into the battery pack.

To connect the battery pack, I put the red and black wires in the screw terminal. 

At first the wires fell out, but by soldering the tip I was able to keep them in place.

 I spun the motor until it was in the correct position and screwed the arm on.

With the first test complete, I moved onto the final (and most tedious) step, building the box.

 First, I attached the motor and PCB to the base of the box by fitting it into the slots on the base.

To keep the box slightly elevated, I added four silicon feet to the corners of the box. 

I used velcro to keep the battery pack secure and aligned it with the nub on the motor

Next, I had to screw the corner pieces on the box. 

The intended way of screwing these in was difficult, since the inside was not fully hollow, so I had to drill the screws in place.

The final step was putting the sides and top of the box together. 

This part was tedious and annoying because the sides of the box kept moving around, and for the top of the box to fit on, everything needed to be in the perfect position. 

With time and help, I was able to get it done. 

Overall, this project had many annoying aspects. 

To make this easier you need to take additional steps that the instructions don’t provide.

Screwing parts that move easily can get very frustrating, and even tape doesn’t help sometimes.

Despite this, I am still glad I chose this project because I learned that sometimes you need to think outside the box to complete a project.

This project helped me improve my soldering skills and made me more resourceful. 

FIgure 1: Screwing acrylic parts onto motor

chrome_2019-07-11_13-52-55

Figure 2: Attaching PCB to motor

chrome_2019-07-11_14-00-42

Figure 3: Screwing the arm onto the motor

chrome_2019-07-11_13-55-10

Figure 4: Screwing corner pieces into place

chrome_2019-07-11_13-58-24

Figure 5: Schematic of Useless Machine with description

flato9hi74n7lke.large_
Image Source: https://github.com/adafruit/Adafruit-MiniPOV4-Kit/tree/master/Hardware

Start typing and press Enter to search