Arduino Pong With Dot Matrix

I am not really sure what section to put this under, but I just picked this one. Anyways, I am working on a project for my brothers and want to play some Pong with them, but I don't really how on a Dot Matrix from [this kit].((https://www.amazon.com/EL-KIT-008-Project-Complete-Ultimate-TUTORIAL/dp/B01EWNUUUA/ref=sr_1_1_sspa?dchild=1&keywords=arduino%20mega%20kit&qid=1626207537&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExR1NEMVNXUkpRS0ZDJmVuY3J5cHRlZElkPUEwODgxNjc5MzA2V1U5RFFLVEpBSSZlbmNyeXB0ZWRBZElkPUEwNTQyNzgzMllKVEdLM0pFNURRViZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=)). I had an idea: to use the map function. I don't really know how to implement this with the Dot Matrix. I have an Arduino Mega along with two potentiometers to accompany my project. All help will be appreciated. Thanks, Austin.

#include <LedControl.h>

int DIN = 10;
int CS =  11;
int CLK = 12;

const int playerOnePin = A0;
const int playerTwoPin = A1;

int playerOneValue;
int playerOneValue2;
int playerTwoValue;
int playerTwoValue2;

LedControl lc = LedControl(DIN, CLK, CS, 0);

void setup() {

  lc.shutdown(0, false);      //The MAX72XX is in power-saving mode on startup
  lc.setIntensity(0, 15);      // Set the brightness to maximum value
  lc.clearDisplay(0);          // and clear the display

  pinMode(playerOnePin, INPUT);
  pinMode(playerTwoPin, INPUT);
  Serial.begin(9600);

}

void loop() {

  readPot();
  movePaddle();
  defaultPaddle();

}

