28 February 2026

Scaffolding: Mapping "Tap Fast"

The concept of the game is to try to press a button at the same time that the LED goes over the other LED, so it's just a timing game where you have to time up your button press with the LED lights crossing over. In our game however you have to use the light sensor instead of a button to when the LED light goes over the indicated light and once you do that it the game ends and you hear a victory jingle and if you don't do it you can try again by restarting the game by using the switch to turn of the playground and turn it on again. You can also use the potentiometer to change the brightness of the LED to whatever you want.

CODE:

#include <Adafruit_CircuitPlayground.h>

int PIN_SWITCH = A3;
int PIN_POT    = A2;

int GREEN_light = 0;
int RED_light = 1;

int next = 0;

bool isSwitchOn() {
  return digitalRead(PIN_SWITCH) == LOW;
}

void setup() {
  CircuitPlayground.begin();
  pinMode(PIN_SWITCH, INPUT_PULLUP);
}

void loop() {

  // Brightness control by the potentiometer
  int pot = analogRead(PIN_POT);
  int brightness = map(pot, 0, 1023, 0, 255);
  CircuitPlayground.setBrightness(brightness);

  // If switch OFF → clear and stop
  if (!isSwitchOn()) {
    CircuitPlayground.clearPixels();
    return;
  }

  int now = millis();

  // Move red every 1000 millisec, and return to 0 after circle
  if (now - next >= 1000) {
    next = now;
    RED_light = RED_light + 1;
    if (RED_light >= 10) RED_light = 0;
  }

  // clear both light and redraw green
  CircuitPlayground.clearPixels();

  CircuitPlayground.setPixelColor(GREEN_light, 0, 255, 0);


 // If red hits green show yellow and check light sensor to win
if (RED_light == GREEN_light) {
  CircuitPlayground.setPixelColor(GREEN_light, 255, 255, 0);

  int lightValue = CircuitPlayground.lightSensor();

  if (lightValue < 50) {
    for (int i = 0; i < 10; i++) {
      CircuitPlayground.setPixelColor(i, 0, 255, 0);
    }
    CircuitPlayground.playTone(990, 60);
    delay(70);
    CircuitPlayground.playTone(1320, 120);
    delay(2000);              
    NVIC_SystemReset();
  }

} else {
  // else keep red light moving
  CircuitPlayground.setPixelColor(RED_light, 255, 0, 0);
}
}

Video: https://www.youtube.com/watch?v=1mJ_hX0fnII








No comments:

Post a Comment

Note: Only a member of this blog may post a comment.