0

sound effect player connecting to arduino mega
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

sound effect player connecting to arduino mega

by roadshark on Sat Oct 19, 2019 5:42 am

Hi Bill,
Hope the cycling is going well (Winter Woolies for you guys) and you are in good health:)
I am trying to get an arduino mega to operate an Adafruit Audio FX Sound Board + 2x2W Amp - WAV/OGG Trigger -16MB PID: 2217.
When a sensor pin is activated I want to play a sound effect just once. I know how to do it just using a switch without a micro controller but connecting to a pin on the mega is a little bit perplexing :( No smoke monster yet!
I have wired the gnd on the trigger pin side of the FX board to the Gnd pin on the Mega next to pin 52.
I have wired the Gnd pin on the other side of the board to the Gnd pin on the mega (next to 5Volt) and the Vin pin to the 5 volt pin on the Mega.
I have name the file T00.wav.
I remember in a previous conversation that you mentioned having to make this pin LOW????
Code: Select all | TOGGLE FULL SIZE
 for (int thisPin = 0; thisPin < sensorPinsCount; thisPin++) {
    pinMode(sensorPins[thisPin], INPUT_PULLUP);
  }
  //Sets the sound effect activation pins to output.
  for (int thisPin = 0; thisPin  < soundEffectActivationPinCount; thisPin++) {
    pinMode(soundEffectActivationPin[thisPin], OUTPUT);
  }

The documentation said that I needed to Trigger the pin for at least 150 milliseconds.
Here is the code I am attempting to use. I think the problem is that I am writing the pin high that is connected to 0 on the fx board to trigger the sound.
Code: Select all | TOGGLE FULL SIZE
if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {//balls passes a breakbeam sensor
          while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
          gameScore += 100;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();
          digitalWrite ((43), HIGH);//Activate Pin 0 on SFX player to trigger sound effect
          delay (150);//Give time for message to get to SFX player.
          digitalWrite ((43), LOW);//Turn pin off.
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 35;  // new pin to blink

        }

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sat Oct 19, 2019 6:35 am

Hi Roadshark.
Hope you are having good riding weather down under. The frost is on the pumpkins here. Time to mount the studded tires on the winter bike.

The trigger pins on the Fx boards are 'active low'. So you will need to invert your logic as below:

Code: Select all | TOGGLE FULL SIZE
          digitalWrite ((43), LOW);//Activate Pin 0 on SFX player to trigger sound effect
          delay (150);//Give time for message to get to SFX player.
          digitalWrite ((43), HIGH);//Turn pin off.

adafruit_support_bill
 
Posts: 74951
Joined: Sat Feb 07, 2009 10:11 am

Re: sound effect player connecting to arduino mega

by roadshark on Sat Oct 19, 2019 7:08 am

Most excellent indeed. An easy fix. THX

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by roadshark on Sat Nov 02, 2019 4:18 am

I am using this code on a Mega which is a master.
Code: Select all | TOGGLE FULL SIZE
if (digitalRead (29) == LOW) {
          while (digitalRead (29));
          gameScore += 200;
         
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink
          delay (1000);
         
          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }

The sensor on pin 29 is activated. There is a light sequence then I want a servo attached to a slave to turn. Does this ("H") send one byte of data and is this the character H?
Here is the code on the slave. I am not sure if my logic is correct here:( I am using the itsy bitsy. When I run the code I get the error
Error compiling for board Adafruit ItsyBitsy 32u4 5V 16MHz.
If I comment out line 39 Wire.onReceive(receiveEvent); the code compiles.
So basically I want the itsy to turn two servo's when commanded by the master, one for a herman head (H)and one for a monster head.(M).
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Servo.h>
Servo hermansHead; // create servo object to control a servo
Servo monsterHead; // create servo object to control a servo
int posH = 0; // variable to store the servo position of hermans head
int pos = 180;    // variable to store the servo position of monster head
void receiveEvent(int howMany); // this function is registered as an event in setup()

//pins to activate sound effect player


//LED pins
int ledPins[] = {A4, A5, 5, 7, 9, 10, 11, 12};
int ledPinsCount = 8;

int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int blinksLeft = 0;  // set this variable any time you want to start a sequence of blinks
int blinkPin = 0;  // This is the pin number for the led to blink.
int i = 0;

void setup()

{
  pinMode(A2, OUTPUT);//connect to pin T05 on sound board
  pinMode(A3, OUTPUT);//connect to pin T06 on sound board
  //turns pins attached to LED's as outputs.
  for (int thisPin = 0; thisPin  < ledPinsCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }

  hermansHead.attach(A0);
  hermansHead.write(posH);
  monsterHead.attach (A1);
  monsterHead.write(pos);
  Wire.begin(8);
  Wire.onReceive(receiveEvent);//if commented out the code compiles.

}


void loop() {

  unsigned long currentMillis = millis();
  char commandString[1];  //declare an array of 1 character
  //int index = 0;
  while (Wire.available())
  {
    commandString[i] = Wire.read(); // places character into array
  }

  // Turn the hermansHead servo 180 degrees, blink lights, play sound effect, turn servo 180 degrees back to 0
  if (commandString[0] == 'H') {
    for (posH = 0; posH <= 180; posH += 2) { // Moves hermans head  from 0 degrees to 180 degrees in steps of 2 degree
      hermansHead.write(posH);              // tell servo to go to position in variable 'posH'
      delay(10);                       // waits 10ms for the servo to reach the position
    }
    pinMode (A2, OUTPUT);
    digitalWrite ((A2), LOW);//Activate Pin T05 on Sound Effects player.
    delay (150);
    pinMode (A2, INPUT);
    for (int blinks = 0; blinks < 5; blinks++)//Blink monster character light
    {
      digitalWrite((40), HIGH);
      delay(200);
      digitalWrite((40), LOW);
      delay(200);

    }

    delay (1000);

    for (posH = 180; posH >= 0; posH -= 2) { // hermans head goes from 0 degrees to 180 degrees
      hermansHead.write(posH);              // tell servo to go to position in variable 'pos'
      delay(10);                       // waits 15ms for the servo to reach the position


    }

  }


  if (commandString[0] == 'M') {
    for (pos = 180; pos >= 0; pos -= 2) { // Moves monster head  from 0 degrees to 180 degrees in steps of 2 degree
      monsterHead.write(pos);              // tell servo to go to position in variable 'pos'
      delay(10);                       // waits 10ms for the servo to reach the position
    }
    digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
    blinksLeft = 3;  // start a series of blinks
    blinkPin = A4;  // new pin to blink
    pinMode (A3, OUTPUT);
    digitalWrite ((A3), LOW);//Activate Pin T06 on Sound Effects player.
    delay (150);
    pinMode (A3, INPUT);

    delay (1000);

    for (pos = 0; pos <= 180; pos += 2) { // MONSTER head goes from 180 degrees to 0 degrees
      monsterHead.write(pos);              // tell servo to go to position in variable 'pos'
      delay(10);                       // waits 15ms for the servo to reach the position


    }
    if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
    {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
      {
        ledState = HIGH;
      }
      else
      {
        ledState = LOW;
        blinksLeft--; // one blink done
      }
      digitalWrite(blinkPin, ledState);
    }

  }
}

Will the slave when it gets a command from the master only perform the instruction once?

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sat Nov 02, 2019 6:31 am

Is there more to the error text? That might give us more to go on.

One thing I see is that "receiveEvent" is not defined. You have a 'forward declaration' for it here: https://www.learncpp.com/cpp-tutorial/f ... larations/
Code: Select all | TOGGLE FULL SIZE
void receiveEvent(int howMany); // this function is registered as an event in setup()

But you have not defined the body of the function.

adafruit_support_bill
 
Posts: 74951
Joined: Sat Feb 07, 2009 10:11 am

Re: sound effect player connecting to arduino mega

by roadshark on Sat Nov 02, 2019 7:32 am

Hi Bill,
I have developed a game based on the carnival game of the dropping a ball into moving clown heads except I have used Frankensteins monster heads. I created a program that worked however because I have some figures turning in the game on micro servos I found that it held up the code too long so I wanted to pass that function to a slave to free up the main processor so it could keep scoring the balls.
The game has 3 variations 1 no sound or electronic scoring (kids have to use own brains). 2 Sound and electronic scoring with pre defined values to sensors. 3. Sound and electronic scoring but random values given to sensors. I am using adafruit push buttons switches with led rings (PRODUCT ID: 481) for selection of games. I plan to have four (one as a reset) illuminating at a time. The tech details say they can take 6 volts but am I able to push this to 9? And how much current does the circle Leds on the switch draw?

Have been working on the code for a few hours. This now compiles but should it do what I want?
I am sending this from the master,
Code: Select all | TOGGLE FULL SIZE
Wire.beginTransmission(8); // transmit to device #8
      Wire.write("M"); // sends one bytes.Servo monster head turns.
      Wire.endTransmission();

I want the slave to turn a servo:) Blink some LED's and play a sound from the sound affects board.
Slave Code
Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Servo.h>
Servo hermansHead; // create servo object to control a servo
Servo monsterHead; // create servo object to control a servo
int posH = 0; // variable to store the servo position of hermans head
int pos = 180;    // variable to store the servo position of monster head

//LED pins
int ledPins[] = {A4, A5, 5, 7, 9, 10, 11, 12};
int ledPinsCount = 8;

int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int blinksLeft = 0;  // set this variable any time you want to start a sequence of blinks
int blinkPin = 0;  // This is the pin number for the led to blink.
int i = 0;
char commandString[1];  //declare an array of 1 character

void setup()

{
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
  pinMode(A2, OUTPUT);//connect to pin T05 on sound board
  pinMode(A3, OUTPUT);//connect to pin T06 on sound board
  //turns pins attached to LED's as outputs.
  for (int thisPin = 0; thisPin  < ledPinsCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }

  hermansHead.attach(A0);
  hermansHead.write(posH);
  monsterHead.attach (A1);
  monsterHead.write(pos);

}


void loop() {

  unsigned long currentMillis = millis();

 
  // Turn the hermansHead servo 180 degrees, blink lights, play sound effect, turn servo 180 degrees back to 0
  if (commandString[0] == 'H') {
    for (posH = 0; posH <= 180; posH += 2) { // Moves hermans head  from 0 degrees to 180 degrees in steps of 2 degree
      hermansHead.write(posH);              // tell servo to go to position in variable 'posH'
      delay(10);                       // waits 10ms for the servo to reach the position
    }
    pinMode (A2, OUTPUT);
    digitalWrite ((A2), LOW);//Activate Pin T05 on Sound Effects player.
    delay (150);
    pinMode (A2, INPUT);
    digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
    blinksLeft = 3;  // start a series of blinks
    blinkPin = A5;  // new pin to blink

    delay (1000);

    for (posH = 180; posH >= 0; posH -= 2) { // hermans head goes from 0 degrees to 180 degrees
      hermansHead.write(posH);              // tell servo to go to position in variable 'posH'
      delay(10);                       // waits 10ms for the servo to reach the position


    }

  }


  if (commandString[0] == 'M') {
    for (pos = 180; pos >= 0; pos -= 2) { // Moves monster head  from 0 degrees to 180 degrees in steps of 2 degree
      monsterHead.write(pos);              // tell servo to go to position in variable 'pos'
      delay(10);                       // waits 10ms for the servo to reach the position
    }
    digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
    blinksLeft = 3;  // start a series of blinks
    blinkPin = A4;  // new pin to blink
    pinMode (A3, OUTPUT);
    digitalWrite ((A3), LOW);//Activate Pin T06 on Sound Effects player.
    delay (150);
    pinMode (A3, INPUT);

    delay (1000);

    for (pos = 0; pos <= 180; pos += 2) { // MONSTER head goes from 180 degrees to 0 degrees
      monsterHead.write(pos);              // tell servo to go to position in variable 'pos'
      delay(10);                       // waits 15ms for the servo to reach the position


    }
    if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
    {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
      {
        ledState = HIGH;
      }
      else
      {
        ledState = LOW;
        blinksLeft--; // one blink done
      }
      digitalWrite(blinkPin, ledState);
    }

  }
}
void receiveEvent(int howMany) {// this function is registered as an event in setup()

  while (Wire.available())
  {
    commandString[i] = Wire.read(); // places character into array
  }
}

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sat Nov 02, 2019 9:40 am

The tech details say they can take 6 volts but am I able to push this to 9?

You will need a little extra resistance in series with the LED. For 9v, 220 ohms should do.

Have been working on the code for a few hours. This now compiles but should it do what I want?

It looks good. The receiveEvent callback function will stuff the command character into the command string and the loop should pic it up on the next pass.

adafruit_support_bill
 
Posts: 74951
Joined: Sat Feb 07, 2009 10:11 am

Re: sound effect player connecting to arduino mega

by roadshark on Mon Nov 04, 2019 6:56 am

Hi Bill,
As usual lots of hours trying to get this code to work. At the moment the code does this ok.
Code: Select all | TOGGLE FULL SIZE
void loop() {
  unsigned long currentMillis = millis();
  switch (state)

  {
    case 0:
      //turns off the motor
      digitalWrite ((49), LOW);//Low_speed
      digitalWrite ((50), LOW);//High_speed



      matrix.begin(0x71);
      matrix.blinkRate(2);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      state = 1;
      break;

    case 1:
      Serial.print("In case 1 selection of type of game played");

      if (digitalRead (22) == LOW) {
        gameScore = 0;
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 85 degrees in steps of 5 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 10ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 85 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 2");
        state = 2;
        break;
      }


      if (digitalRead (23) == LOW) {

        gameScore = 0;
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start up sound
        delay (150);
        pinMode (48, INPUT);

        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }


        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 3");
        state = 3;
        break;
      }


      if (digitalRead (24) == LOW) {

        gameScore = 0;
        randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO
        for ( int i = 0; i < 8; i++) {
          mySensVals[i] = possibleValues[random(0, 7)];

        }
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start sound
        delay (150);
        pinMode (48, INPUT);
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }


        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 4");
        state = 4;
        break;
      }
      state = 1;
      break;//  Here the code loops until a button is pressed to exit this state.

So if I press button connected to pin 22 starting sequence lights on, servo powers up and dc motor turns on but score of 20 appears on display and nothing else works.
So if I press button connected to pin 23 starting sequence lights on, servo powers up and dc motor turns on but score of 10 appears on display and led attached to pin 42 lights up. sound effect plays, nothing else works.
So if I press button connected to pin 24 starting sequence lights on, servo powers up and dc motor turns on, sound effect plays but nothing else works.
Button attached to pin 47 when pressed does not reset game.
I am assuming I have state/break in the wrong places.
My idea was to have the program stay in a state until pin 47 is activated. In case 1 the player chooses between three options. Depending on the choice the code should be directed to one of three states. The only way to get out of a state is to press button attached to 47???
It is like the code enters a state but then leaves it and goes on its merry way. In game three I tried to randomise the scoring so one sensor could have 5 different values (that is the plan at least) so there is a lot of code in this game. Here it is for your experienced eye to peruse.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_LEDBackpack.h>
#include <Wire.h>
Adafruit_7segment matrix = Adafruit_7segment();
#include <Servo.h>

Servo ballRelease;// create servo object to control a servo

int pos = 85;    // variable to store the servo position ball release
int gameScore = 0;//players score
int gameScoreDefault = 0;
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 47}; //Pin 22 = no scoring. Pin 23 = normal game. Pin 24 = Random scores Pins  (25, 26, 27, 28, 29, 30, 31, 32)= ball sensors
int sensorPinsCount = 12;
int mySensVals[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for the eight pins normal game.
int possibleValues[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for a random game.
int headLedPins[] = {33, 34, 35, 36, 37, 38};//LEDS FOR start up sequence
int headLedPinsCount = 6;
int scoreLedPins [] = {39, 40, 41, 42, 43};
int scoreLedPinsCount = 5;
int soundEffectActivationPin[] = {44, 45, 46, 48}; //sound effect player (44=100.  45= 20. 46 = 10   48= start up sound)
int soundEffectActivationPinCount = 4;
int state = 0;
int x = 0;


//Blinking SCORE LEDS
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int blinksLeft = 0;  // set this variable any time you want to start a sequence of blinks
int blinkPin = 0;  // This is the pin number for the led to blink.

void setup() {
  Serial.begin(9600);
  Wire.begin ();

  //ATTACH SEVOS TO PINS AND SET DEFAULT POSITION
  ballRelease.attach(9);
  ballRelease.write(pos);


  pinMode (49, OUTPUT);
  pinMode (50, OUTPUT);

  //Sets the reset button and break beam sensors pins to inputs.
  for (int thisPin = 0; thisPin < sensorPinsCount; thisPin++) {
    pinMode(sensorPins[thisPin], INPUT_PULLUP);
  }
  //Sets the sound effect activation pins to output.
  for (int thisPin = 0; thisPin  < soundEffectActivationPinCount; thisPin++) {
    pinMode(soundEffectActivationPin[thisPin], INPUT);
  }
  //sets the scoring LEDs pins to out put
  for (int thisPin = 0; thisPin  < scoreLedPinsCount; thisPin++) {
    pinMode(scoreLedPins[thisPin], OUTPUT);
  }
  //Sets the pins for the LEDS on MOVING HEAD  to output.
  for (int thisPin = 0; thisPin  < headLedPinsCount; thisPin++) {
    pinMode(headLedPins[thisPin], OUTPUT);
  }

}
void loop() {
  unsigned long currentMillis = millis();
  switch (state)

  {
    case 0:
      //turns off the motor
      digitalWrite ((49), LOW);//Low_speed
      digitalWrite ((50), LOW);//High_speed



      matrix.begin(0x71);
      matrix.blinkRate(2);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      state = 1;
      break;

    case 1:
      Serial.print("In case 1 selection of type of game played");

      if (digitalRead (22) == LOW) {
        gameScore = 0;
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 85 degrees in steps of 5 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 10ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 85 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 2");
        state = 2;
        break;
      }


      if (digitalRead (23) == LOW) {

        gameScore = 0;
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start up sound
        delay (150);
        pinMode (48, INPUT);

        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }


        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 3");
        state = 3;
        break;
      }


      if (digitalRead (24) == LOW) {

        gameScore = 0;
        randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO
        for ( int i = 0; i < 8; i++) {
          mySensVals[i] = possibleValues[random(0, 7)];

        }
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start sound
        delay (150);
        pinMode (48, INPUT);
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }


        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 4");
        state = 4;
        break;
      }
      state = 1;
      break;//  Here the code loops until a button is pressed to exit this state.
    //***************************************GAME ONE NO SOUNDS AND NO DISPLAY****************************************************
    case 2:
      Serial.print("In case 2 game 1");

      matrix.begin(0x71);
      matrix.print(gameScoreDefault, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {//reset button

        gameScore = 0;
        state = 0;
        break;
      }
      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
        while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
        gameScore += 100;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 39;  // new pin to blink

      }
      if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
        while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
        gameScore += 5;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 40;  // new pin to blink
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();
      }

      if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
        while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
        gameScore += 20;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 41;  // new pin to blink
      }
      if (digitalRead (28) == LOW) {
        while (digitalRead (28));
        gameScore += 10;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 42;  // new pin to blink
      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29));
        gameScore += 200;

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 43;  // new pin to blink
        delay (1000);

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      //Herman character turns on  player scores of
      if ((gameScore == 100) || (gameScore == 220))  {
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo Hermans head turns.
        Wire.endTransmission();
      }
      //Monster Character turns on player score when 150 or 300
      if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
      {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        {
          ledState = HIGH;
        }
        else
        {
          ledState = LOW;
          blinksLeft--; // one blink done
        }
        digitalWrite(blinkPin, ledState);
      }
    //state = 2;
    //break;




    //*********************************************GAME  TWO********************************************************************************************************************
    case 3:
      Serial.print("In case 3 game 2");

      matrix.begin(0x71);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {

        gameScore = 0;
        state = 0;
        break;
      }


      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }

      if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
        while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
        gameScore += 100;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        pinMode (44, OUTPUT);
        digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
        delay (150);
        pinMode (44, INPUT);
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 39;  // new pin to blink

      }
      if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
        while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
        gameScore += 5;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 40;  // new pin to blink


        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();



      }

      if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
        while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
        gameScore += 20;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        pinMode (45, OUTPUT);
        digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
        delay (150);
        pinMode (45, INPUT);

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 41;  // new pin to blink
      }

      if (digitalRead (28) == LOW) {
        while (digitalRead (28));
        gameScore += 10;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        pinMode (46, OUTPUT);
        digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
        delay (150);
        pinMode (46, INPUT);

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 42;  // new pin to blink

      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29));
        gameScore += 200;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 43;  // new pin to blink


        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo Hermans head turns.
        Wire.endTransmission();
      }


      //Herman character turns on bonus player scores of
      if ((gameScore == 120) || (gameScore == 220))  {
        gameScore += 50;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();


        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo Hermans head turns.
        Wire.endTransmission();
      }
      //Monster Character turns on player score when 150 or 300
      if ((gameScore == 70) || (gameScore == 300))  {//Monster head character bonus
        gameScore += 60;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();



      }
      if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
      {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        {
          ledState = HIGH;
        }
        else
        {
          ledState = LOW;
          blinksLeft--; // one blink done
        }
        digitalWrite(blinkPin, ledState);
      }
    //state = 3;
    //break;



    // *************************************GAME THREE *************************************************************************************************
    case 4:
      Serial.print("In case 4 game 3");

      matrix.begin(0x71);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {

        gameScore = 0;
        state = 0;
        break;
      }


      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if (digitalRead (25) == LOW) {
        while (digitalRead (25) == LOW);
        x = mySensVals[1];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (26) == LOW) {
        while (digitalRead (26) == LOW);
        x = mySensVals[2];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking pinMode (45, OUTPUT);

          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();


        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 33;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (27) == LOW)  {
        while (digitalRead (27) == LOW);
        x = mySensVals[3];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }
        if (x == 20) {
          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }




      }

      if (digitalRead (28) == LOW) {
        while (digitalRead (28) == LOW);
        x = mySensVals[4];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
       
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29) == LOW);
        x = mySensVals[5];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (30) == LOW) {
        while (digitalRead (30) == LOW);
        x = mySensVals[6];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
       
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }

        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink



          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (31) == LOW) {
        while (digitalRead (31) == LOW);
        x = mySensVals[7];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite ((43), LOW);//Turn pin off.

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (32) == LOW) {
        while (digitalRead (32) == LOW);
        x = mySensVals[8];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();
        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        state = 4;
        break;
      }


  }
}


