GROUP 15 – DYNABOX

DynaBox

Escape Room Box Created to leave Users Puzzled

Group 15: Phil, Catalin, Ralu

350mm x 230mm x 90mm

Instruction Manual

The central switches are used to power on a specific puzzle, the top one is for the centre and left hand side, the other for the right hand side

Using the upper switch will power on the puzzle on the far left

To solve this puzzle the user must match the position of the shapes on the reference panel

Once this happens, the puzzle will be complete and the user will be able to play puzzle two

Puzzle two revolves around sequences. The four LEDs on the first stage indicate which button the user must press

After pushing the button – depending on input, the box will send a signal telling the user they are correct or incorrect

If the user solves all randomised sequences correctly, a tune is played and all LEDs flash in pairs

Puzzle three is a music puzzle. It is powered on using the lower switch

Listen to the tune it plays – each note corresponds to a button

The user must figure out what matches

The tune replays after approximately 3 mins

If the user inputs the correct code, a blue light is shown on the 3rd panel and a tune is played

Project Brief

Project 3 Escape Room Puzzle – Interactive Game Design

Escape Rooms require you to solve a series of puzzles within a time limit to escape. Create a puzzle for participants to solve using the skills you’ve learnt. Your puzzle might include a sequence of things to solve, should offer some help if participants get stuck, whilst also rewarding them on its completion. You are asked to design an Arduino based circuit that could be used as part of an escape room.

Cognitive problem solving skills are highly important in everyday life, from carrying out basic to highly complex multi-step tasks. Problem solving games have been extremely popular throughout time hinting to humanities love for creating intricate puzzles and games to test their own logical analysis.

The escape room puzzle requires multiple stages using tactical, visual and auditory elements as well as each separate stage/game when solved unlocks the next stage/game.

After every correct solution the user will receive a visual or auditory reward as well as a global indicator to show total completion.

The puzzle must have little to no instructions and strictly cryptic hints. This increases the puzzle’s difficulty and forces the user into critically thinking their way to solving our puzzle instead of solving it through direct instruction.

Target Market:

The Puzzle Box market is a part of the general puzzle market but differs such that puzzle boxes tend to be complex 3D objects that need to be solved (rather than being put together such as a classical puzzle) and tend to be of a higher difficulty to solve as they often require a mixture of problem solving, cracking, accretive and critical thinking skills to solve them. Puzzle boxes usually serve as a collectible after one is solved due to their unique aesthetic appearance.

Market definition: The Puzzles Market focuses on jigsaw and 3D puzzles, as well as slider puzzles, Rubik’s cubes and other brain teasers.

  • Revenue in the Puzzles segment amounts to US$170.50m in 2022. The market is expected to grow annually by 5.09% (CAGR 2022-2027).
  • In global comparison, most revenue is generated in China (US$1,243.00m in 2022).
  • In relation to total population figures, per person revenues of US$2.49 are generated in 2022.
  • In 2013, UK manufacturers sold approximately 1.77 million puzzles. The figures have increased significantly since, accumulating to an estimated 7.4 million puzzles sold in 2021.
  • In 2021, the UK exported approximately 908.37 million U.S. dollars’ worth of scale models, wheeled toys, puzzles, and dolls to other countries around the world.
  • In 2021, the UK imported nearly three billion U.S. dollars’ worth of scale models, wheeled toys, puzzles, and dolls from other countries.
  • In 2020, imports with a quantity of approximately 254.59 million kilograms were made.
  • In 2018, toys (such as puzzles) with a net weight of 61.51 million kilograms were exported from the UK.

Manufacturer sales of puzzles in the United Kingdom (UK) from 2008 to 2021 (in 1,000 GBP)

This statistic displays the annual sales value of puzzles made by manufacturers in the United Kingdom (UK) from 2008 to 2021. In 2021, UK manufacturers sold an estimated 33.3 million British pounds’ worth of puzzles.

As these statistics show, the puzzle market is healthy and growing fast in the UK. Our team feels that electronic based puzzles would be popular due to the fact that they would bring more opportunity in terms of difficulty levels, a large variety of different and creative puzzles to crack and, most importantly a electronic puzzle can me made so that the solution is different every time meaning that it can be used and solved again and again whilst requiring a different solution every time. 

Project Goals

  • Create a fun and mind racking interactive game that tests the users problem solving skills
  • Create an object that leaves a user perplexed and takes time to decipher
  • Make sure to leave as few hints for the user and make them cryptic to force the user into a critical thinking and mind cracking solving process
  • The puzzle should be hard enough for the user to receive a euphoric and proud feeling when they solve the puzzle 
  • To make the puzzle look aesthetic and follow a certain theme for better game play 
  • To have auditory, visual and tactile features for more in depth game play

Components

  • 9V Battery x2
  • RGB LED x1
  • Green LED x3
  • Blue LED x1
  • Arduino Uno x2
  • Piezo x2 
  • Push Button x8 
  • Large Push Button x4
  • Potentiometer x3
  • DPDT Slide Switch x2
  • Resistor 560Ohms  x8
  • Resistor 2200Ohms x 3
  • Blu Tack 
  • Insulating Tape
  • Zip tie
  • Black Acrylic Sheet 60cm x 60cm 

Tools

  • Band Saw 
  • Hand Saw 
  • Coping Saw 
  • Power Drill 
  • Hot Adhesive
  • Scissors
  • 3D Printer
  • Photoshop

Task List 

PHIL

  • Potentiometer code & circuit 
  • 3D designs & CAD 
  • Gantt Chart 
  • 3D Printing 
  • Engineering Drawings 
  • Project Brief 
  • Project Goals
  • Target Market 
  • Market Research 
  • Design Choices 
  • WordPress Design

CATALIN

  • Colour Led Game & Circuit 
  • Low Fidelity Prototype 
  • Breadboard Layout
  • Flowchart 
  • Cut Acrylic For Larger Box Shell 
  • Box Assembly 
  • Soldering 
  • Combine Circuits
  • Group Meetings Logs 
  • Shortcomings 
  • Sketches 
  • Youtube Videos

