Analog Pong

by littleBits May 9, 2014
8

Play Pong on your computer screen using two analog sensor modules as inputs.

*To complete this exercise, you will need to download the Arduino softwareYou will also need to download Processing to run the Processing sketch.

This sketch will allow you to send two analog sensor values from the Arduino module to a Processing sketch that is running a version of the game Pong. The Pong paddle positions move based on the sensor values in this two player game.

For more “getting started” sketches, click here.

For support check out the littleBits and Arduino forums.

To learn more about coding, find examples on Arduino’s website.


Bits used: Arduino, dimmer, fork, power

Tags: Programming Fun & Games

Videos and Recipes

Instructions

  1. 1

    Install the software

    Before connecting the Arduino module to your computer, download the Arduino software from arduino.cc (it's free!).

    If you're using Windows, double-click the installer you just downloaded to install the Arduino software. Proceed through the installation process. Now, navigate to the following folder on your computer:

    C:\Program Files (x86)\Arduino\drivers

    Double-click on "dpinst-x86.exe" or "dpinst-amd64.exe" depending on if you have a 32-bit or 64-bit version of Windows respectively. This application will install drivers for your Arduino to communicate with your computer.

    If you're running the Mac OS, unzip the downloaded file and move the Arduino application to your "Applications" folder to install the Arduino software.

    You will also need to download the Processing software from processing.org (also free!).

    If you're using Windows, unzip the downloaded file. This will provide you with a folder with a name like "processing-2.2.1". Inside this folder is a file named "processing.exe". This is your Processing application.

    If you're using the Mac OS, unzip the downloaded file and move the Processing application to your "Applications" folder to install the Processing software.

    Download the sketches

    Above in the "Other files" section of this project are two files. Downloaded these files. "Analog Pong - Arduino" is the sketch for your Arduino module and "Analog Pong - Processing" is a collection of files that make up your sketch for the desktop software written in Processing.

    First, download "Analog Pong - Arduino". This will provide you with a single file named "analog_pong_arduino.ino". Double-click this file and Arduino should open and prompt you asking to move this sketch to its own project folder, click OK. The Arduino software should now be on your screen displaying the pong sketch for your Arduino module.

    Second, download "Analog Pong - Processing". This will provide you with a zip archive named "analog_pong_processing.zip". Unzip this archive. Return to the location of your Processing application and run it. Go to the menu at the top and click File > Open... Navigate to the location of your Processing sketch folder "analog_pong_processing" then select "analog_pong_processing.pde" and click Open.

    You should now have both your Arduino software open with the Arduino Pong sketch and the Processing software open with the Processing Pong sketch.

  2. 2

    Build your circuit (see the photo gallery for an image of the setup). Make sure your two dimmers are connected to the analog input pins, A1 and A0.

  3. 3

    Plug the micro USB cable into both the Arduino module and a USB port on your computer.

  4. 4

    Turn your power module on. You should see a red light illuminate on the power module and a yellow light flash on your Arduino module.

  5. 5

    In the Arduino program, select your board. Go to Tools > Board > Arduino Leonardo. This littleBits Arduino module is modeled after the Arduino Leonardo and is therefore compatible.

  6. 6

    Next select the serial port that the Arduino will communicate over by going back to "Tools" and selecting the correct port in the "Port" section. The name of the port will depend if you are on a Mac or Windows computer. On a Mac, it will start with /dev/tty.usbmodem... and a PC will start with COM… In Windows, you can look for the USB serial device in the ports section of the Windows Device Manager. If you are on a Linux machine, the port will look like /dev/ttyUSB…

  7. 7

    To upload the Arduino sketch to your Arduino module, press the arrow button in the top left corner of the screen. You will see a blue bar at the bottom of the window that says "Compiling sketch..." followed by "Uploading...", and you should see two yellow lights flash on your Arduino module when this happens. The upload is complete when you see "Done uploading" at the bottom of your sketch window.

  8. 8

    Now, switch over to the Processing sketch. Press the "Play" button in the Processing window at the top-left corner. This will open another window that says "littleBits PONG". Press any key on your keyboard to start the game and move the dimmers to adjust the Pong paddles.

  9. 9

    If you are unable to move your Pong paddles, you may need to make a slight adjustment to the code in the Processing sketch so that you connect to the correct serial port.


    Scroll down until you find this line of code:
    myPort = new Serial(this, Serial.list()[0], 9600);


    You will need to replace the 0 in Serial.list()[0]. Start counting up from 0. After each change you make, try and run the game. Each time you change this number you are essentially selecting a new serial port to communicate with.

Bits used (5)

Bundles

Or add this bundle to get all these bits and more for a price savings

Collections

Or add this collection to get all these bits and more for a price savings

Other files

  • 8 Comments
  • Donji 4 months ago

    Didn't work. Please help. The processing file is saying: Cannot find a class or type named "Paddle"

  • Donji 3 months ago

    Sweet! Thank you, I'll try that as soon as I get a chance.

  • Donji 3 months ago

    ok, now I figured part of it out. I had to add new tabs to the main file: analog_pong_processing. I copy and pasted the code from the "ball" file onto one of the tabs and named that tab "ball" I did the same for the GUI file and the Paddle file.

    It seems there is no way to add tabs from one processing window to another by dragging and dropping the tabs.

    Now that I have that figured out, I'm getting a warning saying NullPointerException and I am trying to figure out what that means.

  • Donji 3 months ago

    I'm not sure how to add the png's to the processing doc.

  • Donji 3 months ago

    Ok, now I have managed to get the GUI to load, but now the only problem is I have to figure out how to change the code so the module is compatable with dimmers instead of sliders.

    • thorprichard about 2 months ago

      @Donji, I found that it works fine with littleBits dimmers... the trick was to correctly specify the serial port in the Processing file. Another method for finding which serial port to use, I just substituted 100 for 0 from the instructions above in the analog_pong_processing file in the Processing.app.

      For example, instead of:

      myPort = new Serial(this, Serial.list()[0], 9600);

      I changed it to:

      myPort = new Serial(this, Serial.list()[100], 9600);

      Conveniently, that threw an error in the Processing.app window and helpfully printed a list of the available serial ports. This list happens to be the same as what's listed in the Arduino.app (under the "Tools" menu, "Serial Port' submenu). I then read the list of the printed list of available serial ports in the bottom portion of the Processing.app window from left to right, top to bottom, starting my count from from zero for the first item in the list, and I found the one that matched what the Arduino.app specified was #7 in this list. (Your list will be different, of course.)

      Using this method, here's how that line of code looked:

      myPort = new Serial(this, Serial.list()[7], 9600);

      And, each time I wanted to play, I had to first upload the Arduino.app file first and then run the Processing.app file to start the game. That is, if I made a change to the Processing.app file I couldn't just "stop" and "run" the change. I had to first upload the Arduino.app file again, and then run the changed Processing.app file.

      Lastly, the other change I made was to the paddles... I just thought they were too short, so I changed the padHeight value in the Processing.app file to be 120. Seems to work okay and I don't lose as often now. :-)

      I hope this helps!

  • Donji 3 months ago

    Can someone make a youtube video for this? That would be awesome. Especially if it went over every step in detail. I would totally make one, if only I could figure this last step out.

  • littleBits 3 months ago

    Hello Donji, We're sorry about the confusion with the processing code! There was a problem with uploading the files to the site, but it's fixed now and all the files are up. If you download and unzip the new Processing folder, everything should run correctly (please see our new note in step 1). Videos are coming soon!

Must be logged in to comment. Sign in. Not a member? Join now