I have not yet hooked up to the itsy as trying to get the master working. Sound effect board working.

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Nov 04, 2019 7:16 am

One problem I see is that there is no break at the end of case 2 or case 3. If you get to the end of case 2, it will continue on to execute the code in case 3. And if you get to the end of case 3, it will go on to execute case 4.

adafruit_support_bill
 
Posts: 74951
Joined: Sat Feb 07, 2009 10:11 am

Re: sound effect player connecting to arduino mega

by roadshark on Mon Nov 04, 2019 7:51 am

Thx for taking the time to review the case:) Placed the breaks but still will not read the scoring sensors.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_LEDBackpack.h>
#include <Wire.h>
Adafruit_7segment matrix = Adafruit_7segment();
#include <Servo.h>

Servo ballRelease;// create servo object to control a servo

int pos = 85;    // variable to store the servo position ball release
int gameScore = 0;//players score
int gameScoreDefault = 0;
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 47}; //Pin 22 = no scoring. Pin 23 = normal game. Pin 24 = Random scores Pins  (25, 26, 27, 28, 29, 30, 31, 32)= ball sensors
int sensorPinsCount = 12;
int mySensVals[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for the eight pins normal game.
int possibleValues[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for a random game.
int headLedPins[] = {33, 34, 35, 36, 37, 38};//LEDS FOR start up sequence
int headLedPinsCount = 6;
int scoreLedPins [] = {39, 40, 41, 42, 43};
int scoreLedPinsCount = 5;
int soundEffectActivationPin[] = {44, 45, 46, 48}; //sound effect player (44=100.  45= 20. 46 = 10   48= start up sound)
int soundEffectActivationPinCount = 4;
int state = 0;
int x = 0;
bool gameTwo = false;

//Blinking SCORE LEDS
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int blinksLeft = 0;  // set this variable any time you want to start a sequence of blinks
int blinkPin = 0;  // This is the pin number for the led to blink.

void setup() {
  Serial.begin(9600);
  Wire.begin ();

  //ATTACH SEVOS TO PINS AND SET DEFAULT POSITION
  ballRelease.attach(9);
  ballRelease.write(pos);


  pinMode (49, OUTPUT);
  pinMode (50, OUTPUT);

  //Sets the reset button and break beam sensors pins to inputs.
  for (int thisPin = 0; thisPin < sensorPinsCount; thisPin++) {
    pinMode(sensorPins[thisPin], INPUT_PULLUP);
  }
  //Sets the sound effect activation pins to output.
  for (int thisPin = 0; thisPin  < soundEffectActivationPinCount; thisPin++) {
    pinMode(soundEffectActivationPin[thisPin], INPUT);
  }
  //sets the scoring LEDs pins to out put
  for (int thisPin = 0; thisPin  < scoreLedPinsCount; thisPin++) {
    pinMode(scoreLedPins[thisPin], OUTPUT);
  }
  //Sets the pins for the LEDS on MOVING HEAD  to output.
  for (int thisPin = 0; thisPin  < headLedPinsCount; thisPin++) {
    pinMode(headLedPins[thisPin], OUTPUT);
  }

}
void loop() {
  unsigned long currentMillis = millis();
  switch (state)

  {
    case 0:
      //turns off the motor
      digitalWrite ((49), LOW);//Low_speed
      digitalWrite ((50), LOW);//High_speed



      matrix.begin(0x71);
      matrix.blinkRate(2);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      state = 1;
      break;

    case 1:
      Serial.print("In case 1 selection of type of game played");

      if (digitalRead (22) == LOW) {
        gameScore = 0;
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 85 degrees in steps of 5 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 10ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 85 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 2");
        state = 2;
        break;
      }


      if (digitalRead (23) == LOW) {
        gameTwo = true;
        gameScore = 0;
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start up sound
        delay (150);
        pinMode (48, INPUT);

        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }


        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 3");
        state = 3;
        break;
      }


      if (digitalRead (24) == LOW) {

        gameScore = 0;
        randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO
        for ( int i = 0; i < 8; i++) {
          mySensVals[i] = possibleValues[random(0, 7)];

        }
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start sound
        delay (150);
        pinMode (48, INPUT);
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }


        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 4");
        state = 4;
        break;
      }
      state = 1;
      break;//  Here the code loops until a button is pressed to exit this state.
    //***************************************GAME ONE NO SOUNDS AND NO DISPLAY****************************************************
    case 2:
      Serial.print("In case 2 game 1");

      matrix.begin(0x71);
      matrix.print(gameScoreDefault, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {//reset button

        gameScore = 0;
        state = 0;
        break;
      }
      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
        while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
        gameScore += 100;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 39;  // new pin to blink

      }
      if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
        while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
        gameScore += 5;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 40;  // new pin to blink
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();
      }

      if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
        while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
        gameScore += 20;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 41;  // new pin to blink
      }
      if (digitalRead (28) == LOW) {
        while (digitalRead (28));
        gameScore += 10;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 42;  // new pin to blink
      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29));
        gameScore += 200;

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 43;  // new pin to blink
        delay (1000);

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      //Herman character turns on  player scores of
      if ((gameScore == 100) || (gameScore == 220))  {
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo Hermans head turns.
        Wire.endTransmission();
      }
      //Monster Character turns on player score when 150 or 300
      if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
      {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        {
          ledState = HIGH;
        }
        else
        {
          ledState = LOW;
          blinksLeft--; // one blink done
        }
        digitalWrite(blinkPin, ledState);
      }
    //state = 2;
    break;




    //*********************************************GAME  TWO********************************************************************************************************************
    case 3:
      if (gameTwo == true) {
        Serial.print("In case 3 game 2");

        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (digitalRead (47) == LOW) {

          gameScore = 0;
          state = 0;
          break;
        }


        if (gameScore >= 220) {
          digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
          digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
        }

        if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
          while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
          gameScore += 100;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
          while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
          gameScore += 5;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }

        if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
          while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
          gameScore += 20;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink
        }

        if (digitalRead (28) == LOW) {
          while (digitalRead (28));
          gameScore += 10;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink

        }

        if (digitalRead (29) == LOW) {
          while (digitalRead (29));
          gameScore += 200;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 70) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        //state = 3;
      }
   
    //break;



    // *************************************GAME THREE *************************************************************************************************
    case 4:
      Serial.print("In case 4 game 3");

      matrix.begin(0x71);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {

        gameScore = 0;
        state = 0;
        break;
      }


      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if (digitalRead (25) == LOW) {
        while (digitalRead (25) == LOW);
        x = mySensVals[1];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (26) == LOW) {
        while (digitalRead (26) == LOW);
        x = mySensVals[2];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking pinMode (45, OUTPUT);

          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();


        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 33;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (27) == LOW)  {
        while (digitalRead (27) == LOW);
        x = mySensVals[3];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }
        if (x == 20) {
          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }




      }

      if (digitalRead (28) == LOW) {
        while (digitalRead (28) == LOW);
        x = mySensVals[4];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29) == LOW);
        x = mySensVals[5];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (30) == LOW) {
        while (digitalRead (30) == LOW);
        x = mySensVals[6];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }

        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink



          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (31) == LOW) {
        while (digitalRead (31) == LOW);
        x = mySensVals[7];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite ((43), LOW);//Turn pin off.

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (32) == LOW) {
        while (digitalRead (32) == LOW);
        x = mySensVals[8];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();
        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        state = 4;
       
      }

