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

    Download the Arduino software to your computer (it's free!) and open the "Analog Pong" sketch. You will also need to download Processing to run the Processing sketch.


    The "Analog Pong" Arduino and Processing files can be found on this page in the "other files" section. When you unzip the analog_pong_processing folder, you will want to open and run the file named analog_pong_processing.pde
    NOTE: Don't take this file out of the folder. It needs to be in the same location as the four other files in the folder.

  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 COMM… 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", 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 (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. We found that we needed to replace the 0 with the number 2.

Bits used (5)

Collections

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

Other files

  • 8 Comments
  • Donji 3 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 1 month 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