Individual Assignment:
This week, I walked through learned how to use the Seeed XIAO RP2040 microcontroller board, and programmed it to communicate remotly
#include
#include
#include
Adafruit_SSD1306 myDisplay(128, 64, &Wire);
int paddleX; // X-coordinate of the paddle
int paddleY; // Y-coordinate of the paddle
int paddleWidth = 20;
int paddleHeight = 5;
int ballX; // X-coordinate of the ball
int ballY; // Y-coordinate of the ball
int ballSpeedX = 1; // Ball's horizontal speed
int ballSpeedY = 1; // Ball's vertical speed
int joystickXPin = A0; // Analog pin for joystick X-axis
int joystickYPin = A1; // Analog pin for joystick Y-axis
void setup() {
myDisplay.begin(SSD1306_SWITCHCAPVCC, 0x3C);
paddleX = 54; // Initial paddle position
paddleY = 56; // Initial paddle position
ballX = 64; // Initial ball position
ballY = 32; // Initial ball position
}
void loop() {
myDisplay.clearDisplay();
// Read joystick values for paddle control
int joystickX = analogRead(joystickXPin);
paddleX = map(joystickX, 0, 1023, 0, 108);
// Ensure the paddle stays within the screen boundaries
paddleX = constrain(paddleX, 0, 108);
// Update ball position
ballX += ballSpeedX;
ballY += ballSpeedY;
// Check for ball collisions with screen edges
if (ballX <= 0 || ballX >= 127) {
ballSpeedX = -ballSpeedX;
}
if (ballY <= 0 || ballY >= 63) {
ballSpeedY = -ballSpeedY;
}
// Check for ball collision with paddle
if (ballY >= paddleY && ballY <= paddleY + paddleHeight &&
ballX >= paddleX && ballX <= paddleX + paddleWidth) {
ballSpeedY = -ballSpeedY;
}
// Draw paddle and ball
myDisplay.fillRect(paddleX, paddleY, paddleWidth, paddleHeight, WHITE);
myDisplay.fillCircle(ballX, ballY, 3, WHITE);
myDisplay.display();
delay(10); // Adjust the delay for game speed
}
The code above is developed with the help of ChatGPT
With my group, I compared the Ardunio IDE "Blink" example running on the RP2040 to an equivalent MicroPython code running on a virtual microcontroller.