break;
  }
}


This piece of code is what I am using to try and change a state. Can I have 'break;' inside curly brackets?
Code: Select all | TOGGLE FULL SIZE
if (digitalRead (47) == LOW) {//reset button

        gameScore = 0;
        state = 0;
        break;
      }


I think this seems a logical way to code this game. Is there another approach that may work?

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Nov 04, 2019 8:09 am

Placed the breaks but still will not read the scoring sensors.

Still missing the break between case 3 and case 4. (you have one there, but it is commented out).

What do you see in the Serial monitor when you run this and press the button connected to pin 22? Do you see "In case 2 game 1"?"

adafruit_support_bill
 
Posts: 74951
Joined: Sat Feb 07, 2009 10:11 am

Re: sound effect player connecting to arduino mega

by roadshark on Mon Nov 04, 2019 8:31 am

This is what I see now. In case 1 selection of type of game playedmoving to case 2In case 2 game 1
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_LEDBackpack.h>
#include <Wire.h>
Adafruit_7segment matrix = Adafruit_7segment();
#include <Servo.h>

Servo ballRelease;// create servo object to control a servo

int pos = 85;    // variable to store the servo position ball release
int gameScore = 0;//players score
int gameScoreDefault = 0;
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 47}; //Pin 22 = no scoring. Pin 23 = normal game. Pin 24 = Random scores Pins  (25, 26, 27, 28, 29, 30, 31, 32)= ball sensors
int sensorPinsCount = 12;
int mySensVals[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for the eight pins normal game.
int possibleValues[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for a random game.
int headLedPins[] = {33, 34, 35, 36, 37, 38};//LEDS FOR start up sequence
int headLedPinsCount = 6;
int scoreLedPins [] = {39, 40, 41, 42, 43};
int scoreLedPinsCount = 5;
int soundEffectActivationPin[] = {44, 45, 46, 48}; //sound effect player (44=100.  45= 20. 46 = 10   48= start up sound)
int soundEffectActivationPinCount = 4;
int state = 0;
int x = 0;
bool gameTwo = false;

//Blinking SCORE LEDS
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int blinksLeft = 0;  // set this variable any time you want to start a sequence of blinks
int blinkPin = 0;  // This is the pin number for the led to blink.

void setup() {
  Serial.begin(9600);
  Wire.begin ();

  //ATTACH SEVOS TO PINS AND SET DEFAULT POSITION
  ballRelease.attach(9);
  ballRelease.write(pos);


  pinMode (49, OUTPUT);
  pinMode (50, OUTPUT);

  //Sets the reset button and break beam sensors pins to inputs.
  for (int thisPin = 0; thisPin < sensorPinsCount; thisPin++) {
    pinMode(sensorPins[thisPin], INPUT_PULLUP);
  }
  //Sets the sound effect activation pins to output.
  for (int thisPin = 0; thisPin  < soundEffectActivationPinCount; thisPin++) {
    pinMode(soundEffectActivationPin[thisPin], INPUT);
  }
  //sets the scoring LEDs pins to out put
  for (int thisPin = 0; thisPin  < scoreLedPinsCount; thisPin++) {
    pinMode(scoreLedPins[thisPin], OUTPUT);
  }
  //Sets the pins for the LEDS on MOVING HEAD  to output.
  for (int thisPin = 0; thisPin  < headLedPinsCount; thisPin++) {
    pinMode(headLedPins[thisPin], OUTPUT);
  }

}
void loop() {
  unsigned long currentMillis = millis();
  switch (state)

  {
    case 0:
      //turns off the motor
      digitalWrite ((49), LOW);//Low_speed
      digitalWrite ((50), LOW);//High_speed



      matrix.begin(0x71);
      matrix.blinkRate(2);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      state = 1;
      break;

    case 1:
      Serial.print("In case 1 selection of type of game played");

      if (digitalRead (22) == LOW) {
        gameScore = 0;
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 85 degrees in steps of 5 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 10ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 85 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 2");
        state = 2;
        break;
      }


      if (digitalRead (23) == LOW) {
        gameTwo = true;
        gameScore = 0;
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start up sound
        delay (150);
        pinMode (48, INPUT);

        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }


        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 3");
        state = 3;
        break;
      }


      if (digitalRead (24) == LOW) {

        gameScore = 0;
        randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO
        for ( int i = 0; i < 8; i++) {
          mySensVals[i] = possibleValues[random(0, 7)];

        }
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start sound
        delay (150);
        pinMode (48, INPUT);
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }


        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 4");
        state = 4;
        break;
      }
      state = 1;
      break;//  Here the code loops until a button is pressed to exit this state.
    //***************************************GAME ONE NO SOUNDS AND NO DISPLAY****************************************************
    case 2:
      Serial.print("In case 2 game 1");

      matrix.begin(0x71);
      matrix.print(gameScoreDefault, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {//reset button

        gameScore = 0;
        state = 0;
        break;
      }
      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
        while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
        gameScore += 100;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 39;  // new pin to blink

      }
      if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
        while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
        gameScore += 5;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 40;  // new pin to blink
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();
      }

      if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
        while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
        gameScore += 20;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 41;  // new pin to blink
      }
      if (digitalRead (28) == LOW) {
        while (digitalRead (28));
        gameScore += 10;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 42;  // new pin to blink
      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29));
        gameScore += 200;

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 43;  // new pin to blink
        delay (1000);

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      //Herman character turns on  player scores of
      if ((gameScore == 100) || (gameScore == 220))  {
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo Hermans head turns.
        Wire.endTransmission();
      }
      //Monster Character turns on player score when 150 or 300
      if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
      {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        {
          ledState = HIGH;
        }
        else
        {
          ledState = LOW;
          blinksLeft--; // one blink done
        }
        digitalWrite(blinkPin, ledState);
      }
    //state = 2;
    break;




    //*********************************************GAME  TWO********************************************************************************************************************
    case 3:
      if (gameTwo == true) {
        Serial.print("In case 3 game 2");

        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (digitalRead (47) == LOW) {

          gameScore = 0;
          state = 0;
          break;
        }


        if (gameScore >= 220) {
          digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
          digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
        }

        if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
          while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
          gameScore += 100;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
          while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
          gameScore += 5;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }

        if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
          while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
          gameScore += 20;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink
        }

        if (digitalRead (28) == LOW) {
          while (digitalRead (28));
          gameScore += 10;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink

        }

        if (digitalRead (29) == LOW) {
          while (digitalRead (29));
          gameScore += 200;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 70) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        //state = 3;
      }
   
    break;



    // *************************************GAME THREE *************************************************************************************************
    case 4:
      Serial.print("In case 4 game 3");

      matrix.begin(0x71);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {

        gameScore = 0;
        state = 0;
        break;
      }


      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if (digitalRead (25) == LOW) {
        while (digitalRead (25) == LOW);
        x = mySensVals[1];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (26) == LOW) {
        while (digitalRead (26) == LOW);
        x = mySensVals[2];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking pinMode (45, OUTPUT);

          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();


        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 33;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (27) == LOW)  {
        while (digitalRead (27) == LOW);
        x = mySensVals[3];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }
        if (x == 20) {
          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }




      }

      if (digitalRead (28) == LOW) {
        while (digitalRead (28) == LOW);
        x = mySensVals[4];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29) == LOW);
        x = mySensVals[5];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (30) == LOW) {
        while (digitalRead (30) == LOW);
        x = mySensVals[6];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }

        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink



          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (31) == LOW) {
        while (digitalRead (31) == LOW);
        x = mySensVals[7];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite ((43), LOW);//Turn pin off.

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (32) == LOW) {
        while (digitalRead (32) == LOW);
        x = mySensVals[8];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();
        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        state = 4;
        break;
      }

