In my quest to learn EV3 programming I realized that it is not just robots what can be coded into the little Brick. The screen isn’t really that good with 178×128 pixels resolution but I can do a few tricks with it.
So what Bouncing Ball is, it is a little demo where a ball bounces on the screen. I have learnt how to use variables and wires with it because I really didn’t understand how wires and variables where supposed to work. Now I do.
Let me show you the code, I comment through it and then I explain you what I exactly learnt. By the way, would you like a tutorial about how to code with EV3? I would love to write it but I would need your help so the less you know and the more you want to learn the better. Just write me a comment here so we can work it out.
Bouncing Ball EV3 Code
Before writing any code, in any kind of language, I really enjoy writing first a little pseudo-code that help me divide the problem in smaller simpler parts ( divide and conquer approach ).
So here is the pseudo code.
We represent our ball with a position give by x,y and with a speed dx, dy on each axis.
x, y = 0
dx, dy = 1
x = x + dx
if x is not in the range [0,178] then
dx = dx * -1
y = y + dy
if y is not in the range [0,128] then
dy = dy * -1
draw cicle on x,y
Mainly we just increase x and y until we see any of them get out of the boundaries, when we see it we reverse the direction and done. The result is pretty much like this…
So the EV3 Code would be like…
You can download the EV3 source code here.
EV3 Source Code file
A Variable block is a container of a type of data that can be numeric, text or logic.
You can create new variable by click on the corner and writing the variable name.
There are two basic operations that can be done with this block:
This is the default mode when you add a variable block. It is always a very good idea to initialize your variables to a known value. By default EV3 assign zero to the variables using this block, edit it or use a wire.
When you set the mode to read the variable will make available through a wire the value it has inside that can be either a number, text or logic as usual.
Variables and wires
So when you do something like this
The variable has a value inside and send it to the screen. You will see the number 4…
But what happens on this case?
The variable is evaluated and 4 is placed on the wire, then the variable is changed but as it isn’t evaluated again the wire still has 4.
Keep this in mind when developing apps for your robot.
The screen refresh rate is quite poor so it is hard to get a nice experience but it is a great way of learning some features of the EV3 code as ranges, variables and switches.
Hope you enjoyed it. And I am listening to your suggestions or problems.