RALU

  • Music Puzzle & Circuit 
  • Breadboard layout 
  • Combine Code 
  • WordPress Setup & layout
  • Final Project Assembly 
  • Testing 
  • Debugging 
  • Puzzle Descriptions in WordPress
  • Presentation
  • Timeline 
  • Recycle and Reuse
  • WordPress Layout

Timeline

Week 9 – Main Project Handout, Idea Generation, Find Materials and begin developing item

All – Discuss options and choose project, brainstormed ideas, decide on how the object would function, research the different or extra components which we may need to progress and create our project

Catalin – Produce sketches of product and lo-fi prototype, start to work on circuit, produce flowcharts on programmatically flow,

Week 10 – Create code and Circuits 

Ralu – Create code for Piezo Puzzle Stage 3

Phil – Start to work with 3D Models of products

Week 11 – Assemble Puzzles and Object + Stress Testing 

Ralu – Bug test code and I2C Connection

Catalin – Assembled 3D Printed Parts, Solder Components

All – Connect circuits

Week 12 – Blog Site 

Meetings

19/11/2022

After looking at all of the projects we decided to go with the escape room puzzle box. This option seemed like the most intriguing idea. Our group was enamoured by ways of making a complete puzzle to leave the user perplexed. We aim to create something that a user can spend at least 10 minutes on that is of medium solving difficulty.

Brainstorming:

In order to come up with ideas we look at already existing products on the market to derive the good and leave out the bad from them. This led us to a multistage puzzle box. The idea of having one object was inspired by Chris Ramsey – a youtuber how specialises in puzzle solving videos. Doing market research led us coming to the idea of a multistage object. 

21/11/2022

Deciding on a project led us to solving the first solution on our design journey. The functionality. We decided that it would be a good idea to have individual puzzles, almost like stages compiled together to create what is essentially an escape room as an object. The puzzle would consist of a:

  • Light code stage 
  • Piezo Morse code stage
  • Rotating Potentiometer stage

Aesthetic wise, we would like the object to have a minimalist look, something that doesn’t give away too much but still appeals to the user through mystery.

28/11/2022

The design for the product began. At this stage, many original ideas were altered for usability purposes. For example, the piezo puzzle requires hints otherwise it will be very difficult to solve and understand what they need to do in order to finish the puzzle. While the design for the casing was made the circuits were finalised. 

Stage one – the potentiometer circuit was made using three potentiometers return values, when they are within a specific range, one statement will run signalling to the user that this was complete

Stage two – the light LED puzzle was originally created on a much bigger breadboard, however, due to the size of the previous design, we ended up scaling down to use one half breadboard. It contains a row of 4 different coloured LEDs which light up indicating what the user needs to press in order to solve the puzzle. After this is solved a signal is sent to the final part using I2C. 

Stage three – this stage is put on ‘hold’ constantly awaiting the signal produced from stage two. When it receives this signal the variables and functions are run and this begins the puzzle. This functionality ensures that the three stages have to run in order of stage 1 – 3. If the user tries to access the third stage, it will not respond without stage 1, likewise with stage 2. 

5/12/2022

All sides of the original design were 3D printed and ready to assemble

8/12/2022

At this point both circuits were working independently – this meant it was ready to be tested with the I2C connection. The connection would be created by using the RX and TX ports to send a variable between Stages 1 and 2 and Stage 3 to signal that both have been completed. At this stage it was identified that the circuits were prone to disconnecting and malfunctions due to hardware connections. This was solved by using blue tac, insulating tape and zip ties to keep cables together.

9/12/2022

In order to ensure the buttons securely fit into the breadboard we soldered the ends of the buttons to prevent fraying. After this, each component was inserted into the breadboard and then secured with blue tac.

After the 3D print was produced, it was clear that with the amount of wires present in the circuits would not be able to fit in the box. We solved this issue by expanding box size to a panel with a height of 8cm, length of 35cm and width of 23cm. This made sure to fit components, ensuring there would be enough space for both breadboards and high enough to not disconnect the wires potentially stopping the circuit from working. 

We encountered many issues regarding circuitry which led to a lot of bug fixing after inserting the circuit into the black box. Most of these were solved by double checking wires and components for loose wiring. 

12/12/2022

Finishing design touches added

Design Choices

We have decided to use carbon black PLA plastic as it creates an outstanding visual contrast between the plastic shell and the electronics. Due to this, the buttons and LED’s pop out in contrast and create a sci-fi look which we have pursued throughout the project.

The first stage of our puzzle is the potentiometer puzzle which we have chosen to implement as it has similar characteristics to popular safe cracking puzzles. The hints for which were designed to be simple to understand (i.e. the direction if the dials) however it requires trial and error or as commonly known a brute force technique to solve, just like a safe cracking puzzle would require.

The second stage was chosen to be visual based (unlike the fist stage was tactile), therefore we decided to use large and colourful buttons which corresponded to the same colour LED’s. The LED’s were placed onto the same panel as the first puzzle. As the buttons have their own panel, this is to better visually illustrate the linear continuation and to better link the games together.

The final stage was chosen to be auditory so that the puzzle box covers all the senses. This was also made to be the hardest game so that each game increases with difficulty as it common with similar puzzle boxes. The difficulty was made harder using more cryptic hints, as well as having a wider range of inputs for the solutions. This game was also made to significantly differ from similar games in puzzle boxes to add originality and for harder difficulty.

LED Stage:

For this stage, four led lights up. These indicate which button the user needs to press depending and in what order. After pressing the correct button a new sequence shows up and if this sequence is incorrect an animation is played alongside a tune which indicates to the user that they have entered the incorrect code. When this stage is complete a signal is sent to the piezo stage. 

Piezo Stage:

For this stage, the piezo will make a series of beats with varying tones which are then translated by the user with key inputs through a button. If the user translates the code they heard correctly then they are given a green light and the user can go onto the next quiz. The light will flash in various different colours depending on how close the user is to the solution. After the puzzle is solved the final green led will turn on indicating that the user has completed the puzzle. 

Potentiometer Stage:

This stage is using the potentiometer to map the correct values to preset boundaries. When the rotation value is between the specified boundaries on all 3 the puzzle is solved and one light is displayed. This indicates to the user that they have won this puzzle and can move onto the next part. 

Flowcharts

Finalised Product

Breadboard Diagram

Recycle and Reuse

