Pong: paddle movement problems, logic issue

I'm having some issues getting the paddles on a pong game I'm working on to function correctly. I'm using an large LED matrix controlled with MAX7219's thats 16 LEDs high, the paddles are 3 LED's long. I'm able to control the paddle on the bottom 8 LED's without a problem but when moving up to the top 8 LED's is where the issue occurs.

The paddle does not display correctly on the top zeros.

0
0
0
0
0
0
1
1

1
0
0
0
0
0
0
0

I think there's something wrong with the logic and I can't figure it out.

pong[0] is the bottom column and pong[1] is the top column.

Here's the paddle code:

  if(paddle1Val<=7){
     bitSet(pong[0], paddle1Val);   
    if(paddle1Val-1>=0){
      bitSet(pong[0],paddle1Val-1);
    }
    else{
      bitSet(pong[1], 7);
      bitSet(pong[1],6);
        carry==1;

    }

      if(carry==0){
        if(paddle1Val-2>=0){
          bitSet(pong[0], paddle1Val-2);
        }
        else{
          bitSet(pong[1], 7);
        }
      }
    }

  if(paddle1Val>7){
    bitSet(pong[1],paddle1Val);
    bitSet(pong[1],paddle1Val-1);
    bitSet(pong[1],paddle1Val-2);

  }

Can you guys help me fix the logic so that the paddle will move up to the top of the display?

Thanks!

Hi

You have ...

carry==1;

If you mean to set the variable carry to 1, the code should be ...

carry = 1;

Let us know what you get when you change this.

Also, we can't see where you assign an initial value to carry before getting to the code you posted. You need to make sure that it has a sensible value before you get to the if (carry == 0) conditional.

Hope this helps.

Hi Hackscribble,

Thanks for taking a look at my code.

I tried changing the carry value to = 1 but it didn't seem to change how the paddle function the same issue occurred.

Can you give a bit more detail about the design and the exact problem you are seeing?

  1. In your original post, you show two columns of eight LEDs. You say that the bottom one shows the bit pattern held in the byte in pong[0], and the top one shows pong[1]. Which way up do you display the bits? In other words, if pong[0] is set to B00000001, is it the top or bottom LED in the bottom set that goes on?

  2. Your original post shows three bits set to 1: two in pong[0] and one in pong[1]. I guess this is showing a three bit paddle in the way you want it to work?

  3. If that's what you want when the paddle moves into the top column of LEDs, what do you actually see when you run your code? Do any of the top LEDs come on?

Thanks

Ray

Some questions about your latest code posted.

I can see where you are changing the value of paddle1Val based on getting an "up" or "down" command from the user.

Couple of things. What is the valid range of values for paddle1Val? How do you want these to relate to the position of the three bit paddle in the 16 bit column?

And if the user keeps sending an "up" command, for example, I can't see the code to stop the paddle going too high out of range.

All the best

Ray

Thanks Ray I appreciate your help,

I was about to post the below info but I just saw you posted some more questions I'll answer your newest questions up here.
There is currently no limit for the paddles yet, I haven't implemented any yet. But if I press down button repeatedly it will continue to move down and off the screen. Its something that will need to be implemented eventually.

Bellow are the answers to the first questions you posted. Thanks!

Here's a description of the project, I made a large LED display using 6 MAX7219 LED driver chips. Its 16 LEDs tall and 24 LED's across. I connected a Bluetooth module on the Arduino, this part works as I'm able to move the paddles. The paddles are 3 LED's tall and I need them to be able to travel to the top and to the bottom of the column.

The problem: with the current code the paddles can move down to the bottom of the column but they do not move up past a certain point. The diagram below shows the starting state of the paddle when the program is turned on.

0
0
0
0
0
0 < ----Paddle doesn't show up here ever.
1 <---- Highest point the paddle will travel in this column
1

1
0
0
0
0
0
0
0 <-----Paddle can show up here

From this point I can press the down button and the paddle will move down. Now when the paddle is at the above position and I were to press the up button 3 times (increasing paddle1Val 3 times) the paddle will stay in this position, but it will take 4 down button presses before the paddle will move down.

