0

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

Please be positive and constructive with your questions and comments.

Re: sound effect player connecting to arduino mega

by roadshark on Sun Dec 15, 2019 3:58 am

Tried what you said (see previous post). I think I have destroyed a couple of the Mofsets :( I connected The drain (centre pin to the motor terminal),the source pin (right pin) to the -3v. I then attached a wire with a 10k Resistor in series to the gate pin (left pin). (As per the layput on the data sheet.) I attached the +3v to the other motor terminal. When I powered up nothing happened. :) As soon as I touched the gate wire to the positive +3 volt the motor started up however it kept going even after the wire from the gate was removed. I powered down. On start up the motor started straight away without touching the gate pin. Using a multimeter if I select ohms and place one of the probe tips on the S pin and the other tip on the Drain Pin I get a reading. This would indicate to me that I have fried the gate shut:( Surely 3volts through a 10k resistor should not be causing the frying?? Your thoughts. Before this I tried it using 5 volts same problem.

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

Re: sound effect player connecting to arduino mega

by roadshark on Sun Dec 15, 2019 5:38 am

Spent the afternoon in the shed:(
Scenario 1 +3 volt from power supply straight to dc motor. -3v to pin S on motor. D to other terminal of DC motor. Turn on power.
Motor does not start.
Connect G to +3V rail. Motor starts.
Remove G from +3V rail Motor keeps going.
Connect G to - 3V. The motor stops or slows down.
Remove G from -3V the motor starts again.
Scenario 2.
+3 volt from power supply straight to dc motor. G connected to +3V rail. D to other terminal of DC motor. Turn on power.
Motor does not start.
Connect S to -3v on rail motor turns.
Remove S from -3V on rail motor stops.
Connect G to -3V on rail and connect S to -3V on rail motor turns.

Also I have tried another 2 mofsets and on one of them when you connect G to +3volts the motor starts and will not stop even when you connect G to -3V like in the first scenario where the motor would stop.
My understanding is that when a small voltage is applied to a gate it closes the circuit allowing voltage to flow but as soon as the voltage is terminated the gate should open breaking the circuit.
Can you make sense of this?????

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sun Dec 15, 2019 8:29 am

The gate on a MOSFET is basically a little capacitor. When charged up, current flows between the source and drain.

When you connect the gate to the positive voltage it should turn the MOSFET on. If you disconnect it and leave the wire unconnected, there may still be a charge left on the gate, so it may not turn off. Touching the gate wire to GND should discharge the gate and turn it off.

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

Re: sound effect player connecting to arduino mega

by roadshark on Sun Dec 15, 2019 8:42 am

the motor I am using for testing is a small hobby motor that runs on 3v. I disconnected the gate and the motor kept running for about 1 minute before it stopped. Now in the game I am using PWM. I need the motor to stop when the game is over when I anologue write the pin as a 0. I need the head to stop turning straight away. In a previous design I used a transistor as a gate to turn the 9V to about 5v using a small voltage regulator. When a player reached a certain score I turned that pin off and turned another on that put 9 volts into the motor This worked fine though I thought that the transistor could not handle the amps drawn by the motor hence my venture into PWM as a more realistic way to control the motor speed. Is mofsets the best application here also it still does not solve the problem of turning the mega on and off?

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sun Dec 15, 2019 9:24 am

Leaving the gate unconnected is not guaranteed to turn it off. If you connect the gate to ground, it should turn the motor off. If controlling with a digital pin on your Mega, it will accomplish the same thing.

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

Re: sound effect player connecting to arduino mega

by roadshark on Mon Dec 16, 2019 3:48 am

Ok so it looks like the mosfet is wired correctly. I am using pin 12 on the mega as the pwm pin so by writing it with analogueWrite (12, 0) should turn the pin off? At the moment when the code requests the pin analogueWrite (12,170) the mega reboots so It looks like I need a capacitor to give the mosfet extra power when the motor starts and allowing the mega to continue functioning?
May be I should use a motor driver. I have the adafruit DRV8833 It is rated at 1.2 amps. The motor is rated from 4.5 to 18 volts. Motor nominal voltage is 12v. I will be putting 9v as the power source. No load current is 70mA . Full load current is 1.38A. Would this be at 12 volts? Would I be able to get away with the DRV8833.

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Dec 16, 2019 7:18 am

Ok so it looks like the mosfet is wired correctly. I am using pin 12 on the mega as the pwm pin so by writing it with analogueWrite (12, 0) should turn the pin off?

Yes, that should work.

At the moment when the code requests the pin analogueWrite (12,170) the mega reboots so It looks like I need a capacitor to give the mosfet extra power when the motor starts and allowing the mega to continue functioning?

A capacitor should help with the motor startup. What is the current capacity of your 9v power supply?

May be I should use a motor driver. I have the adafruit DRV8833 It is rated at 1.2 amps.

I don't think a DRV8833 would help. Most likely the problem is the power supply. Some capacitors can help with the startup current rush. But you need to make sure that the power supply can keep up with the steady-state current requirements once it is running.

Full load current is 1.38A. Would this be at 12 volts?

Assuming that the rating is based on a 12v supply, at 9v, the full load current would be about 1A. That means the startup/stall current is probably closer to 2A.

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

Re: sound effect player connecting to arduino mega

by roadshark on Tue Dec 17, 2019 2:12 am

The power supply is rated at 3.5A
hoe much current is drawn by a break beam sensor unit a small one? I have eight of these.
So I have a
sound effects board.
A mega
An itsy
6 LED switches
1 x Latching illuminated switch
8 breakbeam sensors (small)
1 9V motor.

2 Micro servo (Do these draw continuous current or onyl when called on in the code?
6 led light sequence/

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Tue Dec 17, 2019 7:08 am

The 2 processors are probably in the 25-50mA range
Allow 20mA per led switch
Another 20mA per break-beam
Micro servos are probably in the 100mA range when active but stationary with no load. They can be 1A or more when moving.
So your 3.5A supply should handle it.

The other factor is wiring. If your motor circuit shares power and ground wiring with the processor circuit, the resistance of the wire can cause a voltage drop proportional to the current. You should run power and ground from the power supply directly to the motor circuit. Then use separate wires to provide power and ground to the processor. That way, any drop in voltage due to motor current will affect only the motor.

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

Re: sound effect player connecting to arduino mega

by roadshark on Wed Dec 25, 2019 12:57 am

Hi Bill,
Thx for all the support. I wish you a very safe and happy holiday seasons.

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Wed Dec 25, 2019 8:32 am

Always a pleasure. Here's to another year of good riding and fun projects. :)

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

Re: sound effect player connecting to arduino mega

by roadshark on Sat Jan 25, 2020 3:41 am

Hi Bill, Hope U had a safe festive season. Have been on holidays for 5 weeks however torn my left cartilage so been off the bike:(. I am still unable to get the game to work. So here is what is happening so far. On power up the score light sequence starts and the correct value appears on the display for example white LED lights up and the displays shows the value of 5 so the player knows that this colour slot is worth 5 points. This goes through the 5 colours and values. At the end the display turns to 0. The program now waits for the player to select the game type. When the player selects the start game button the correct sound effect plays and the lighting sequence starts. The servo operates and lets the balls drop down into the collection slot. This is correct so far:) Now what is meant to happen is the head is meant to start turning, that is the DC motor is to start up. In the code this is analogueWrite 12. But what happens is the mega shuts down and reboots:(. I have include a wiring diagram. I placed a large capacitor in the circuit but no effect:(. Now I tried running the motor just from the power supply through the mosfet without connecting it to the PWM pin on the mega. No power to the mega or the bck converter. Now if I connect positive on the power supply to the positive wire on the mosfet circuit and negative the same way nothing happens. If I reverse the polarity the motor starts up and the capacitor heats up????? I have wired the n channel when looking at the side with the numbers facing you. (The flat side is at the back) Gate Motor Load.
Obviously this does not make sense.
I think what is happening is when the pin 12 is active it causes a short in the mosfet circuit and resets the mega?
Do I need the resistor in this circuit?
As always your thoughts appreciated:)
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 = 0;    // 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;
int motorPin = 12;


//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(0x70);

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

  pinMode (motorPin, 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
      analogWrite(motorPin, 0);


      matrix.blinkRate(0);
      gameScore = 5;
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      digitalWrite(40, HIGH);
      delay(1500);
      digitalWrite(40, LOW);
      gameScore = 10;
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      digitalWrite(42, HIGH);
      delay(1500);
      digitalWrite(42, LOW);
      gameScore = 20;
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      digitalWrite(41, HIGH);
      delay(1500);
      digitalWrite(41, LOW);
      gameScore = 100;
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      digitalWrite(39, HIGH);
      delay(1500);
      digitalWrite(39, LOW);
      gameScore = 200;
      matrix.print(gameScore, DEC);
      matrix.writeDisplay();
      digitalWrite(43, HIGH);
      delay(1500);
      digitalWrite(43, LOW);
      gameScore = 0;
      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) {
        matrix.blinkRate(0);

        for (int j = 0; j < 3; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(200);
            digitalWrite(headLedPins[i], LOW);
            delay (200);
          }
        }
        for (pos = 0; pos <= 90; pos += 10) { // 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 (2500);//Gives time for the balls to roll down into catcher.
        for (pos = 90; pos >= 0; pos -= 8) { // 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

        }


        Serial.println("moving to case 2");
        state = 2;

        break;
      }


      if (digitalRead (23) == LOW) {
        matrix.blinkRate(0);

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

        for (int j = 0; j < 3; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(200);
            digitalWrite(headLedPins[i], LOW);
            delay (200);
          }
        }


        for (pos = 0; pos <= 90; pos += 10) { // goes from 0 degrees to 85 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 = 90; pos >= 0; pos -= 10) { // goes from 85 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(10);// waits 15ms for the servo to reach the position

        }



        Serial.print("moving to case 3");
        state = 3;
        break;
      }


      if (digitalRead (24) == LOW) {
        matrix.blinkRate(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 < 3; j++)//Start game light sequence
        {
          for (int i = 0; i < headLedPinsCount; i++)
          {
            digitalWrite(headLedPins[i], HIGH);
            delay(200);
            digitalWrite(headLedPins[i], LOW);
            delay (200);
          }
        }


        for (pos = 0; pos <= 90; pos += 10) { // goes from 0 degrees to 85 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 = 90; pos >= 0; pos -= 9) { // goes from 120 degrees to 0 degrees
          ballRelease.write(pos);              // tell servo to go to position in variable 'pos'
          delay(15);// waits 15ms for the servo to reach the position

        }


        Serial.print("moving to case 4");
        state = 4;
        break;
      }

      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");
      analogWrite(motorPin, 190);


      matrix.print(gameScoreDefault, DEC);
      matrix.writeDisplay();
      if (digitalRead (47) == LOW) {

        Serial.println("Reset Button Pressed!");

        state = 0;
        break;
      }
      if (gameScore >= 220) {
        analogWrite(motorPin, 255);

      }
      //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) == LOW);
        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) == LOW);
        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.println("In case 3 game 2");
      analogWrite(motorPin, 190);

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

        Serial.println("Reset Button Pressed!");

        state = 0;
        break;
      }


      if (gameScore >= 220) {
        analogWrite(motorPin, 255);

      }

      if ((digitalRead (25) == LOW) || (digitalRead (32) == LOW)) {
        while ((digitalRead (25) == LOW) || (digitalRead (32) == LOW));
        gameScore += 100;
        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.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.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) == LOW);
        gameScore += 10;

        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) == LOW);
        gameScore += 200;

        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.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.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);
      }



      break;



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

      analogWrite(motorPin, 190);

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

        Serial.println("Reset Button Pressed!");

        state = 0;
        break;
      }


      if (gameScore >= 220) {
        analogWrite(motorPin, 255);

      }
      //BREAK BEAM SENSOR SCORING
      if (digitalRead (25) == LOW) {
        while (digitalRead (25) == LOW);
        x = mySensVals[1];
        gameScore += x;

        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.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 = 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 (27) == LOW)  {
        while (digitalRead (27) == LOW);
        x = mySensVals[3];
        gameScore += x;

        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.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.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.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.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.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.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.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;
  }
}
Attachments
wiring.png
wiring.png (42.18 KiB) Viewed 43 times

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

