01 March 2026

Scaffolding: Mapping - NeoMemory

We created a small interactive game using the Adafruit Circuit Playground that combines a button, a potentiometer, a light sensor, and NeoPixels. The game starts when the player presses a button connected to A3. Once the game begins, a short animation plays where one NeoPixel lights up red and then turns off. This red light represents the target position the player needs to remember. The player uses a potentiometer as a controller (A2). We map the potentiometer’s analog value to numbers between 0 and 9. Each number corresponds to one of the NeoPixels, and the selected pixel lights up blue to show the player’s current choice. This gives clear visual feedback while turning the knob. When the player thinks their selected position matches the red target, they press the light sensor to confirm their answer. If the selected position is correct, the player moves on to the next round. If it is wrong, the game immediately resets. Each time the player gets a correct answer, the animation becomes longer, adding one more red light to remember. The player must correctly match the pattern four times in a row to win. If the player wins, all the NeoPixels light up green. If they lose, all the lights turn red before the game restarts.







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

//Brody Townsend
//Mathieu Castonguay

//Potentiometer
int potPin = A2; //Must use A2 as said on Canvas.
int potValue = 0; //Analog value (0-1023)
int ledCount = 0; //The LED the player is selecting.

//Memory Variables
int howManyColor = 1; //How many LEDS are in sequence.
unsigned long previousTime = 0;
int animIndex = 0;
bool showingColor = false;
int randomArray[20]; //Stores 20 random LED positions
int indexs = 0; //Tracks progress

//Game State
bool RandomizeColor; //True when sequence finished showing
bool gameStart; //True when game is running
bool correct; //True when round is finished.

//Light sensor trigger
bool lightTriggered = false; //To help prevent repeated light activation


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

    //First Random sequence
      for(int i = 0; i < howManyColor; i++) {
      randomArray[i] = random(0, 10);
      Serial.println(randomArray[i]);
    }
}


void loop() {
  // put your main code here, to run repeatedly:

   ButtonPress(); //Checks if button has been pressed.

if(gameStart) //Runs logic if game has started
{
  RandomColor();
 
  if (RandomizeColor) {
      MoveColorLight();
      CheckLight();
    }
}
}


//Random Color sequence display.
void RandomColor()//change it instead of delay using millis
{
  if (!RandomizeColor)
  {// If we still have colors left to show
    if (animIndex < howManyColor)
    {// Get current time
      unsigned long currentTime = millis();
       // If we are NOT currently showing a color
      if (!showingColor)
      {
         // Get the LED index from the random sequence
        int r = randomArray[animIndex];
        CircuitPlayground.setPixelColor(r, 255, 0, 0);
        // Save the time when we turned it on
        previousTime = currentTime;
        // Mark that we are now showing a color
        showingColor = true;
      }
      //check if 1 second has passed
      else if (currentTime - previousTime > 1000)
      {
        CircuitPlayground.clearPixels();
        previousTime = currentTime;
        animIndex++;
        showingColor = false;
      }
    }//loop ends
    else
    {
      RandomizeColor = true;
      animIndex = 0;
    }
  }
}

//Moves player LED using Potentiometer
void MoveColorLight()
{
  if(!correct){
      potValue = analogRead(potPin);   // 0 - 1023

  // Convert 0-1023 into 0-10
  //Put as 1010, as high value on potentiometer was not showing last Neopixel, and was quickly jumping between two different pixels.
  ledCount = map(potValue, 0, 1010, 0, 9);

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


 // for (int i = 0; i < ledCount; i++) {
    CircuitPlayground.setPixelColor(ledCount, 0, 0, 255); // Blue color
//  }

  delay(50);
  }
}


//Checks light sensor for player confirmation
void CheckLight() {

  int lightValue = CircuitPlayground.lightSensor();

  if (lightValue < 10 && !lightTriggered) { //If its very dark, and not already triggered.
   
    delay(1000); //Simple delay to give player time to confirm selection and remove hand.
    lightTriggered = true;


    if (ledCount == randomArray[indexs]) { //Checks if player chose correct LED
      Serial.println("Correct!");
      indexs++;

      if(indexs == howManyColor) {
        correct = true;
        CircuitPlayground.clearPixels();
        CircuitPlayground.setPixelColor(ledCount, 0, 255, 0);
        howManyColor++;
        indexs = 0;

        if(howManyColor == 5){
          Win();
        } else {
          Finish();
        }
      }
    }
    else {
      Serial.println("Wrong!");
      correct = true;
      CircuitPlayground.clearPixels();
      CircuitPlayground.setPixelColor(ledCount, 255, 0, 0);
      howManyColor = 1;
      Lose();
    }
  }

  // Reset trigger when light goes back Up
  if (lightValue > 50) {
    lightTriggered = false;
  }
}


void Finish(){
    delay(1000);
    //set everything back when finish to keep the loop
    RandomizeColor = false;
    correct = false;
    animIndex = 0;
    showingColor = false;

    // Generate new random sequence(no more delay in loop need it here)
    for(int i = 0; i < howManyColor; i++) {
      randomArray[i] = random(0, 10);
      Serial.println(randomArray[i]);
    }
    CircuitPlayground.clearPixels();
}

void Win()
{
 
  for (int i = 0; i < 10; i++) {
  CircuitPlayground.setPixelColor(i, 0, 255, 0);
  }
  howManyColor = 1;
  Finish();

}

void Lose()
{
  for (int i = 0; i < 10; i++) {
  CircuitPlayground.setPixelColor(i, 255, 0, 0);
  }
  Finish();

}

void ButtonPress()//whenever button press check its value then start or close game
{
    int buttonState = digitalRead(10);
     if(buttonState == 1){
      if(gameStart)
      {
      gameStart = false;
      howManyColor = 1;
      Finish();
      delay(100);
      }else
      {
        gameStart = true;
        delay(300);
      }
     }
}





No comments:

Post a Comment

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