In order to accommodate for an eco friendly design we used recycled PLA plastic from other models for 3D printed components. The main body of the acrylic box was created from scrap in QMULs Makerspace bin and the button were left aside from an existing project. As none of our components are heavily combined with industrial adhesives it is very easy to take apart and recycles – or simply reuse other components.

Shortcomings

Due to time constraints, though the code for the I2C connection was complete alongside wiring, we were not able to bug test the two Arduino Uno circuits in one. This led to stages 1 + 2 and 3 running independent – instead of linearly. This meant though there are stages, the user is able to complete the hardest stage – stage 3 first.

Another shortcoming was the fact that we were not able to have a cube box with different sides for each game, due to the space and dimensions that the Arduino and bread board take up within the box. We therefore decided to have a design decision of making it a board panel of the 3 games.

We also were not able to have a servo in the box that opens up a token compartment, due to the constrictions of our scheduling and also the space limitation within the box.

Code

//POTENTIOMETER CODE
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 11;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

 

int sensorPinB = A1;    // select the input pin for the potentiometer
int sensorValueB = 0;  // variable to store the value coming from the sensor

 

int sensorPinC = A2;    // select the input pin for the potentiometer
int sensorValueC = 0;  // variable to store the value coming from the

 

int lightPin = A3;  //ldr pin
int lightLVL=0;

 

bool nextGame = false;

 

//CATALIN 4 COLOURED LED CODE

 

int CHOICE_OFF = 0;  //Used to control LEDs

 

int CHOICE_NONE = 0;  //Used to check buttons

 

#define CHOICE_RED (1 << 0) //1  LEFT SHIFT OPPERATOR

 

#define CHOICE_GREEN (1 << 1) //2

 

#define CHOICE_BLUE (1 << 2) // 4

 

#define CHOICE_YELLOW (1 << 3) //8
//LED VARIABLES

 

int LED_RED = 10;

 

int LED_GREEN = 3;

 

int LED_BLUE = 13;

 

int LED_YELLOW = 5;

 

int WinLED1= A4;

 

int WinLED2= A5;
 
// Button pin definitions

 

int BUTTON_RED = 9;

 

int BUTTON_GREEN = 2;

 

int BUTTON_BLUE = 12;

 

int BUTTON_YELLOW = 6;

 

// Buzzer pin definitions

 

int BUZZER1 = 4;

 

int BUZZER2 = 7;

 

int ENDGAME = 1;
// Define game parameters

 

int ROUNDS_TO_WIN = 3;  //Number of rounds to win

 

int ENTRY_TIME_LIMIT = 3000;  //Amount of time to press a button before game times out

 

int MODE_MEMORY = 0;  //resets the game

 

// Game state variables

 

byte gameMode = MODE_MEMORY;  //By default, let’s play the memory game

 

byte gameBoard[32];  //Contains the combination of buttons as we advance

 

byte gameRound = 0;  //Counts the number of succesful rounds the player has made it through

 

void setup()

 

{

 

  ///PHIL SETUP

 

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);

 

  ///

 

 Serial.begin(9600);

 

 
}

 

void loop()

 

{
potentiometercode(); //for phils game
//if phils game is complete initiate catalins game
if (nextGame==true){

 

//initial setup
startsetup ();

 

//initialising the gamemode.
loopingwinnerstart();

 

}

 

//else user continues to try to compleate phils game
else{
  nextGame=false;
}
}

 

// Returns 0 if player loses, or 1 if player wins

 

boolean play_memory(void)

 

{

 

  randomSeed(millis());  // Seed the random generator with random amount of millis()

 

  gameRound = 0;  // Reset the game to the beginning

 

  while (gameRound < ROUNDS_TO_WIN)

 

  {

 

    add_to_moves();  // Add a button to the current moves, then play them back

 

    playMoves();  // Play back the current game board

 

    // Then require the player to repeat the sequence.

 

    for (byte currentMove = 0; currentMove < gameRound; currentMove++)

 

    {

 

      byte choice = wait_for_button();  // See what button the user presses

 

      if (choice == 0) return false;  // If wait timed out, player loses

 

      if (choice != gameBoard[currentMove]) return false;  // If the choice is incorect, player loses
    }

 

    delay(1000);  // Player was correct, delay before playing moves
  }

 

  return true;  // Player made it through all the rounds to win!
}

 

// Plays the current contents of the game moves

 

void playMoves(void)

 

{

 

  for (byte currentMove = 0; currentMove < gameRound; currentMove++)

 

  {

 

    toner(gameBoard[currentMove], 150);

 

    // amount of time between button playback

 

    // shorten this to make game harder

 

    delay(150);  //
  }
}
void startsetup ()
{
  pinMode(BUTTON_RED, INPUT_PULLUP);

 

  pinMode(BUTTON_GREEN, INPUT_PULLUP);

 

  pinMode(BUTTON_BLUE, INPUT_PULLUP);

 

  pinMode(BUTTON_YELLOW, INPUT_PULLUP);

 

  pinMode(LED_RED, OUTPUT);

 

  pinMode(LED_GREEN, OUTPUT);

 

  pinMode(LED_BLUE, OUTPUT);

 

  pinMode(LED_YELLOW, OUTPUT);

 

  pinMode(WinLED1, OUTPUT);

 

  pinMode(WinLED2, OUTPUT);

 

  pinMode(BUZZER1, OUTPUT);

 

  pinMode(BUZZER2, OUTPUT);

 

}
// Adds a new random button to the game sequence, by sampling the timer

 

void add_to_moves(void)

 

{

 

  byte newButton = random(0, 4);
  // convert this number, 0 to 3, to CHOICEs

 

  if (newButton == 0) newButton = CHOICE_RED;

 

  else if (newButton == 1) newButton = CHOICE_GREEN;

 

  else if (newButton == 2) newButton = CHOICE_BLUE;

 

  else if (newButton == 3) newButton = CHOICE_YELLOW;

 

  gameBoard[gameRound++] = newButton;  // Add this new button to the game array
}

 

//LIGHT CONTROL
// Lights a given LEDs

 

// Pass in a byte that is made up from CHOICE_RED, CHOICE_YELLOW,

 

void setLEDs(byte leds)

 

