Introduction: Arduino Vertical Pong

This is a program to be run with arduino and processing on a computer, it is two-player, and controlled by the potentiometers. The piezo will play sounds, the button will perform various actions in the game, and the LEDs show the score.

You will need:
-an arduino uno
-2 potentiometers
-18 wires
-6 LEDs (3 red, 3 yellow)
-1 button
-1 piezo speaker
-wires to connect the potentiometers
( I used alligator clip wires along with 6 normal wires to connect the potentiometers )
-6 330 ohm resistors
-1 10k ohm resistor.

Step 1: Assembly

Step 2: Arduino Code

float p1;
float p2;
float del;
float button;
char import;
boolean playing;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(A2, INPUT);
}

void loop() {
p1 = analogRead(A5);
p2 = analogRead(A0);
if ( analogRead(A2) >= 900) {
button = 1;
} else {
button = 0;
}
String sent = String(p1) + " " + String(p2) + " " + String(button);
Serial.println(sent);
if (Serial.available()) {
import = Serial.read();
}
if (import == 1) {
digitalWrite(2, HIGH);
import = 0;
topScore();
}
if (import == 2) {
digitalWrite(3, HIGH);
import = 0;
topScore();
}
if (import == 3) {
digitalWrite(4, HIGH);
import = 0;
topWin();
}
if (import == 4) {
digitalWrite(6, HIGH);
import = 0;
bottomScore();
}
if (import == 5) {
digitalWrite(7, HIGH);
import = 0;
bottomScore();
}
if (import == 6) {
digitalWrite(8, HIGH);
import = 0;
bottomWin();
}
if (import == 7) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
import = 0;
}
if (import == 8) {
tone(11, 642, 50);
delay(50);
tone(11, 863, 50);
import = 0;
}
}
void topScore() {
int p[] = {349, 523, 699};
int l[] = {75, 75, 75};
for (int i = 0; i < 3; i++) {
tone(11, p[i], l[i]);
delay(l[i]);
}
}
void topWin() {
int p[] = {349, 349, 523};
int l[] = {300, 200, 500};
for (int i = 0; i < 3; i++) {
tone(11, p[i], l[i]);
delay(l[i]);
}
}
void bottomScore() {
int p[] = {699, 523, 349};
int l[] = {75, 75, 75};
for (int i = 0; i < 3; i++) {
tone(11, p[i], l[i]);
delay(l[i]);
}
}
void bottomWin() {
int p[] = {523, 523, 349};
int l[] = {300, 200, 400};
for (int i = 0; i < 3; i++) {
tone(11, p[i], l[i]);
delay(l[i]);
}
}

Step 3: Processing Code

import processing.serial.*;

Serial port = new Serial(this, Serial.list()[0], 9600); String value; boolean gameover= false, right = false, left = false, d = false, a = false, last = false, playing = false; int topscore=0, bottomscore=0, button = 0; float changespeed=0; Paddle bottom; Ball pongball; Paddle top; void setup() { frameRate(100); noStroke(); pongball= new Ball(); bottom=new Paddle(); top=new Paddle(); top.y=0;size(1024,500); value=port.readStringUntil('\n'); } void draw() { String[] imports; background(0); if (port.available()>0) { value=port.readStringUntil('\n'); } if (value!=null) { imports = split(value, " "); int i = 0; while (i 0) { bottomscore = 0; topscore = 0; port.write(7); gameover = false; playing = false; } } if (button > 0) { gameover = false; playing = false; } } } top.show(); bottom.show(); if (!gameover) { pongball.move(); } else { if (!playing) { sendthing(); } if (last) { pongball.x = top.x+60; pongball.y = top.y+13; pongball.up = false; pongball.right = false; } else { pongball.x = bottom.x+60; pongball.y = bottom.y-13; pongball.up = true; pongball.right = true; } } pongball.bounce(); pongball.show(); if (pongball.y<-8) { gameover=true; bottomscore++; last = true; } if (pongball.y>508) { last = false; gameover=true; topscore++; } }

void sendthing() { if (topscore == 1 && !last) { port.write(1); } if (topscore == 2 && !last) { port.write(2); } if (topscore == 3 && !last) { port.write(3); } if (bottomscore == 1 && last) { port.write(4); } if (bottomscore == 2 && last) { port.write(5); } if (bottomscore == 3 && last) { port.write(6); }

playing = true; }

class Ball { int x, y; boolean up, right; Ball() { x=16; y=484; up=true; right=true; } void move() { if (up==true) { y=int(y-2-changespeed/2); } else //up==false { y=int(y+2+changespeed/2); } if (right==true) { x=int(x+5+changespeed); } else //right==false { x=int(x-5-changespeed); } } void bounce() { if (get(int(x)-8, int(y))!=color(0)) { right=true; if (y>9&&y<491) { port.write(8); } } if (get(int(x)+8, int(y))!=color(0)) { right=false; if (y>9&&y<491) { port.write(8); } } if (get(int(x), int(y)-8)==color(255, 0, 0)) { up=false; if (y>9&&y<491) { port.write(8); } } if (get(int(x), int(y)+8)==color(255, 0, 0)) { up=true; if (y>9&&y<491) { port.write(8); } } } void show() { fill(247, 226, 48); ellipse(x, y, 16, 16); } }

class Paddle { int x, y; Paddle() { x=250; y=496; } void show() { fill(255, 0, 0); rect(x, y, 120, 4); } }