I’m having a bit of an issue with an LCD project I’m trying to make (Pong) with my Arduino Uno. The paddles move absolutely fine without the ball, but with it, only update after the ball hits the edge, so it can’t move. I don’t know what the issue is or if t is fixable, but here is my code:
Did you convert the code to a document and then auto-translate it to something else and then copy that text from the document to this forum ?
Can you show us the actual code ?
I tried to fix the sketch and make a Wokwi simulation, but it does not do much:
Sorry part of it was lost in translation as for some reason I couldn’t save the code so I had to take pictures and copy and paste from them, which ruins some of the code, like the j:
I’m still unable to save the code so I’ll try again later
the main reason for your problem is that you move the "ball"
in a for-loop() and
with the use of delay()
If you like you may check out the sketch from my post #5 to see how the movement can be done without blocking loop() ... (This blocks and does not let the other functions get processing time).
There are also a lot of other changes, mainly to avoid double coding and to keep each routine/function short enough that it may fit on one screen without scrolling. It is also advisable to use const expressions for data which you use at more than one place and which you might want to change in future.
So you can play a bit with e.g. the WinLevel and also the left and right margins where your paddles are placed.
I’ve read the code and don’t fully understand it as I have a limited understanding of C++, which is why I am doing this project (which is why I haven’t just copy/paste it). There was a void Set that summoned the ball and a void remove to get rid of it, but I tried using just that section in my code and the ball doesn’t appear. You also mentioned how just moving the ball in a for loop is bad, and I don’t know what else to do.
as you are not the only one struggling with this topic when beginning to code I have taken some time to write some shorter examples which I coded and placed on Wokwi so that everyone interested can check the functionality:
I will publish the code in a separate thread to keep this thread clean but post the link to it after it has been established. They are probably not best practice in all aspects therefore I am open for improvements so that these examples may grow and can be used in future as a simple reference for beginners.
I checked all the links and I kind of understand how it works, so I continued the code (I’m not finished, so far only the right side can lose and the ball goes only in one direction and nothing happens if button is low which will soon change ball direction and there is no scoring system) but as soon as I add the if at the end, to add some part of pong to it, it starts to spam the words “LEFT SIDE WINS!” I also added a millis timer (I think). Here is my code: