Introduction: Pong Controller
Control Processing games with arduino.
Step 1: Build Circuit
what you will need:
arduino uno
breadboard
8 Jumper wires
2 potentiometers
connect the potentiometers to the A0 and A1 pins
Step 2: Write Code for Arduino
int firstSensor = 0; // first analog sensor
int secondSensor = 1; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
char letter = Serial.read();
void setup() { // start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(2, INPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0-255:
firstSensor = analogRead(A0)/4;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0-255:
secondSensor = analogRead(1)/4;
// read switch, map it to 0 or 255L
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
// send sensor values:
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
while (Serial.available() > 0) {
Serial.read(); //now do something with this byte
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Step 3: Write Code for Processing
import processing.serial.*;
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
boolean gameStart = false;
boolean hit = false;
float x = 500;
float y = 260;
float speedX = random(3, 5);
float speedY = random(3, 5);
int leftColor = 128;
int rightColor = 128;
int diam;
int rectSize = 50;
float diamHit;
int score1 = 0;
int score2 = 0;
int sound = 0;
void setup() {
myPort = new Serial(this, Serial.list()[0], 9600);
size(1000, 520); // Stage size
noStroke(); // No border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
String portName = Serial.list()[0];
noStroke();
smooth();
ellipseMode(CENTER);
}
void draw() {
background(0);
rect (width-20, xpos-rectSize/2, 5, rectSize);
rect (20, ypos-rectSize/2, 5, rectSize);
textSize(20);
text("Player1: "+score1,width/4,30);
text("Player2: "+score2,width/1.5,30);
fill(255);
diam = 10;
ellipse(x, y, diam, diam);
if((x > width-21 && x < width -15 && y < xpos+rectSize/2 && y > xpos-rectSize/2)
||((x < 21 && x > 15 && y > ypos-rectSize/2 && y < ypos+rectSize/2) ) )
{
hit = true;
}
else {
hit = false;
}
if (gameStart) {
x = x + speedX;
y = y + speedY;
// if ball hits movable bar, invert X direction and apply effects
if((x > width-21 && x < width -15 && y < xpos+rectSize/2 && y > xpos-rectSize/2)
||((x < 21 && x > 15 && y > ypos-rectSize/2 && y < ypos+rectSize/2) ) )
{
speedX = speedX * -1;
x = x + speedX;
rightColor = 0;
fill(random(0,128),random(0,128),random(0,128));
diamHit = random(75,150);
ellipse(x,y,diamHit,diamHit);
}
// resets things if you lose
if (x > width) {
gameStart = false;
score1++;
x = width/2;
y = height/2;
speedX = random(3, 5);
speedY = random(3, 5);
rectSize = 50;
}
if (x <0) {
gameStart = false;
score2++;
x = width/2;
y = height/2;
speedX = random(3, 5);
speedY = random(3, 5);
rectSize = 50;
}
// if ball hits up or down, change direction of Y
if ( y > height || y < 0 ) {
speedY = speedY * -1;
y = y + speedY;
}
if (score1==3 || score2==3){
score1=0;
score2=0;
}
}
}
void mousePressed() { gameStart = !gameStart;
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0]*2;
ypos = serialInArray[1]*2;
println(xpos + "t" + ypos + "t" + hit);
// Send a capital A to request new sensor readings:
if (hit = true){
myPort.write('B');
}
else{
myPort.clear();
myPort.write('A');
}
// Reset serialCount:
serialCount = 0;
}
}
}