Sound Card Oscilloscope & Arduino

How Ping-Pong Game are Implemented #arduSerie 28

J3
Jungletronics
5 min readMar 14, 2017

--

Hello, hobbyist!! welcome!!! Let’s playing Ping-Pong with arduino?
we start by tell you how does Pong Game code work.

Explaining interruptions on Arduino via SoundCard Oscilloscope (c. Zeitnitz V 1.46)

I’ve already done that in others occasions but now let’s go to the extra mile…
I’m going to leave all these code for download in this web page so please bear with me for the next few minutes because I thought it would be a fun experience!!!
In order to deal with this project, the first thing you need to know is how interruptions occurs inside ATmega328p Chip.
Here is a minimal common code for it:

Let’s take a closer look at the code above with the help of an sound card oscilloscope and find out how they generate a three differents PWM signal on pins 3, 4 and 5 of arduino.

But before, see, please, this video:

Every 1 millisecond an interrupt is generated and the routine within the ISR method has the opportunity to run …Cool.
By the way, ISR stands for Interrupt Service Routine.

Each of these pins runs at different frequencies due to the counting in binary fashion. See and understand…

BIT_0 Counting at 500 Hz (pin 3)
BIT_1 Counting at 250 Hz (pin 4)
BIT_2 Counting at 125 Hz (pin 5)

Since there is an interruption generated every millisecond, we already calculated in a mere 3 microseconds the run of ISR routine ( see in this video) Cool!

Now let’s put more code inside. See this second version:

We have now a fake PING PONG implemented. It’s fun, but it gets boring in a minute or so…
Fortunately, there is a lot more we can see here…but, better, lets run the last code and calmly analyze each condition, line by line:

See this graphs please:

This is a modeling spreadsheet to explain the main concepts of ping pong.

AGAINST THE PADLLE & AUTOPLAY:

See this formula:

if ((pos_x > 6) && (speed_x == 1) && (pos_y < (paddle_r + 1)) && (pos_y > (paddle_r — 1))) { speed_x = -1; }

Let’s suppose the ball leaves that place (F8) in the positive direction (for the right).
Coming that way it goes against the right padlle-bat.
There (F11), exactly when the ball is in, it meets the first two clauses ((pos_x > 6) && (speed_x == 1)) of the above equation, as well as the third and fourth clause, since pos_y is greater than padlle-bat minus 1 and less than padlle-bat plus one.
Bingo!!! the ball will reverse direction when hitting the right padlle-bat.

if ((pos_x < 1) && (speed_x == -1) && (pos_y < (paddle_r + 1)) && (pos_y > (paddle_r — 1))) { speed_x = 1; }

Now, suppose again the ball is right there (D6),
Going in that direction (to the left) reaches that point (D4) the ball
will revert the direction again as it meets all the clauses of that equation above.
See that it comes back in a straight line.
We will see how to make it go back in angles in a moment.

AUTOPLAY or ONE-PLAY MODE:

This part tells who’s in command. autoplay or one-play mode.
autoplay mode never misses!!!
The paddle-bat always chases the ball along the y axis…

BACK IN ANGLES:

We add velocity on the x-axis to make the ball touch the walls and not just follow in a straight line;
From now on the ball makes angles with the walls.
For the ball does not escape, the next two ifs must hold it inside the space of the game.

CEILLING:

If the ball reaches that position (B3…I3), it will reverse its direction with respect to the y-axis.

BOTTOM:

At the same time if the ball reaches that region (B10 — I10), it will also reverse its direction with respect to the y axis for the reverse reason.

SERVING BALLS

If the ball reaches that position (A3..A10) without reaching the padlle-bat (one-play mode) then it appears on the opposite side.
The same thing happens on the y-axis, only in reverse order.

Here is schematic:

Alright, I had read a lot, did some Internet research and wrap up this information on my web page.
Please, give me a visit (or see others video) and give me a chance to show you off my ideas.
You’re watching the jungletronics channel. This is your host, j3.
As always leave your thoughts in the comments section below. and If You learned something with this Video, Give It a Thumbs Up!

Download all Archive for this Project

References:

Worksheet Explaination

Soundcard Oscilloscope by Christian Zeitnitz

Arduino’s Youtube Videos

Proteus — Arduino & IC 74HC138 — Simple Ping Pong Game

Arduino Pong Game w/ IC 74HC138!!! 01 #arduSerie

Ping-Pong Game Explained!!! Simulation on Atmel Studio 7 — #arduSerie 25

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!