{

 

  if ((leds & CHOICE_RED) != 0)

 

    digitalWrite(LED_RED, LOW);

 

  else

 

    digitalWrite(LED_RED, HIGH);

 

  if ((leds & CHOICE_GREEN) != 0)

 

    digitalWrite(LED_GREEN, LOW);

 

  else

 

    digitalWrite(LED_GREEN, HIGH);

 

  if ((leds & CHOICE_BLUE) != 0)

 

    digitalWrite(LED_BLUE, LOW);

 

  else

 

    digitalWrite(LED_BLUE, HIGH);

 

  if ((leds & CHOICE_YELLOW) != 0)

 

    digitalWrite(LED_YELLOW, LOW);

 

  else

 

    digitalWrite(LED_YELLOW, HIGH);
}
// Wait for a button to be pressed.

 

// Returns one of LED colors (LED_RED, etc.) if successful, 0 if timed out

 

byte wait_for_button(void)

 

{

 

  long startTime = millis();  // Remember the time we started the this loop

 

  while ((millis() – startTime) < ENTRY_TIME_LIMIT)  // Loop until too much time has passed

 

  {

 

    byte button = checkButton();

 

    if (button != CHOICE_NONE)

 

    {

 

      toner(button, 150);  // Play the button the user just pressed

 

      while (checkButton() != CHOICE_NONE)
        ;  //wait for user to release button

 

      delay(10);  //

 

      return button;
    }
  }

 

  return CHOICE_NONE;  // If we get here, we’ve timed out!
}

 

// Returns a ‘1’ bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.

 

byte checkButton(void)

 

{

 

  if (digitalRead(BUTTON_RED) == 0) return (CHOICE_RED);

 

  else if (digitalRead(BUTTON_GREEN) == 0) return (CHOICE_GREEN);

 

  else if (digitalRead(BUTTON_BLUE) == 0) return (CHOICE_BLUE);

 

  else if (digitalRead(BUTTON_YELLOW) == 0) return (CHOICE_YELLOW);

 

  return (CHOICE_NONE);  // If no button is pressed, return none
}

 

// Light an LED and play tone

 

// Red, upper left:     440Hz

 

// Green, upper right:  880Hz

 

// Blue, lower left:    587HZ

 

// Yellow, lower right: 784Hz

 

void toner(byte which, int buzz_length_ms)

 

{

 

  setLEDs(which);  //Turn on a given LED

 

  //Play the sound associated with the given LED

 

  switch (which)

 

  {

 

    case CHOICE_RED:

 

      buzz_sound(buzz_length_ms, 1136);

 

      break;

 

    case CHOICE_GREEN:

 

      buzz_sound(buzz_length_ms, 568);

 

      break;

 

    case CHOICE_BLUE:

 

      buzz_sound(buzz_length_ms, 851);

 

      break;

 

    case CHOICE_YELLOW:

 

      buzz_sound(buzz_length_ms, 638);

 

      break;
  }

 

  setLEDs(CHOICE_OFF);  // Turn off all LEDs
}

 

// Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.

 

void buzz_sound(int buzz_length_ms, int buzz_delay_us)

 

{

 

  // Convert total play time from milliseconds to microseconds

 

  long buzz_length_us = buzz_length_ms * (long)1000;

 

  // Loop until the remaining play time is less than a single buzz_delay_us

 

  while (buzz_length_us > (buzz_delay_us * 2))

 

  {

 

    buzz_length_us -= buzz_delay_us * 2;  //Decrease the remaining play time

 

    // Toggle the buzzer at various speeds

 

    digitalWrite(BUZZER1, LOW);

 

    digitalWrite(BUZZER2, HIGH);

 

    delayMicroseconds(buzz_delay_us);

 

    digitalWrite(BUZZER1, HIGH);

 

    digitalWrite(BUZZER2, LOW);

 

    delayMicroseconds(buzz_delay_us);
  }
}
void loopingwinnerstart(){

 

  setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW);  // Turn all LEDs on

 

  delay(1000);

 

  setLEDs(CHOICE_OFF);  // Turn off LEDs

 

 

 

  delay(250);

 

  if (gameMode == MODE_MEMORY)

 

  {

 

    // Play memory game

 

    if (play_memory() == true)

 

      play_winner();  // Player won, play winner tones

 

    else

 

      play_loser();  // Player lost, play loser tones
  }
}
// Play the winner sound and lights

 

void play_winner(void)

 

{

 

  analogWrite(WinLED2, 0);

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  delay(100);

 

  Serial.println(” You Won “);

 

  delay(100);

 

  setLEDs(CHOICE_OFF);
 
  delay(1000);
 
  Serial.println(“FINISHED”);

 

 

 

 

 

  analogWrite(WinLED2, 255);

 

  analogWrite(ledPin, 255);

 

 

 

  exit(1);

 

}

 

// Play the winner sound

 

void winner_sound(void)

 

{

 

  // Toggle the buzzer at various speeds

 

  for (byte x = 250; x > 70; x–)

 

  {

 

    for (byte y = 0; y < 3; y++)

 

    {

 

      digitalWrite(BUZZER2, HIGH);

 

      digitalWrite(BUZZER1, LOW);

 

      delayMicroseconds(x);

 

      digitalWrite(BUZZER2, LOW);

 

      digitalWrite(BUZZER1, HIGH);

 

      delayMicroseconds(x);
    }
  }
}

 

// Play the loser sound/lights

 

void play_loser(void)

 

{

 

  setLEDs(CHOICE_RED | CHOICE_GREEN);

 

  buzz_sound(255, 1500);

 

  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);

 

  buzz_sound(255, 1500);

 

  setLEDs(CHOICE_RED | CHOICE_GREEN);

 

  buzz_sound(255, 1500);

 

  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);

 

  buzz_sound(255, 1500);

 

  delay(100);

 

  Serial.println(gameRound + ” You Lost “);

 

  delay(100);

 

  analogWrite(WinLED2, 0);

 

 
}

 

////phil

 

