Note: this page refers to a product that is retired.

EsploraPong

Play the classic Pong game with the Esplora using Processing.

Esplora Pong

Want to play Pong on your computer with your Esplora? This example shows you how.

This example is designed to be used with a Processing sketch. Processing is an open source programming environment. The Esplora reads the slider and buttons and sends data to the Processing sketch serially over USB, and Processing uses the incoming data to move paddles in a game of Pong.

If you don't have Processing installed on your computer, download it from the Processing site and follow the installation instructions.

Download the zip file containing the Processing sketch for this example.

You only need to unzip this folder inside your Processing sketches folder, and open the .pde file in Processing.

Hardware Required

  • Arduino Esplora

Software Required

Circuit

Only your Arduino Esplora is needed for this example.

Esplora board using slider and pushbuttons
Esplora board using slider and pushbuttons

Code

This example sends the states of the slider and three pushbuttons serially to the computer. The associated Processing sketch reads the serial data to control the paddles and the state of the game.

The data is always sent in the same order: slider, button 1, button 3, button 4. The data are separated by commas. The Serial.println() command that sends the final button also sends a newline character. The Processing sketch looks for the newline to know when it's got all the sensor data.

NOTE: Serial ports on a computer can only be accessed by one program at a time. If you have the Arduino IDE's Serial Monitor open, then the Processing Sketch will not be able to access the Esplora's serial port. Likewise, if the Processing sketch is still running, you won't be able to open the Serial Monitor or reprogram your Esplora.

1/*
2
3 Esplora Pong
4
5 This sketch connects serially to a Processing sketch to control a Pong game.
6
7 It sends the position of the slider and the states of three pushbuttons to the
8
9 Processing sketch serially, separated by commas. The Processing sketch uses that
10
11 data to control the graphics in the sketch.
12
13 The slider sets a paddle's height
14
15 Switch 1 is resets the game
16
17 Switch 2 resets the ball to the center
18
19 Switch 3 reverses the players
20
21 You can play this game with one or two Esploras.
22
23 Created on 22 Dec 2012
24
25 by Tom Igoe
26
27 This example is in the public domain.
28
29 */
30
31#include <Esplora.h>
32
33void setup() {
34
35 Serial.begin(9600); // initialize serial communication
36}
37
38void loop() {
39
40 // read the slider and three of the buttons
41
42 int slider = Esplora.readSlider();
43
44 int resetButton = Esplora.readButton(SWITCH_1);
45
46 int serveButton = Esplora.readButton(SWITCH_3);
47
48 int switchPlayerButton = Esplora.readButton(SWITCH_4);
49
50 Serial.print(slider); // print the slider value
51
52 Serial.print(","); // add a comma
53
54 Serial.print(resetButton); // print the reset button value
55
56 Serial.print(","); // add another comma
57
58 Serial.print(serveButton); // print the serve button value
59
60 Serial.print(","); // add another comma
61
62 Serial.println(switchPlayerButton); // print the last button with a newline
63
64 delay(10); // delay before sending the next set
65}

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.