My code is not working it is not upluding to my arduino nano

here is the code pls help i was making a ping pong game on a 8x8 matrix screen
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>

Adafruit_8x8matrix matrix = Adafruit_8x8matrix();

// Ball position and direction
int ball_x = 3;
int ball_y = 3;
int ball_dx = 1;
int ball_dy = 1;

// Paddle position
int paddle_x = 0;
int paddle_y = 0;

void setup() {
matrix.begin(0x70); // Initialize the matrix display
matrix.setBrightness(15); // Set the brightness of the display (0-15)
randomSeed(analogRead(0)); // Seed the random number generator
}

void loop() {
// Move the ball
ball_x += ball_dx;
ball_y += ball_dy;

// Check for collision with the walls
if (ball_x < 0) {
ball_x = 0;
ball_dx = -ball_dx;
} else if (ball_x > 7) {
ball_x = 7;
ball_dx = -ball_dx;
}
if (ball_y < 0) {
ball_y = 0;
ball_dy = -ball_dy;
} else if (ball_y > 7) {
ball_y = 7;
ball_dy = -ball_dy;
}

// Check for collision with the paddle
if (ball_x >= paddle_x && ball_x < paddle_x + 3 && ball_y == paddle_y) {
ball_dy = -ball_dy;
}

// Clear the display
matrix.clear();

// Draw the ball
matrix.drawPixel(ball_x, ball_y, LED_ON);

// Draw the paddle
matrix.drawRect(paddle_x, paddle_y, 2, 0, LED_ON);

// Update the display
matrix.writeDisplay();

// Move the paddle
if (digitalRead(2) == LOW) {
paddle_x--;
if (paddle_x < 0) {
paddle_x = 0;
}
}
if (digitalRead(3) == LOW) {
paddle_x++;
if (paddle_x > 5) {
paddle_x = 5;
}
}

// Pause for a short time to slow down the game
delay(50);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

You need to declare a pin the pins to use with the 8x8 DIN (data in).

[edit] Unless you have the Adafruit 8x8 backpack... that seems to do the wiring without declaring I2C (SCL, SDA) SPI (SCK, MOSI, CS) pins.

That's fine, probably.

Instead of doing that at the end of the loop, you could do this at the beginning:

void loop() {

  static unsigned long lastLoopTime;
  if (millis() - lastLoopTime < 50) return;   // too soon to loop again

  lastLoopTime = millis();

// original code below will run every 50 milliseconds.

which accomplish the same goal, but makes the frame rate consistent as long as all the calculations and stuff take less than 50 milliseconds.

It's a variation of the "blink without delay" pattern. I use it alla time for animations that want to run smoothly, like 10 ms gives a 100 Hz frame or "refresh" frequency.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.