void potentiometercode(){

 

  analogWrite(WinLED1, 0);
  int lightLevel = analogRead(lightPin);  //ldr read light imput and give out output as lightLevel
  lightLevel =map(lightLevel,0,50,0,1023);
 
  //Serial.println(lightLevel);

 

 
 
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin); //MIDDLE
  Serial.println(sensorValue);
  sensorValueB = analogRead(sensorPinB); //LEFT
  Serial.println(sensorValueB);
  sensorValueC = analogRead(sensorPinC); //RIGHT
  Serial.println(sensorValueC);
 
 // Serial.println(lightLevel);

 

   //&& lightLVL==255
   //commmented out lightLVL in analog
//when all potentiometers facing correct direction and ldr covered game is solved
  if (sensorValue>-1 && sensorValue<10 && sensorValueB>1000 && sensorValueB<1024 && sensorValueC>1000 && sensorValueC<1024  ){

 

    analogWrite(ledPin, 255); //dependant on ldr  indicates soution

 

    nextGame = true; //when solved to initiate catalins game

 

    analogWrite(WinLED1, 255);

 

    }
  else{
    analogWrite(ledPin, LOW);
    analogWrite(WinLED1, 0);
    nextGame = false;
   

 

  }
}

 

////PHIL POTENTIOMETER CODE
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 11;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

 

int sensorPinB = A1;    // select the input pin for the potentiometer
int sensorValueB = 0;  // variable to store the value coming from the sensor

 

int sensorPinC = A2;    // select the input pin for the potentiometer
int sensorValueC = 0;  // variable to store the value coming from the

 

int lightPin = A3;  //ldr pin
int lightLVL=0;

 

bool nextGame = false;

 

//CATALIN 4 COLOURED LED CODE

 

int CHOICE_OFF = 0;  //Used to control LEDs

 

int CHOICE_NONE = 0;  //Used to check buttons

 

#define CHOICE_RED (1 << 0) //1  LEFT SHIFT OPPERATOR

 

#define CHOICE_GREEN (1 << 1) //2

 

#define CHOICE_BLUE (1 << 2) // 4

 

#define CHOICE_YELLOW (1 << 3) //8
//LED VARIABLES

 

int LED_RED = 10;

 

int LED_GREEN = 3;

 

int LED_BLUE = 13;

 

int LED_YELLOW = 5;

 

int WinLED1= A4;

 

int WinLED2= A5;
 
// Button pin definitions

 

int BUTTON_RED = 9;

 

int BUTTON_GREEN = 2;

 

int BUTTON_BLUE = 12;

 

int BUTTON_YELLOW = 6;

 

// Buzzer pin definitions

 

int BUZZER1 = 4;

 

int BUZZER2 = 7;

 

int ENDGAME = 1;
// Define game parameters

 

int ROUNDS_TO_WIN = 3;  //Number of rounds to win

 

int ENTRY_TIME_LIMIT = 3000;  //Amount of time to press a button before game times out

 

int MODE_MEMORY = 0;  //resets the game

 

// Game state variables

 

byte gameMode = MODE_MEMORY;  //By default, let’s play the memory game

 

byte gameBoard[32];  //Contains the combination of buttons as we advance

 

byte gameRound = 0;  //Counts the number of succesful rounds the player has made it through

 

void setup()

 

{

 

  ///PHIL SETUP

 

  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);

 

  ///

 

 Serial.begin(9600);

 

 
}

 

void loop()

 

{
potentiometercode(); //for phils game
//if phils game is complete initiate catalins game
if (nextGame==true){

 

//initial setup
startsetup ();

 

//initialising the gamemode.
loopingwinnerstart();

 

}

 

//else user continues to try to compleate phils game
else{
  nextGame=false;
}
}

 

// Returns 0 if player loses, or 1 if player wins

 

boolean play_memory(void)

 

{

 

  randomSeed(millis());  // Seed the random generator with random amount of millis()

 

  gameRound = 0;  // Reset the game to the beginning

 

  while (gameRound < ROUNDS_TO_WIN)

 

  {

 

    add_to_moves();  // Add a button to the current moves, then play them back

 

    playMoves();  // Play back the current game board

 

    // Then require the player to repeat the sequence.

 

    for (byte currentMove = 0; currentMove < gameRound; currentMove++)

 

    {

 

      byte choice = wait_for_button();  // See what button the user presses

 

      if (choice == 0) return false;  // If wait timed out, player loses

 

      if (choice != gameBoard[currentMove]) return false;  // If the choice is incorect, player loses
    }

 

    delay(1000);  // Player was correct, delay before playing moves
  }

 

  return true;  // Player made it through all the rounds to win!
}

 

// Plays the current contents of the game moves

 

void playMoves(void)

 

{

 

  for (byte currentMove = 0; currentMove < gameRound; currentMove++)

 

  {

 

    toner(gameBoard[currentMove], 150);

 

    // amount of time between button playback

 

    // shorten this to make game harder

 

    delay(150);  //
  }
}
void startsetup ()
{
  pinMode(BUTTON_RED, INPUT_PULLUP);

 

  pinMode(BUTTON_GREEN, INPUT_PULLUP);

 

  pinMode(BUTTON_BLUE, INPUT_PULLUP);

 

  pinMode(BUTTON_YELLOW, INPUT_PULLUP);

 

  pinMode(LED_RED, OUTPUT);

 

  pinMode(LED_GREEN, OUTPUT);

 

  pinMode(LED_BLUE, OUTPUT);

 

  pinMode(LED_YELLOW, OUTPUT);

 

  pinMode(WinLED1, OUTPUT);

 

  pinMode(WinLED2, OUTPUT);

 

  pinMode(BUZZER1, OUTPUT);

 

  pinMode(BUZZER2, OUTPUT);

 

}
// Adds a new random button to the game sequence, by sampling the timer

 

void add_to_moves(void)

 

{

 

  byte newButton = random(0, 4);
  // convert this number, 0 to 3, to CHOICEs

 

  if (newButton == 0) newButton = CHOICE_RED;

 

  else if (newButton == 1) newButton = CHOICE_GREEN;

 

  else if (newButton == 2) newButton = CHOICE_BLUE;

 

  else if (newButton == 3) newButton = CHOICE_YELLOW;

 

  gameBoard[gameRound++] = newButton;  // Add this new button to the game array
}

 

//LIGHT CONTROL
// Lights a given LEDs

 

// Pass in a byte that is made up from CHOICE_RED, CHOICE_YELLOW,

 

