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