Week 03: Embedded Programming

Individual Assignment:
This week, I walked through learned how to use the Seeed XIAO RP2040 microcontroller board, and programmed it to communicate remotly

1. Browsed the data sheet for the microcontroller

2. Downloaded the Arduino IDE and connected the microcontroller

3. Use the Blink example in the library to test blink the microcontroller

4. I soldered the microcontroller into a cute tiny breadboard

5. I connected a small 1"x.5" OLED screen to the controller and test run it

To run OLED with the RP2040, I referred to the tutorial provided at wiki.seedstudio.com

link to the tutorial

6. I integrated a joystick and created a simple a pong game

              
                #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.

We wrote the code to be exactly the same as the Arduino IDE example.