void setLEDs(byte leds)

 

{

 

  if ((leds & CHOICE_RED) != 0)

 

    digitalWrite(LED_RED, LOW);

 

  else

 

    digitalWrite(LED_RED, HIGH);

 

  if ((leds & CHOICE_GREEN) != 0)

 

    digitalWrite(LED_GREEN, LOW);

 

  else

 

    digitalWrite(LED_GREEN, HIGH);

 

  if ((leds & CHOICE_BLUE) != 0)

 

    digitalWrite(LED_BLUE, LOW);

 

  else

 

    digitalWrite(LED_BLUE, HIGH);

 

  if ((leds & CHOICE_YELLOW) != 0)

 

    digitalWrite(LED_YELLOW, LOW);

 

  else

 

    digitalWrite(LED_YELLOW, HIGH);
}
// Wait for a button to be pressed.

 

// Returns one of LED colors (LED_RED, etc.) if successful, 0 if timed out

 

byte wait_for_button(void)

 

{

 

  long startTime = millis();  // Remember the time we started the this loop

 

  while ((millis() – startTime) < ENTRY_TIME_LIMIT)  // Loop until too much time has passed

 

  {

 

    byte button = checkButton();

 

    if (button != CHOICE_NONE)

 

    {

 

      toner(button, 150);  // Play the button the user just pressed

 

      while (checkButton() != CHOICE_NONE)
        ;  //wait for user to release button

 

      delay(10);  //

 

      return button;
    }
  }

 

  return CHOICE_NONE;  // If we get here, we’ve timed out!
}

 

// Returns a ‘1’ bit in the position corresponding to CHOICE_RED, CHOICE_GREEN, etc.

 

byte checkButton(void)

 

{

 

  if (digitalRead(BUTTON_RED) == 0) return (CHOICE_RED);

 

  else if (digitalRead(BUTTON_GREEN) == 0) return (CHOICE_GREEN);

 

  else if (digitalRead(BUTTON_BLUE) == 0) return (CHOICE_BLUE);

 

  else if (digitalRead(BUTTON_YELLOW) == 0) return (CHOICE_YELLOW);

 

  return (CHOICE_NONE);  // If no button is pressed, return none
}

 

// Light an LED and play tone

 

// Red, upper left:     440Hz

 

// Green, upper right:  880Hz

 

// Blue, lower left:    587HZ

 

// Yellow, lower right: 784Hz

 

void toner(byte which, int buzz_length_ms)

 

{

 

  setLEDs(which);  //Turn on a given LED

 

  //Play the sound associated with the given LED

 

  switch (which)

 

  {

 

    case CHOICE_RED:

 

      buzz_sound(buzz_length_ms, 1136);

 

      break;

 

    case CHOICE_GREEN:

 

      buzz_sound(buzz_length_ms, 568);

 

      break;

 

    case CHOICE_BLUE:

 

      buzz_sound(buzz_length_ms, 851);

 

      break;

 

    case CHOICE_YELLOW:

 

      buzz_sound(buzz_length_ms, 638);

 

      break;
  }

 

  setLEDs(CHOICE_OFF);  // Turn off all LEDs
}

 

// Toggle buzzer every buzz_delay_us, for a duration of buzz_length_ms.

 

void buzz_sound(int buzz_length_ms, int buzz_delay_us)

 

{

 

  // Convert total play time from milliseconds to microseconds

 

  long buzz_length_us = buzz_length_ms * (long)1000;

 

  // Loop until the remaining play time is less than a single buzz_delay_us

 

  while (buzz_length_us > (buzz_delay_us * 2))

 

  {

 

    buzz_length_us -= buzz_delay_us * 2;  //Decrease the remaining play time

 

    // Toggle the buzzer at various speeds

 

    digitalWrite(BUZZER1, LOW);

 

    digitalWrite(BUZZER2, HIGH);

 

    delayMicroseconds(buzz_delay_us);

 

    digitalWrite(BUZZER1, HIGH);

 

    digitalWrite(BUZZER2, LOW);

 

    delayMicroseconds(buzz_delay_us);
  }
}
void loopingwinnerstart(){

 

  setLEDs(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW);  // Turn all LEDs on

 

  delay(1000);

 

  setLEDs(CHOICE_OFF);  // Turn off LEDs

 

 

 

  delay(250);

 

  if (gameMode == MODE_MEMORY)

 

  {

 

    // Play memory game

 

    if (play_memory() == true)

 

      play_winner();  // Player won, play winner tones

 

    else

 

      play_loser();  // Player lost, play loser tones
  }
}
// Play the winner sound and lights

 

void play_winner(void)

 

{

 

  analogWrite(WinLED2, 0);

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  setLEDs(CHOICE_GREEN | CHOICE_BLUE);

 

  winner_sound();

 

  setLEDs(CHOICE_RED | CHOICE_YELLOW);

 

  winner_sound();

 

  delay(100);

 

  Serial.println(” You Won “);

 

  delay(100);

 

  setLEDs(CHOICE_OFF);
 
  delay(1000);
 
  Serial.println(“FINISHED”);

 

 

 

 

 

  analogWrite(WinLED2, 255);

 

  analogWrite(ledPin, 255);

 

 

 

  exit(1);

 

}

 

// Play the winner sound

 

void winner_sound(void)

 

{

 

  // Toggle the buzzer at various speeds

 

  for (byte x = 250; x > 70; x–)

 

  {

 

    for (byte y = 0; y < 3; y++)

 

    {

 

      digitalWrite(BUZZER2, HIGH);

 

      digitalWrite(BUZZER1, LOW);

 

      delayMicroseconds(x);

 

      digitalWrite(BUZZER2, LOW);

 

      digitalWrite(BUZZER1, HIGH);

 

      delayMicroseconds(x);
    }
  }
}

 

// Play the loser sound/lights

 

void play_loser(void)

 

{

 

  setLEDs(CHOICE_RED | CHOICE_GREEN);

 

  buzz_sound(255, 1500);

 

  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);

 

  buzz_sound(255, 1500);

 

  setLEDs(CHOICE_RED | CHOICE_GREEN);

 

  buzz_sound(255, 1500);

 

  setLEDs(CHOICE_BLUE | CHOICE_YELLOW);

 

  buzz_sound(255, 1500);

 

  delay(100);

 

  Serial.println(gameRound + ” You Lost “);

 

  delay(100);

 

  analogWrite(WinLED2, 0);

 

 
}

 