break;
  }
}

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by roadshark on Mon Nov 04, 2019 8:35 am

button 23 ase 1 selection of type of game playedmoving to case 3In case 3 game 2
button 24 n case 1 selection of type of game playedmoving to case 4In case 4 game 3

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Nov 04, 2019 10:46 am

If you change the "Serial.print" statements to "Serial.println" it will make the debugging output a little easier to read.

So it looks like you are doing the state change OK. Then you call "matrix.begin" again. This should not be necessary - and could possibly be where you are hanging up. Try commenting out the extra "matrix.begin" statements.

adafruit_support_bill
 
Posts: 74951
Joined: Sat Feb 07, 2009 10:11 am

Re: sound effect player connecting to arduino mega

by roadshark on Tue Nov 05, 2019 7:49 am

Thx for the line tip. it makes it easier to read for sure. Here is the latest message on press of button 23
In case 1 selection of type of game played
moving to case 3
In case 3 game 2

Still same problem in that nothing is being read in case 3. (and other cases as well 2 and 4) I press sensor buttons no change in score.
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_LEDBackpack.h>
#include <Wire.h>
Adafruit_7segment matrix = Adafruit_7segment();
#include <Servo.h>

Servo ballRelease;// create servo object to control a servo

int pos = 85;    // variable to store the servo position ball release
int gameScore = 0;//players score
int gameScoreDefault = 0;
int sensorPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 47}; //Pin 22 = no scoring. Pin 23 = normal game. Pin 24 = Random scores Pins  (25, 26, 27, 28, 29, 30, 31, 32)= ball sensors
int sensorPinsCount = 12;
int mySensVals[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for the eight pins normal game.
int possibleValues[] = {100, 5, 20, 10, 200, 20, 5, 100};//Values for a random game.
int headLedPins[] = {33, 34, 35, 36, 37, 38};//LEDS FOR start up sequence
int headLedPinsCount = 6;
int scoreLedPins [] = {39, 40, 41, 42, 43};
int scoreLedPinsCount = 5;
int soundEffectActivationPin[] = {44, 45, 46, 48}; //sound effect player (44=100.  45= 20. 46 = 10   48= start up sound)
int soundEffectActivationPinCount = 4;
int state = 0;
int x = 0;
bool gameTwo = false;

//Blinking SCORE LEDS
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 500;           // interval at which to blink (milliseconds)
int blinksLeft = 0;  // set this variable any time you want to start a sequence of blinks
int blinkPin = 0;  // This is the pin number for the led to blink.

void setup() {
  Serial.begin(9600);
  Wire.begin ();
  matrix.begin(0x71);

  //ATTACH SEVOS TO PINS AND SET DEFAULT POSITION
  ballRelease.attach(9);
  ballRelease.write(pos);


  pinMode (49, OUTPUT);
  pinMode (50, OUTPUT);

  //Sets the reset button and break beam sensors pins to inputs.
  for (int thisPin = 0; thisPin < sensorPinsCount; thisPin++) {
    pinMode(sensorPins[thisPin], INPUT_PULLUP);
  }
  //Sets the sound effect activation pins to output.
  for (int thisPin = 0; thisPin  < soundEffectActivationPinCount; thisPin++) {
    pinMode(soundEffectActivationPin[thisPin], INPUT);
  }
  //sets the scoring LEDs pins to out put
  for (int thisPin = 0; thisPin  < scoreLedPinsCount; thisPin++) {
    pinMode(scoreLedPins[thisPin], OUTPUT);
  }
  //Sets the pins for the LEDS on MOVING HEAD  to output.
  for (int thisPin = 0; thisPin  < headLedPinsCount; thisPin++) {
    pinMode(headLedPins[thisPin], OUTPUT);
  }

}
void loop() {
  unsigned long currentMillis = millis();
  switch (state)

  {
    case 0:
    Serial.println("In case 0 turn off motor");
      //turns off the motor
      digitalWrite ((49), LOW);//Low_speed
      digitalWrite ((50), LOW);//High_speed



      //matrix.begin(0x71);
      matrix.blinkRate(2);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      state = 1;
      break;

    case 1:
      Serial.println("In case 1 selection of type of game played");

      if (digitalRead (22) == LOW) {
        gameScore = 0;
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 85 degrees in steps of 5 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 10ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 85 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.println("moving to case 2");
        state = 2;
        break;
      }


      if (digitalRead (23) == LOW) {
        gameTwo = true;
        gameScore = 0;
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start up sound
        delay (150);
        pinMode (48, INPUT);

        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }
        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }


        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 3");
        state = 3;
        break;
      }


      if (digitalRead (24) == LOW) {

        gameScore = 0;
        randomSeed(analogRead(A0));//initialize the random number generator with a fairly random input, pin AO
        for ( int i = 0; i < 8; i++) {
          mySensVals[i] = possibleValues[random(0, 7)];

        }
        pinMode (48, OUTPUT);
        digitalWrite ((48), LOW);//Activate start sound
        delay (150);
        pinMode (48, INPUT);
        for (int j = 0; j < 5; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(100);
            digitalWrite(headLedPins[i], LOW);
            delay (100);
          }
        }


        for (pos = 0; pos <= 85; pos += 5) { // goes from 0 degrees to 120 degrees in steps of 1 degree
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);                       // waits 15ms for the servo to reach the position
        }
        delay (3500);//Gives time for the balls to roll down into catcher.
        for (pos = 85; pos >= 0; pos -= 3) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 10ms for the servo to reach the position

        }

        digitalWrite ((49), HIGH);//Turns on DC motor via resistor AND transistor
        Serial.print("moving to case 4");
        state = 4;
        break;
      }
      state = 1;
      break;//  Here the code loops until a button is pressed to exit this state.
    //***************************************GAME ONE NO SOUNDS AND NO DISPLAY****************************************************
    case 2:
      Serial.println("In case 2 game 1");

      //matrix.begin(0x71);
      matrix.print(gameScoreDefault, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {//reset button

        gameScore = 0;
        state = 0;
        break;
      }
      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
        while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
        gameScore += 100;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 39;  // new pin to blink

      }
      if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
        while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
        gameScore += 5;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 40;  // new pin to blink
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();
      }

      if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
        while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
        gameScore += 20;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 41;  // new pin to blink
      }
      if (digitalRead (28) == LOW) {
        while (digitalRead (28));
        gameScore += 10;
        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 42;  // new pin to blink
      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29));
        gameScore += 200;

        digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
        blinksLeft = 3;  // start a series of blinks
        blinkPin = 43;  // new pin to blink
        delay (1000);

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      //Herman character turns on  player scores of
      if ((gameScore == 100) || (gameScore == 220))  {
        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("H"); // sends one bytes.Servo Hermans head turns.
        Wire.endTransmission();
      }
      //Monster Character turns on player score when 150 or 300
      if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus

        Wire.beginTransmission(8); // transmit to device #8
        Wire.write("M"); // sends one bytes.Servo monster head turns.
        Wire.endTransmission();

      }


      if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
      {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW)
        {
          ledState = HIGH;
        }
        else
        {
          ledState = LOW;
          blinksLeft--; // one blink done
        }
        digitalWrite(blinkPin, ledState);
      }
    //state = 2;
    break;




    //*********************************************GAME  TWO********************************************************************************************************************
    case 3:
      if (gameTwo == true) {
        Serial.println("In case 3 game 2");

        //matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (digitalRead (47) == LOW) {

          gameScore = 0;
          state = 0;
          break;
        }


        if (gameScore >= 220) {
          digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
          digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
        }

        if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
          while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
          gameScore += 100;
          //matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if ((digitalRead (26) == LOW) || (digitalRead (31) == LOW)) {
          while ((digitalRead (26) == LOW) || (digitalRead (31) == LOW));
          gameScore += 5;
          //matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }

        if ((digitalRead (27) == LOW) || (digitalRead (30) == LOW)) {
          while ((digitalRead (27) == LOW) || (digitalRead (30) == LOW));
          gameScore += 20;
         // matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink
        }

        if (digitalRead (28) == LOW) {
          while (digitalRead (28));
          gameScore += 10;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink

        }

        if (digitalRead (29) == LOW) {
          while (digitalRead (29));
          gameScore += 200;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 70) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
         // matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();



        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        //state = 3;
      }
   
    break;



    // *************************************GAME THREE *************************************************************************************************
    case 4:
      Serial.println("In case 4 game 3");

      //matrix.begin(0x71);
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {

        gameScore = 0;
        state = 0;
        break;
      }


      if (gameScore >= 220) {
        digitalWrite ((49), LOW); //Turns off DC Motor Slow speed.
        digitalWrite ((50), HIGH);//Turns on DC motor High Speed,
      }
      //BREAK BEAM SENSOR SCORING
      if (digitalRead (25) == LOW) {
        while (digitalRead (25) == LOW);
        x = mySensVals[1];
        gameScore += x;
       // matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (26) == LOW) {
        while (digitalRead (26) == LOW);
        x = mySensVals[2];
        gameScore += x;
        //matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking pinMode (45, OUTPUT);

          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();


        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 33;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (27) == LOW)  {
        while (digitalRead (27) == LOW);
        x = mySensVals[3];
        gameScore += x;
        //matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();
        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }
        if (x == 20) {
          pinMode (45, OUTPUT);
          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }
        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }




      }

      if (digitalRead (28) == LOW) {
        while (digitalRead (28) == LOW);
        x = mySensVals[4];
        gameScore += x;
       // matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (29) == LOW) {
        while (digitalRead (29) == LOW);
        x = mySensVals[5];
        gameScore += x;
        //matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }
        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (30) == LOW) {
        while (digitalRead (30) == LOW);
        x = mySensVals[6];
        gameScore += x;
        //matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }

        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink



          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }

      }

      if (digitalRead (31) == LOW) {
        while (digitalRead (31) == LOW);
        x = mySensVals[7];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((43), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);
          digitalWrite ((43), LOW);//Turn pin off.

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
      }


      if (digitalRead (32) == LOW) {
        while (digitalRead (32) == LOW);
        x = mySensVals[8];
        gameScore += x;
        matrix.begin(0x71);
        matrix.print(gameScore, DEC);
        matrix.writeDisplay();

        if (x == 10) {
          pinMode (46, OUTPUT);
          digitalWrite ((46), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (46, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 42;  // new pin to blink
        }

        if (x == 20) {

          digitalWrite ((45), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (45, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 41;  // new pin to blink

        }
        if (x == 100) {
          pinMode (44, OUTPUT);
          digitalWrite ((44), LOW);//Activate Pin T05 on Sound Effects player.
          delay (150);
          pinMode (44, INPUT);

          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 39;  // new pin to blink

        }

        if (x == 5) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 40;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();

        }

        if ( x == 200) {
          digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
          blinksLeft = 3;  // start a series of blinks
          blinkPin = 43;  // new pin to blink

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }


        //Herman character turns on bonus player scores of
        if ((gameScore == 120) || (gameScore == 220))  {
          gameScore += 50;
          //matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();

          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("H"); // sends one bytes.Servo Hermans head turns.
          Wire.endTransmission();
        }
        //Monster Character turns on player score when 150 or 300
        if ((gameScore == 150) || (gameScore == 300))  {//Monster head character bonus
          gameScore += 60;
          //matrix.begin(0x71);
          matrix.print(gameScore, DEC);
          matrix.writeDisplay();


          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("M"); // sends one bytes.Servo monster head turns.
          Wire.endTransmission();
        }
        if ((currentMillis - previousMillis >= interval) && (blinksLeft > 0))
        {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW)
          {
            ledState = HIGH;
          }
          else
          {
            ledState = LOW;
            blinksLeft--; // one blink done
          }
          digitalWrite(blinkPin, ledState);
        }
        state = 4;
        break;
      }

break;
  }
}


Have commented out the matrix begin.

roadshark
 
Posts: 556
Joined: Sun Nov 29, 2015 5:53 am

Please be positive and constructive with your questions and comments.