Devices & Components
Breadboard - 400 contacts
40 colored male-male jumper wires
Arduino Uno Rev3
40 colored male-female jumper wires
arduino Joystick Module
MAX7219 module
Software & Tools
Arduino IDE
Project description
Code
cpp
1#include <LedControl.h> 2 3// MAX7219 pins: DIN, CLK, CS 4LedControl lc = LedControl(12, 11, 10, 1); 5 6// Paddle (left side) 7int paddleY = 3; 8 9// Ball 10int ballX = 4; 11int ballY = 4; 12 13int dx = 1; 14int dy = 1; 15 16void setup() { 17 lc.shutdown(0, false); 18 lc.setIntensity(0, 8); 19 lc.clearDisplay(0); 20 21 Serial.begin(9600); 22} 23 24void loop() { 25 26 // ---------- JOYSTICK ---------- 27 int joy = analogRead(A0); 28 paddleY = map(joy, 0, 1023, 0, 6); 29 30 // ---------- MOVE BALL ---------- 31 ballX += dx; 32 ballY += dy; 33 34 // ---------- WALL COLLISION ---------- 35 if (ballY <= 0 || ballY >= 7) { 36 dy = -dy; 37 } 38 39 // right wall bounce 40 if (ballX >= 7) { 41 dx = -1; 42 } 43 44 // ---------- PADDLE COLLISION ---------- 45 if (ballX == 1) { 46 if (ballY == paddleY || ballY == paddleY + 1) { 47 dx = 1; // bounce back 48 } else { 49 // reset if missed 50 ballX = 4; 51 ballY = 4; 52 dx = 1; 53 dy = 1; 54 } 55 } 56 57 // ---------- CLEAR SCREEN ---------- 58 lc.clearDisplay(0); 59 60 // ---------- DRAW PADDLE ---------- 61 lc.setLed(0, paddleY, 0, true); 62 lc.setLed(0, paddleY + 1, 0, true); 63 64 // ---------- DRAW BALL ---------- 65 lc.setLed(0, ballY, ballX, true); 66 67 delay(120); 68}
Comments
Only logged in users can leave comments