void defaultPaddle() {
  byte a[8] = {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

  if (playerOneValue2 == 0) {
    printByte(a);
  }
}

void printByte(byte character []) {

  int i = 0;
  for (i = 0; i < 8; i++)
  {
    lc.setRow(0, i, character[i]);
  }
}

void readPot() {

  playerOneValue = analogRead(A0);

  playerOneValue2 = map(playerOneValue, 0, 1023, 0, 7);

  Serial.println(playerOneValue2);
}

void movePaddle() {

  if (playerOneValue2 == 1) {
    displayNewPaddle();
  }
}

void displayNewPaddle() {
  byte a2[8] = {0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,};
}

#include <LedControl.h>

int DIN = 10;
int CS =  11;
int CLK = 12;

byte displayImage[8] =     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// considering a 2d co-ordinate system with origin (0,0) at bottom left corner
int ballX = 3; //X position
int ballY = 7; //Y position - top
int speedX = 0; // no X movement, can be -1, 0 or 1
int speedY = -1; // downward Y movement, can be -1, 0 or 1
int paddleX = 4; // X position of center of paddle - can be 1 to 6. Paddle width is 3
int score = 0;

LedControl lc = LedControl(DIN, CLK, CS, 0);

void setup() {
  pinMode (A0, INPUT);
  pinMode (A1, INPUT);
  lc.shutdown(0, false);      // Keep MAX72XX awake
  lc.setIntensity(0, 15);     // Set the brightness to maximum value
  lc.clearDisplay(0);         // and clear the display
}

void loop() {
  // update ball position
  ballX = ballX + speedX;
  ballY = ballY + speedY;

  // check for ball interaction with walls
  if (ballX == 0 || ballX == 7) {
    speedX = speedX * -1; // bouncing off walls in horizontal direction
  }

  // bouncing off ceiling
  if (ballY == 7) {
    speedY = speedY * -1; // bouncing off the ceiling
  }

  // bouncing off the paddle
  if (ballY == 0 && ballX >= (paddleX - 1) && ballX <= (paddleX + 1)) {
    speedY = speedY * -1;
    score++;    // player earns a point
  }

  // going past the paddle
  if (ballY == 0 && ballX < (paddleX - 1) && ballX > (paddleX + 1)) {
    // going past the paddle - player is out
    Serial.println(); Serial.print("Score: "); Serial.println(score);
    for (int i = 0; i < 8; i++) {
      displayImage[i] = 0x00;
    }
    displayImage[3] = 0xFF; displayImage[4] = 0xFF; // show a line
    renderByte(displayImage);
    while (1); // Freeze
  }
  // clearing the image variable
  for (int i = 0; i < 8; i++) {
    displayImage[i] = 0x00;
  }

  // generating new image
  addPixel(ballX, ballY); // adding the ball position to image
  addPixel(paddleX - 1, 0); addPixel(paddleX, 0); addPixel(paddleX + 1, 0);
  // adding paddle position to image
  renderByte(displayImage); // show the generated image

  // handling paddle control
  if (analogRead(A0) && paddleX > 0) {
    paddleX = paddleX - 1; // move paddle left
  }

  if (analogRead(A1) && paddleX < 7) {
    paddleX = paddleX + 1; // move paddle right
  }

  delay(200);
}

void addPixel(int xVal, int yVal) {
  int newRowval = 2 ^ (7 - xVal);
  int rowVal = displayImage[7 - yVal];
  displayImage[7 - yVal] = rowVal || newRowval;  // inserting a 1 at the required pixel
}

void renderByte(byte image [])
{
  int i = 0;
  for (i = 0; i < 8; i++)
  {
    lc.setRow(0, i, image[i]);
  }
}





This makes two of us. map function converts value from one range into another, what this has to do with dot matrix, who knows

Sorry, my code was faulty. The new code up top is the correct one.

What was the question?

I built one of these a few years ago with a student.
We used rotary encoders because they spin forever.

Our development sketches:
Read a single encoder to serial
Read both encoders to serial
Turn on screen and make ball bounce all around the screen
Add a paddle to the screen that the ball bounces off of
Move the paddle with an encoder
Add dynamics to the bounce, center point = even bounce, edge points change angle of bounce
Add a second paddle
Add scoring function

It looks like you're on your way already.

@Johan_Ha Basically, my question was: how do I use the map function to control the movement of the paddle across the Dot Matrix?

@er_name_not_found Yes, but I don't know how to move the paddle vertically is the problem.

A simple way is to store the position of the center of the paddle as a byte.
Use this variable to draw the paddle, all the necessary points are relative to it.
When the paddle moves up turn off the training LED and turn on the leading LED, then increment the position variable.

@er_name_not_found I am very confused.

Then simplify.
Save your code, then save it to a new file of a different name and comment out all game code, then you can focus completely on making a paddle.
Make a variable to store an LEDs location, then blink that LED and post the code.
I will help you step by step.
Any LED is fine

@er_name_not_found I don't know how to store the LED's location.

What do you pass as a parameter to turn on an individual LED?

@er_name_not_found This?

  printByte();
  byte a[8] = {0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,};

So you get a reading from your potentiometer. Say you get 132 as the lowest value and 1021 as the highest value. How many different positions can the paddle have? Say that you have 7 positions. You want to translate the potentiometer position to 7 different values. You do something like:

pot = analogRead(A0);
paddle = Map(pot, 132, 1021, 0, 6);

Now you have the value in paddle. A value from 0 to 6, which describes where to place the paddle.

I made this for you. It should be much easier for you to use.
See what you can do with it. I suggest making a dot move around the screen first.

#include "LedControl.h"
const byte DIN = 6;
const byte CS = 7;
const byte CLK = 8;

LedControl screen = LedControl(DIN, CLK, CS, 1);
byte board[8] = {B00000000 , B00000000 , B00000000 , B00000000 , B00000000 , B00000000 , B00000000, B00000000};
byte row;

// Update display to reflect board
void print_board() {
  for (row = 0; row < 8; row++) {
    screen.setRow(0, row, board[row]);
  }
}

// value = 1 turn on led
// value = 0 turn off led
// choose row and column
void set_led(byte row, byte column, byte value) {
  if (value == 0) {
    board[row] = bitClear(board[row], column);
  } else {
    board[row] = bitSet(board[row], column);
  }
}

void setup() {
  screen.shutdown(0, false);
  screen.setIntensity(0, 8);
  screen.clearDisplay(0);
}

void loop() {
  set_led(0, 0, 1); // turn on the led in the corner
  set_led(4, 4, 1); // turn on an led in the center of the board
  print_board();
}

@er_name_not_found Error in code:

`Arduino: 1.8.15 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

LDMTestPong:39:0: error: unterminated argument list invoking macro "bitClear"

exit status 1

unterminated argument list invoking macro "bitClear"



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

My mistake. I do not see how but a closing parenthesis was left off. I updated the code. Try again please

@er_name_not_found Yes, that displayed a dot in the top-right corner and toward the center.

@er_name_not_found I have this so far (it kinda works):


#include "LedControl.h"
const byte DIN = 10;
const byte CS = 11;
const byte CLK = 12;

const int playerOnePin = A0;
const int playerTwoPin = A1;

int playerOneValue;
int playerOneValue2;
int playerTwoValue;
int playerTwoValue2;

LedControl screen = LedControl(DIN, CLK, CS, 0);
byte board[8] = {0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00};
byte row;


void setup() {
  screen.shutdown(0, false);
  screen.setIntensity(0, 8);
  screen.clearDisplay(0);
  pinMode(playerOnePin, INPUT);
  pinMode(playerTwoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  readPot();
  movePaddle();
  print_board();
}

// Update display to reflect board
void print_board() {
  for (row = 0; row < 8; row++) {
    screen.setRow(0, row, board[row]);
  }
}

// value = 1 turn on led
// value = 0 turn off led
// choose row and column
void set_led(byte row, byte column, byte value) {
  if (value = 0) {
    board[row] = bitClear(board[row], column);
  } else {
    board[row] = bitSet(board[row], column);
  }
}

void readPot() {

  playerOneValue = analogRead(A0);

  playerOneValue2 = map(playerOneValue, 0, 1023, 0, 7);

  Serial.println(playerOneValue2);
}

void movePaddle() {

  set_led(playerOneValue2, 7, 1);

}

Cool, keep playing with it. I will see if I can give you some pointers later.
I figured you'd make some progress with a more straightforward interface.