////phil

 

void potentiometercode(){

 

  analogWrite(WinLED1, 0);
  int lightLevel = analogRead(lightPin);  //ldr read light imput and give out output as lightLevel
  lightLevel =map(lightLevel,0,50,0,1023);
 
  //Serial.println(lightLevel);

 

 
 
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin); //MIDDLE
  Serial.println(sensorValue);
  sensorValueB = analogRead(sensorPinB); //LEFT
  Serial.println(sensorValueB);
  sensorValueC = analogRead(sensorPinC); //RIGHT
  Serial.println(sensorValueC);
 
 // Serial.println(lightLevel);

 

   //&& lightLVL==255
   //commmented out lightLVL in analog
//when all potentiometers facing correct direction and ldr covered game is solved
  if (sensorValue>-1 && sensorValue<10 && sensorValueB>1000 && sensorValueB<1024 && sensorValueC>1000 && sensorValueC<1024  ){

 

    analogWrite(ledPin, 255); //dependant on ldr  indicates soution

 

    nextGame = true; //when solved to initiate catalins game

 

    analogWrite(WinLED1, 255);

 

    }
  else{
    analogWrite(ledPin, LOW);
    analogWrite(WinLED1, 0);
    nextGame = false;
   

 

  }
}

Stage 3

const int MAX = 8;

const int buttonPin = 2;

const int buttonPin2 = 3;

const int buttonPin3 = 4;

const int buttonPin4 = 5;

const int buttonPin5 = 6;

const int buttonPin6 = 7;

const int buttonPin7 = 8;

const int buttonPin8 = 12;

const int completeLED = A5;

bool btnState = false;

bool btnState2 = false;

bool btnState3 = false;

bool btnState4 = false;

bool btnState5 = false;

bool btnState6 = false;

bool btnState7 = false;

bool btnState8 = false;

//pins for RGB LED

int redPin = 9;

int greenPin = 10;

int bluePin = 11;

int buttonState = 0;

int choice;

int speakerPin = 13;

int tempo = 700;

String userTune = “”;

String replaceval = “a”;

String tune;

//for timer

int incrementor1 = 0;

int incrementor2 = 0;

//generate random array of letters

const int len = 3;

char song[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘ f’, ‘g’, ‘h’ };

//pick random value from array

const byte songLength = sizeof(song) / sizeof(song[0]);

char notes[len + 1];  //allow 1 extra for the NULL

//things for random no gen

int NumberArray[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

int n = 0;

int testVal = 0;

//I2C connection

bool readyToStart = false;

char mystr[10]; //Initialized variable to store recieved data

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  //Code output

  pinMode(speakerPin, OUTPUT);

  pinMode(completeLED, OUTPUT);

  //button inputs

  pinMode(buttonPin, INPUT);

  pinMode(buttonPin2, INPUT);

  pinMode(buttonPin3, INPUT);

  pinMode(buttonPin4, INPUT);

  pinMode(buttonPin5, INPUT);

  pinMode(buttonPin6, INPUT);

  pinMode(buttonPin7, INPUT);

  pinMode(buttonPin8, INPUT);

  //RGB LED

  pinMode(redPin, OUTPUT);

  pinMode(greenPin, OUTPUT);

  pinMode(bluePin, OUTPUT);

}

void loop() {

  // put your main code here, to run repeatedly:

  //I2C Connection

  // Serial.readBytes(mystr,5); // Constantly read the serial data and store in var

  // Serial.println(mystr); //Print data on Serial Monitor

  // if (mystr == “Ready”){

  // Serial.println(“Set”);

  // readyToStart = true;

  // delay(100);

  // }

  // if (readyToStart){

  //   //start program

  //   start();

  //   delay(1000);

  //   readyToStart = false;

  // } else {

  //   //dodcf

  //   readyToStart = false;

  //   Serial.println(“false”);

  // }

  // delay(1000);

  //test

  if (testVal == 0){

    start();

    // winTune();

    delay(1000);

    testVal++;

  }

  buttonPress();

  timer(NumberArray, n);

  digitalWrite(completeLED, LOW);

  if (btnState) {

    RGB_color(255, 255, 125);  // ultramarine

    delay(100);

    userTune += “a”;

    btnState = false;

    delay(100);

  }

  if (btnState2) {

    RGB_color(255, 125, 0);  // turquiose

    delay(100);

    userTune += “b”;

    btnState2 = false;

    delay(100);

  }

  if (btnState3) {

    RGB_color(125, 255, 0);  // bright turquiose

    delay(100);

    userTune += “c”;

    btnState3 = false;

    delay(100);

  }

  if (btnState4) {

    RGB_color(125, 0, 255);  //olive green

    delay(100);

    userTune += “d”;

    btnState4 = false;

    delay(100);

  }

  if (btnState5) {

    RGB_color(255, 0, 255);  // forest green

    delay(100);

    userTune += “e”;

    btnState5 = false;

    delay(100);

  }

  if (btnState6) {

    RGB_color(125, 125, 255);  // yellow green

    delay(100);

    userTune += “f”;

    btnState6 = false;

    delay(100);

  }

  if (btnState7) {

    RGB_color(0, 125, 255);  //greenish

    delay(100);

    userTune += “g”;

    btnState7 = false;

    delay(100);

  }

  if (btnState8) {

    RGB_color(125, 0, 125);  //bluish

    delay(100);

    userTune += “h”;

    btnState8 = false;

    delay(100);

  }

  //check if users answer is correct

  if (userTune.length() >= 3) {

    Serial.println(userTune);

    if (userTune == notes) {

      Serial.println(“You win”);

      RGB_color(255, 0, 0);  // Cyan

      digitalWrite(completeLED, HIGH);

      winTune();

      delay(1000);

      exit(1);

    } else if (userTune != notes) {

      RGB_color(255, 0, 0);  // Red

      userTune = “”;

      digitalWrite(completeLED, HIGH);

      delay(10);

      digitalWrite(completeLED, HIGH);

      delay(10);

      Serial.println(“Incorrect”);

      loseTune();

      delay(1000);

    }

  }

}

