Introduction: Pong With Arduino and Processing
This is using an arduino with meters to control the paddles on processing.
This will be using serial ports to be read by processing sent by arduino.
Step 1: The Setup
Materials:
- Arduino
- BreadBoard
- Jump Wires
- Potentiameter
- Processing
- Arduino IDE
Step 2: Coding Arduino IDE
The Code
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
void setup() { // start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) { ;// wait for serial port to connect. Needed for Leonardo only
}
pinMode(1, INPUT); // digital sensor is on digital pin 1
pinMode(2,INPUT);
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(1)/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(2)/4; // // // send sensor values: // Serial.write(firstSensor); // Serial.write(secondSensor);
}
firstSensor = analogRead(1)/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(2)/4;
// send sensor values:
Serial.write(firstSensor);
Serial.write(secondSensor);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
Step 3: Code for Processing
import processing.serial.*;
Serial myPort;
int[] serialInArray = new int[2];
// Where we'll put what we receive int
serialCount = 0;
// A count of how many bytes we receive
int player1, player2;
// Starting position of the ball
boolean firstContact = false;
// Whether we've heard from the
int rectSize = 50;
float ballX,
ballY;
float speedX, speedY;
int inputX, inputY;
int count;
boolean Wait = true;
int player1Score;
int player2Score;
void setup() {
size(500, 500); // Stage size
noStroke(); // No border on the next thing drawn
println(Serial.list());
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
ballX = 250;
ballY = 250;
}
void draw() {
background(0);
fill(100);
// Run Game Game();
}
void serialEvent(Serial myPort) { // read a byte from the serial port: int inByte = myPort.read();
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 > 1 ) {
player1 = serialInArray[0] * 2;
player2 = serialInArray[1] * 2;
// Send a capital A to request new sensor readings: myPort.write('A');
// Reset serialCount: serialCount = 0; }
}
}
void Game() {
Calculator();
PaddleCheck();
fill(255);
rect(25, player1, 5, rectSize);
rect(475, player2, 5, rectSize);
ellipse(ballX, ballY, 20, 20); }
void PaddleCheck() {
if (ballX>=5 && ballX<=25+10 && ballY>=player1 && ballY<=player1+rectSize) {
speedX = -speedX;
speedY = -speedY;
if (inputX == 0) {
inputX = 1;
}
else {
inputX = 0;
}
if (inputY == 0) {
inputY = 1;
} else {
inputY = 0;
}
}
if(ballX>=470 && ballX<=495 && ballY>=player2 && ballY<=player2+rectSize) {
speedX = -speedX;
speedY = -speedY;
if (inputX == 0) {
inputX = 1; }
else {
inputX = 0;
}
if (inputY == 0) {
inputY = 1; }
else {
inputY = 0;
}
}
noFill();
stroke(0, 0, 255);
rect(25-2, player1-2, 5+2, rectSize+2);
}
void Calculator() {
if (Wait == false) {
if (player1+rectSize >= height) {
player1 = height - rectSize;
}
if (player2+rectSize >= height) {
player2 = height - rectSize;
}
ballX = ballX + speedX;
ballY = ballY + speedY;
// Check for boundries
if (ballX>=width-10 || ballX<0-10) {
if(ballX>width-11) {
player1Score++;
}
else if(ballX<0+15) {
player2Score++;
}
ballX = 250;
ballY = 250;
inputX = round(random(0, 1));
inputY = round(random(0, 1));
Wait = true;
}
}
stroke(255);
line(250,0,250,500);
fill(255);
textSize(24);
text("Score: " + player1Score,134,30);
text("Score: " + player2Score,275,30);
if(player1Score>=3) {
text("Player 1 Wins", 250,250);
}
else if(player2Score>=3) {
text("Player 2 Wins", 250,250);
}
}
void mousePressed() {
if (Wait) {
Wait = false;
if (inputX==0) {
speedX = -1;
}
else {
speedX = 1;
}
if (inputY==0) {
speedY = -1;
}
else {
speedY = 1;
}
}
}