01 March 2026

Scaffolding: Mapping - Water Level Game

    Our game concept is that you are trying to fill a bucket up to an exact point with water. Clicking the button will start the game, a red led will appear which is the point you should fill the 'water' or LEDs up to. The red LED will go away and the blue LEDs will slowly 'fill up' the entire circuit playground. The rate at which they fill up will be determined by how much the potentiometer is turned. We decided to treat it like a water valve where if you turn it all the way right the LEDs wouldn't fill up but if you turn it all the way left the LEDs would fill up really fast. We also made it so that if you accidentally filled up the 'water' too much you were able to dump it out by tilting the circuit playground. This way you could still make some mistake and be able to get a win. Once the water level was at the red led, all you have to do is push down the button again and if you were correct in filling up the LEDs to the right point then the whole circuit playground will light up green, otherwise it would light up red showing you were wrong. After that the cycle repeats and another red LED appears showing where to fill the 'water' up to. 

 

 


 

#include <Adafruit_CircuitPlayground.h>
#include <Adafruit_Circuit_Playground.h>

int gameRound = 0; // Current Round of the game
int score = 0; // Player's current score

float elapsedTime = 0; // Elapsed time since start of round
float baseRoundTime = 5; // The amount of time, in seconds, the current round will last for
bool roundActive = false; // Determines whether or not a round of the game is currently in progress
int roundTarget; // The goal the player is trying to hit every round

float bucketProgress = 0; // Determines how much of the bucket is currently full at any given time
int bucketFloor; // The last whole number the player passed when filling up the bucket
float fillAmount = .01; // Base amount of the bucket filled every tenth of a second (Filling will go faster the more the potentiometer is turned)
float pourAmount = .01; // Base amount of the bucket wich is poured out every tenth of a second (Pouring will go faster the more it is tilted)

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  CircuitPlayground.begin();
  delay(1000);
}

void loop() {
  // Button Val
  // put your main code here, to run repeatedly:
  int buttonVal = analogRead(A3);
  int buttonMapped = map(buttonVal, 0, 1000, 0, 1);
  //Serial.println(buttonVal);
  Serial.println(buttonMapped);

  //Potentiometer Val
  int potentiometerVal =analogRead(A2);
  int potentiometerMapped = map(potentiometerVal, 0, 1024, 0, 10);
  //Serial.println(potentiometerVal);
  //Serial.println(potentiometerMapped);

  // Accelerometer Code
  int accelerometerVal = CircuitPlayground.motionX();
  int accelerometerMapped = map(accelerometerVal, -10, 10, 0, 6);
  //Serial.println(accelerometerVal);
  //Serial.println(accelerometerMapped);

  // Generate Random number for difficulty
  //int roundTarget = random(0, 10);
  //Serial.println(roundTarget);

  // Value Monitoring in Console
  /*
  Serial.print("Potentiometer Val: ");
  Serial.print(potentiometerMapped);
  Serial.print(", Button Val: ");
  Serial.println(buttonMapped);
  */

  // Game Code
  // Round is not currently active
  if (!roundActive)
  {
    // Start a round of the game
    roundActive = true;

    // Ensure the bucket is empty at the start of every round
    bucketProgress = 0;

    // Set a random pixel as the goal for the round
    roundTarget = random(0, 9);  // Random Number between 0 and 8
    // Range is set so that final pixel will never be the target
    CircuitPlayground.setPixelColor(roundTarget, 50, 10, 10);

    // 1 Second Delay
    delay(1000);
  }
  // Round is currently in progress / Bucket is filling
  else
  {
    // Fill the bucket
    bucketProgress += fillAmount + (0.05 * potentiometerMapped);

    // If the CPE is tilted too much...
    if (accelerometerMapped <= 2 || accelerometerMapped >= 5)
    {
      int pourFactor = 3 - accelerometerMapped;
      if (pourFactor < 0)
      {
        pourFactor *= -1;
      }
      //Serial.print("PourFactor: ");
      //Serial.println(pourFactor);
      // Pour out Water
      bucketProgress -= pourAmount + ( 0.2 * pourFactor);
    }
    else
    {
      pourAmount = 0;
      //Serial.println("Not Pouring.");
    }

    // Update LEDs
    // Clear Current LEDs
    CircuitPlayground.clearPixels();
    // Turn on the amount of LEDs equal to the floor of bucket progress
    bucketFloor = floor(bucketProgress);
    for (int i = 0; i <= bucketFloor; i++)
    {
      CircuitPlayground.setPixelColor(i, 10, 30, 50);
    }

    delay(100);
  }
  // If a round is active and the breadboard button is pressed, end the round and move onto scoring
  if (roundActive && buttonMapped == 1 || bucketFloor >= 10)
  {
    // Clear Pixels
    CircuitPlayground.clearPixels();

    // Incement Round counter
    gameRound++;

    // If the floor of the bucket value is the same as that round's target...
    if(bucketFloor == roundTarget)
    {
      // Increment player score
      score++;
      // Set all pixels to Green
      for (int i = 0; i < 10; i++)
      {
        CircuitPlayground.setPixelColor(i, 10, 50, 10);
      }
    }
    // Else, if the player ended the round with the incorrect level of water in the bucket
    else
    {
      // Set all pixels to Red-
      for (int i = 0; i < 10; i++)
      {
        CircuitPlayground.setPixelColor(i, 50, 10, 10);
      }
    }

    // Print Debug Line
    /*
    Serial.print("Round: ");
    Serial.print(gameRound);
    Serial.print(", Score ");
    Serial.println(score);

    Serial.print("Bucket Floor: ");
    Serial.print(bucketFloor);
    Serial.print(", Bucket Progress: ");
    Serial.print(bucketProgress);
    Serial.print(", RT ");
    Serial.println(roundTarget);
    */

    // One Second Delay between rounds
    delay(1000);
    // Remove roundActive flag
    roundActive = false;

    // Reset Bucket Fill Progress
    bucketFloor = 0;
    bucketProgress = 0;

    // Clear all pixels
    CircuitPlayground.clearPixels();
  }
}


 

No comments:

Post a Comment

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