void start(){

  int n = sizeof(NumberArray) / sizeof(NumberArray[0]);  //number of elements

  Serial.println(“running”);

  //shuffle array for random beats using bubble sort algorithm

  beatGen(n, NumberArray);

  //sound output

  codeGeneration();

  // replaceval = replaceval + notes;

  notes[0] = ‘a’;

  Serial.println(notes);

  codeOutput(notes, NumberArray, n);

}

void RGB_color(int redValue, int greenValue, int blueValue) {

  analogWrite(redPin, redValue);

  analogWrite(greenPin, greenValue);

  analogWrite(bluePin, blueValue);

}

bool buttonPress() {

  //when button pressed, log it

  if ((digitalRead(buttonPin) == LOW) && (!btnState)) {

    Serial.println(“1 button has been clicked”);

    btnState = true;

    delay(200);

  }

  if ((digitalRead(buttonPin2) == LOW) && (!btnState2)) {

    delay(200);

    Serial.println(“20 button has been clicked”);

    btnState2 = true;

    delay(200);

  }

  if ((digitalRead(buttonPin3) == LOW) && (!btnState3)) {

    delay(200);

    Serial.println(“30 button has been clicked”);

    btnState3 = true;

    delay(200);

  }

  if ((digitalRead(buttonPin4) == LOW) && (!btnState4)) {

    delay(200);

    Serial.println(“4 button has been clicked”);

    btnState4 = true;

    delay(200);

  }

  if ((digitalRead(buttonPin5) == LOW) && (!btnState5)) {

    delay(200);

    Serial.println(“5 button has been clicked”);

    btnState5 = true;

    delay(200);

  }

  if ((digitalRead(buttonPin6) == LOW) && (!btnState6)) {

    delay(200);

    Serial.println(“6 button has been clicked”);

    btnState6 = true;

    delay(200);

  }

  if ((digitalRead(buttonPin7) == LOW) && (!btnState7)) {

    delay(200);

    Serial.println(“7 button has been clicked”);

    btnState7 = true;

    delay(200);

  }

  if ((digitalRead(buttonPin8) == LOW) && (!btnState8)) {

    delay(200);

    Serial.println(“8 Button has been pressed”);

    btnState8 = true;

    delay(200);

  }

}

void playTone(int tone, int duration) {

  //loop through each tone to play it for the specified duration

  for (long i = 0; i < duration * 1000L; i += tone * 2) {

    digitalWrite(speakerPin, HIGH);

    delayMicroseconds(tone);

    digitalWrite(speakerPin, LOW);

    delayMicroseconds(tone);

  }

}

void playNote(char note, int duration) {

  char names[] = { ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘g’ };

  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name

  for (int i = 0; i < sizeof(names); i++) {

    if (names[i] == note) {

      playTone(tones[i], duration);

    }

  }

}

//generate random code – this is what the uesr needs to enter to complete puzzle

String codeGeneration() {

  for (int n = 0; n < len; n++) {

    notes[n] = song[random(songLength)];

    notes[n + 1] = ‘\0’;

    Serial.println(notes);  // ————————–

  }

}

void beatGen(int lengthOfArray, int nArray[]) {

  for (int i = 0; i < lengthOfArray; i++) {

    int j = random(lengthOfArray – i);

    int t = nArray[i];

    nArray[i] = nArray[j];

    nArray[j] = t;

  }

  // result

  for (int i = 0; i < lengthOfArray; i++) Serial.println(nArray[i]);  // ————————

  randomSeed(nArray);

}

void codeOutput(char c[], int beatsTune[], int arrayLength) {

  String val = c;

  Serial.println(“Music Output”);

  for (int i = 0; i < arrayLength; i++) {

    //if the value entered does not match, then delay it

    if (val[i] == ‘ ‘) {

      delay(beatsTune[i] * tempo);

    } else {

      playNote(val[i], beatsTune[i] * tempo);

    }

    // pause between notes

    delay(tempo / 2);

  }

  delay(100);

}

void replay(char c[], int arrayLength) {

  String val = c;

  for (int i = 0; i < arrayLength; i++) {

    //if the value entered does not match, then delay it

    if (val[i] == ‘ ‘) {

      delay(NumberArray[i] * tempo);

    } else {

      playNote(val[i], NumberArray[i] * tempo);

    }

    // pause between notes

    delay(tempo / 2);

  }

  delay(100);

}

//Amount of time to press a button before game times out

void timer(int beatsTune[], int arrayLength) {

  incrementor1++;

  //log when any button is pressed

  if (

    btnState || btnState2 || btnState3 || btnState4 || btnState5 || btnState6 || btnState7 || btnState8) {

    incrementor2 = incrementor1;

    Serial.println(“user pressed”);

  }

  if (incrementor1 > (incrementor2 + 5000)) {

    //play tune for user again if they havent pressed anything for sometime

    Serial.println(“Timed out”);

    // Serial.println(arrayLength);

    replay(notes, 10);

    delay(1000);

    //reset timer

    incrementor1 = incrementor2;

    delay(400);

  } else {

  };  //do nothing

  Serial.println(incrementor1);

  Serial.println(incrementor2);

}

void winTune(){

    //array of characters to be played for tune

    tempo = 100;

    String val = “ccccdddeeffgg”;

    int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };

  //iterate through all characters in val and play each one for duration specified

    for (int i = 0; i < sizeof(beats); i++) {

      //if the value entered does not match, then delay it

      if (val[i] == ‘ ‘) {

        delay(beats[i] * tempo);

      } else {

        playNote(val[i], beats[i] * tempo);

      }

      // pause between notes

      delay(tempo / 2);

    }

  delay(500);

}

void loseTune(){

 //array of characters to be played for tune

  String val = “ge”;

  int missBeat[] = {1, 2};

  //iterate through all characters in val and play each one for duration specified

    for (int i = 0; i < sizeof(missBeat); i++) {

      //if the value entered does not match, then delay it

      if (val[i] == ‘ ‘) {

        delay(missBeat[i] * tempo);

      } else {

        playNote(val[i], missBeat[i] * tempo);

      }

      // pause between notes

      delay(tempo / 2);

    }

  delay(500);

}