Re: sound effect player connecting to arduino mega

by roadshark on Sat Jan 25, 2020 6:43 am

Also if I disconnect the motor and pin 12 the program runs as it should without the head turning. If I drop the balls scoring works 75% of the time and the servos that turn other characters on certain points via the slave processor work as well. The wiring of the motor seems to be the issue. Drawing too much current and starving the mega or wired incorrectly???????

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sat Jan 25, 2020 8:23 am

Good morning Mr. Shark. Glad to see you have escaped all the flames.

Looks like you have the snubber diode reversed from what it should be. It is intended to short circuit reverse-voltage spikes from the motor. As it is now, it will short circuit whenever forward voltage is applied.

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

Re: sound effect player connecting to arduino mega

by roadshark on Sun Jan 26, 2020 2:27 am

Escaped them this time though I'm not at the Pearly Gates yet Bill! (So flames may be in the future:) ) Reversed the diode. Now the Mega does not shut down but motor does not work. Replaced the mosfet with a new one and removed the resistor. Now the motor works so looking good.
Testing the break beam sensors. They are at the moment only registering a ball passing 1 out of 5. Does the emitter send a straight light beam to the emitter or is it diffuse? I am testing the game under the veranda. Could there be background infra red?) The maximum distance between the emitter and collector is 3cm. I am using coloured high bounce balls, these are a translucent plastic with color mixed in. ( I will try painting the balls black to see if it makes a difference. When i break the beam with a piece of wood slowly the sensors work. If I speed the pass of the wood through the beam sometimes it is not registered. I assume I can not speed the rate the mega runs the program but can I interrupt the program to check all the sensors?? Could the balls be travelling too fast?
Attachments
ball.png
Type of ball being used.
ball.png (512.34 KiB) Viewed 25 times
slots.png
The ball slots.
slots.png (522.78 KiB) Viewed 25 times
emitter 1.png
The emitter and collector arrangement
emitter 1.png (558.8 KiB) Viewed 25 times

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

Please be positive and constructive with your questions and comments.