2

I am working on a project for my brothers and anyone else who is interested in playing some LED Pong. I was just stuck on the software part. I don't really know what to do. But, I have an idea: I want to use the map() function to make my life easier, however, I am not sure how to use the map function with the Dot Matrix. All help would be appreciated. All of the stuff came in this kit.

Here is the test code:

#include <LedControl.h>
int DIN = 2;
int CS = 3;
int CLK = 4;
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);
}
void loop() {
 byte a[8] = {0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 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, 180);
}

Here is the other code:

#include <LedControl.h>
int DIN = 2;
int CS = 3;
int CLK = 4;
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]);
 }
}

Here is the picture of the wiring and the schematic:

This is the wiring picture

Wiring Pic

Wiring Pic

Wiring Pic

Schematic

asked Jul 13, 2021 at 20:22
2
  • Your ball will either go straight or diagonally at 45° ? Commented Jul 14, 2021 at 13:22
  • Yes, the ball will go straight, I don't know about the diagonally part, but it won't move the paddle. Commented Jul 14, 2021 at 14:56

1 Answer 1

2

First figure out the min and max values that paddleX can have. I assume if the paddle is two dots wide then its min,max will be 0,6 Then figure out the min and max of your pot, typically 0,1023 for a a 10 bit adc. Now to map your pot value to paddleX value you'd use the command :

paddleX = map(analogRead(A0),0,1023,0,6);

here is the map documentation : https://www.arduino.cc/reference/en/language/functions/math/map/

answered Jul 14, 2021 at 5:46
3
  • For an evenly spaced mapping paddleX = map(analogRead(A0),0,1024,0,7); would provide better results. ( Integer division truncates ) Commented Jul 14, 2021 at 13:12
  • Yes, the paddle is two dots wide. Commented Jul 14, 2021 at 14:57
  • I don't really know what to do from there. Commented Jul 14, 2021 at 15:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.