Red Light Green Light MicroBit Game

by hoangh in Teachers > 9

42 Views, 0 Favorites, 0 Comments

Red Light Green Light MicroBit Game

Screenshot 2024-09-30 at 2.44.21 PM.png

In this project, you’ll create a Red Light, Green Light game using two MicroBits. One MicroBit acts as the light controller (Player 1), while the other controls the runner (Player 2). The devices communicate wirelessly using the radio function, with LEDs and speed counters adding interactive elements.

Supplies

Screenshot 2024-09-30 at 4.09.56 PM.png

Materials Needed

  1. 2 MicroBits
  2. 2 battery packs (optional)
  3. 6 LEDs (Red, Yellow, Green)
  4. 6 resistors
  5. Breadboard (optional)
  6. 8 Jumper wires (6 for LEDs, 2 Ground)
  7. programming environment (MakeCode)

Set Up the Circuit

Screenshot 2024-09-30 at 2.53.22 PM.png
  1. Connect the LEDs to your micro:bit:
  2. Red LED to Pin P0 (for Red Light)
  3. Yellow LED to Pin P1 (for Yellow Light)
  4. Green LED to Pin P2 (for Green Light)
  5. Use resistors to prevent your LEDs from burning out, connecting each resistor between the LED and the ground pin of the micro
  6. Wire up the power: Connect the ground (GND) and 3V pins on your MicroBit to power the LEDs.
  7. Do this on each of the MicroBits.

Program Player 1 (Light Controller)

Screenshot 2024-09-30 at 2.46.14 PM.png
Screenshot 2024-09-30 at 2.50.40 PM.png
Screenshot 2024-09-30 at 2.51.09 PM.png
  1. Initialize the LEDs: In your code, configure Pins P0, P1, and P2 to control the LEDs.
  2. Set up the radio communication: Use radio.setGroup(44) to set up a communication channel between the two MicroBits
  3. Button A for yellow light and red light:
  4. When Button A is pressed, the yellow LED lights up and sends a signal (2) to Player 2.
  5. After 2 seconds, the red LED turns on, and a signal (1) is sent to stop Player 2.
  6. Button B for green light:
  7. When Button B is pressed, the green LED lights up, and a signal (3) is sent, allowing Player 2 to continue.
  8. Visual Feedback: Player 1 gets visual feedback via MicroBit icons, such as a heart for the start of the game, or a happy/sad face depending on win/loss scenarios.


Program Player 2 (Runner)

Screenshot 2024-09-30 at 2.54.50 PM.png
  1. Speed Control:
  2. Player 2 controls the speed of their "running" using Button A (to decrease speed) and Button B (to increase speed).
  3. Stopping on Red Light:
  4. The radio receiver listens for the red light signal (1). If Player 2’s speed is not 0 when they receive this signal, an attempt is lost. Otherwise, Player 2 wins a point.
  5. Feedback and Win Condition:
  6. The game tracks Player 2’s score and remaining attempts. If Player 2 reaches a score of 5, they win the game, and a skull icon is displayed on Player 1’s micro
  7. Player 2 loses if all their attempts are used up, in which case a skull icon is shown.

*The image is using the simulator because it was difficult to get a picture of the radio communication*

Code Logic for Radio Communication

Screenshot 2024-09-30 at 2.58.18 PM.png
  1. Sending and Receiving Signals:
  2. Player 1 sends signals for red, yellow, and green lights using the radio (radio.sendNumber(1) for red, 2 for yellow, and 3 for green).
  3. Player 2's MicroBit listens for these signals and adjusts their speed or stops when necessary.
  4. Handle Win/Loss Scenarios:
  5. If Player 2 stops in time on a red light, a signal (4) is sent to Player 1, who receives it and updates the game status.
  6. If Player 2 fails to stop in time, signal 5 is sent, and Player 2 loses an attempt.

*The image is using the simulator because it was difficult to get a picture of the radio communication*

Test and Debug

Screenshot 2024-09-30 at 10.54.07 PM.png
  1. Upload the Code to both MicroBits using the MakeCode environment.
  2. Test the interaction between the two MicroBits. Press the logo button on Player 1’s MicroBit to start the game, and use Buttons A and B to control the lights and runner speed.
  3. Check the LEDs to make sure they light up in response to the signals. Ensure that Player 2’s speed updates correctly, and the game ends based on score or attempts.



Play the Game

Screenshot 2024-09-30 at 10.53.29 PM.png
  1. Start the Game: Press the logo button on Player 1's MicroBit to start.
  2. Control the Lights: Player 1 controls the lights by pressing Buttons A and B.
  3. Run and Stop: Player 2 must respond to the lights by adjusting their speed using Buttons A and B.
  4. Win or Lose: Player 2 wins by reaching a score of 5 or loses if all attempts are used. If player 2 loses, player 1 wins!