Here's some extra information that I'm not sure will help or not but the Bluetooth controller on my phone receives data after I press the up or down button, I believe it is receiving the current value of paddle1Val after the button is pressed. Using the start position of the paddle when the program is turned on and pressing the up button 3 times the following data is received:
0
-1
-2

  1. In the code pong[0] represents the bottom left column, pong[1] represents the top left column. If you look in the code bellow there is an array for the display call pong[ ], remember my physical display is 16 x 24 so the one in pong[ ] is turn 90 degrees clockwise.

so for pong[0] is set to B00000001 the following led would light up

0
0
0
0
0
0
0
0

1
0
0
0
0
0
0
0

  1. Yes, I'm using a 3 bit paddle. I was using 1's to represent LED's that are turned on and 0 for LED's that are turned off.
  2. See above description

Hi Yatahaze323

Thank you for the details - very clear.

I've looked at the code again, and I think I may have found something.

From your last post, it sounds like paddle1Val starts at 0. When you press "up" button, you were expecting it to increase in value. But the data from Bluetooth you included showed it going down, -1, -2 etc.

Your code has ...

	// if the 1 is sent the incoming byte is "49", make the left paddle go up
	if (incomingP1Byte == 49)
	  {
		player1Serial.println(paddle1Val--);
		delay(10);
          }

This is decrementing paddle1Val for each up button, so after first press, it is -1, I think.

Then look at this part of your code. I have added my comments on what I think is happening.

//----------------------------------left paddle

 carry=0;

  if(paddle1Val<=7){  // this is true, since variable is -1
     bitSet(pong[0], paddle1Val); // can't logically set the -1 bit of a byte, not sure what bitSet does, probably nothing  
    if(paddle1Val-1>=0){  // this is false
      bitSet(pong[0],paddle1Val-1);  // not executed
    }
    else{
      bitSet(pong[1], 7);  // these two lines get executed
      bitSet(pong[1],6);
        carry==1; // this needs to be corrected to carry = 1
     
    }
 
      if(carry==0){  // I think this is still true, because of previous problem
        if(paddle1Val-2>=0){ // this is false
          bitSet(pong[0], paddle1Val-2); // not executed
        }
        else{
          bitSet(pong[1], 7);  // executed
        }
      }
    }

So, it looks as though, so long as paddle1Val is negative, there is no change in the bit settings in pong[0] and pong[1]. Looks like it is this part of the code that needs work.

Since you only have 14 possible positions of the paddle, an easier approach could be ...

  1. Alter paddle1Val so that it runs from 1 to 14, with 8 being the starting value, equivalent to the position shown in your diagram.

  2. Set up a two dimensional array of bytes:

byte paddleBitMap [15] [1].
  1. Initialise this with the values (bit patterns) you want in pong[0] and pong[1] for each of the 14 positions of paddle1Val.

  2. When you want to update the LEDs, you just need to do something like ...

for (i = 0; i < 2; i++)
{
   pong[i] = paddleBitMap[paddle1Val][i];
}

or simply

   pong[0] = paddleBitMap[paddle1Val][0];   
   pong[1] = paddleBitMap[paddle1Val][1];

You can use the same bit map array for the other paddle ...

   pong[46 + i] = paddleBitMap[paddle2Val][i]

NOTE: to avoid problems, you need to make sure that paddle1Val does not go outside the permitted range, so you need to work on the up / down part of your code too.

NOTE: if changing paddle1Val to run from 1 to 14 causes a lot of work in other parts of your program, you can keep it running from -6 to +7 (and initial value 0). But then you need to change the code that accesses the bit map array, like this ...

   pong[0] = paddleBitMap[paddle1Val + 7][0];   
   pong[1] = paddleBitMap[paddle1Val + 7][1];

Hope that helps

Ray

Hi Ray,

Thanks for looking everything over and explaining some of the problems. I'm gonna try implementing the ideas you posted and see if I can get it working.

Thanks again!