Bouncing ball

Using the OLED display from the 30 Days Lost in Space kit, I have a frame around the entire display with a ball bouncing around inside it.

COMPONENTS AND SUPPLIES

OLED Display

1

220 Ohm Resistor

1

Male To Male Wire

8

Small Breadboard

1

Switch Push Button

1

ABOUT THIS PROJECT

I thought it might be fun to try to create a simple pong type game where you simply try to keep the ball from hitting the bottom of the screen.  This was just a test to see if I could create a bouncing ball inside a frame.  Eventually I will get a paddle in there with some additional buttons to control it.

For now, it always starts at a random position and direction.  The button is used to “pause” it for debugging purposes.

CODE

				
					
				
			

SCHEMATICS

COMMENTS

Comments (6)

  • Austin Eckman Reply

    Well Done!!

    May 11, 2021 at 11:05 am
  • Stefano Nicolini Reply

    I messed up the schematic for the button. One side of the button should go directly to 5v. The other side should go to pin2 AND to the resister which then goes to ground. It should be a pull down resister.

    May 11, 2021 at 3:03 pm
    • Belrey Dahonog Reply

      Hi Stefano, I’ll try to update your image using fritzing for more accurate representation, let me know if I got the image right.

      May 16, 2021 at 12:03 am
  • Stefano Nicolini Reply

    I downloaded fritzing and created a new schematic. How can I upload it?

    May 20, 2021 at 11:36 am
    • Belrey Dahonog Reply

      Do you see any edit button on your end? I’ve tried to update the schematic but the public post won’t show the updated one. We’ve forwarded this concern to the devs for fixing.

      May 20, 2021 at 11:44 pm
  • Stefano Nicolini Reply

    No, there is no option to edit the post or any part of it.

    May 21, 2021 at 3:57 pm

Leave a Reply to Stefano Nicolini Cancel reply

Your email address will not be published. Required fields are marked *

#include U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI //#define DEBUG // width and height of the screen int width; int height; // position of the ball double ballX; double ballY; // direction it is moving in int xDir = 1; int yDir = 1; // slope will negate when a wall is hit double slope; double yIntercept; #define ballRadius 2 #define buttonPin 2 // draws the frame and the ball at the current location void draw(void) { u8g.setFont(u8g_font_unifont); u8g.drawFrame (0, 0, width, height); u8g.drawDisc (round(min(max(ballX, ballRadius+1), width-ballRadius-1)), round(min(max(ballY, ballRadius+1), height-ballRadius-1)), ballRadius); } void setup() { #ifdef DEBUG Serial.begin(9600); #endif // i am rotating 90 degrees because it seems to move better with the x axis shorter than // the y axis, since the x axis is currently driving this u8g.setRot90(); // assign default color value u8g.setColorIndex(1); // pixel on pinMode(buttonPin, INPUT); // set up random number generator randomSeed(analogRead(0)); // get dimensions of the screen width = u8g.getWidth(); height = u8g.getHeight(); // randomize start position of the ball and the slope ballX = random(ballRadius+1, width-ballRadius-1); ballY = random(ballRadius+1, height-ballRadius-2); slope = random(20, 500) / 100.0; yIntercept = ballY – ballX * slope; } void loop() { // read if button was pressed int button = digitalRead(buttonPin); #ifdef DEBUG Serial.print(“X=”); Serial.print(ballX); Serial.print(” Y=”); Serial.print(ballY); Serial.print(” slope=”); Serial.print(slope); Serial.print(” Y Intercept=”); Serial.print(yIntercept); Serial.println(); #endif // if button was pressed, do nothing as long as it is pressed if (button == HIGH) { while (digitalRead(buttonPin) == HIGH) { } } // if the ball gets out of range of the side borders, change direction and slope if ((ballX = width-ballRadius-2)) { xDir = -xDir; slope = -slope; yIntercept = ballY – (ballX * slope); } // if the ball gets out of range of the top/bottom borders, change direction and slope if ((ballY = height-ballRadius-2)) { yDir = -yDir; slope = -slope; yIntercept = ballY – (ballX * slope); } // change x position based on direction ballX += xDir*4; // set y position based on formula for a line ballY = slope * ballX + yIntercept; // draw it u8g.firstPage(); do { draw(); } while (u8g.nextPage()); }
PROJECT AUTHOR
Leave a comment
TEAM MEMBERS
Stefano Nicolini

Developer

TABLE OF CONTENTS
Leave a comment
Share on whatsapp
Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest
Share on email

SIMILAR PROJECTS YOU MIGHT LIKE

I cascaded four SN74HC595 shift registers to control 32 LEDs on 4 registers using 3 wires. I am adding a I2C 20×4 LCD screen to display the register and led values. I might add a standard 16×2 LCD screen to show up it is done.
A simple light meter. It’s not calibrated to any meaningful scale, but the digital readout shows a higher number the brighter is the light falling on the photoresistor.