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 Sat Nov 09, 2019 6:58 am

Have had a cold blast from Antarctica here so away with the Summer shorts and out with the Winter woolies:(
The problem was the switch. For some reason one of the feet was misbehaving. I thought I swapped switches previously so not sure what happened. Now the code is running as expected (at the moment) with the reset button working.
I have now attached the itsy and progress. The serial commands are working. That is when the score pin 200 or 5 is activated on the master the slave (Itsy) turns the corresponding servo. Initially I had the servo's continually sweeping so I placed this code at the end of the servo's movement.
Code: Select all | TOGGLE FULL SIZE
commandString[0] = 'K';//Stops the sevo turning
As the servo's turn a character I have them turning towards each other so one starts at 0 degrees the other at 180 degrees. The sound effect works for each character and the LED blinks however I have a problem here.
The sequence of events;
1. The servo turns 180 degress showing the monster character to the player. (Works)
2. It pauses there whilst a sound is played (works) and a LED illuminates the character. (works)
3. The servo returns to its original position. (works)
The problem here is that the servo turns back to its original position before the light sequence begins.
If I use a delay to allow time there is a could be a possibility a command may be missed?
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, INPUT);//connect to pin T05 on sound board
  pinMode(A3, INPUT);//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 = A4;  // 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


    }
    commandString[0] = 'K';//Puts a character in the string to stop servo turning
  }

  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
    }
    pinMode (A3, OUTPUT);
    digitalWrite ((A3), LOW);//Activate Pin T06 on Sound Effects player.
    delay (150);
    pinMode (A3, INPUT);
   
    digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
    blinksLeft = 4;  // start a series of blinks
    blinkPin = A5;  // new pin to blink
   

    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


    }

    commandString[0] = 'K';//Stops the sevo turning
  }
  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
  }
}


When the servo reaches a position of 180 or 0 does it still draw current (is this the stall position) ?

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sat Nov 09, 2019 7:54 am

The problem is here:

Code: Select all | TOGGLE FULL SIZE
    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


That 'for' loop will take about 0.9 seconds to execute. And while it is in the loop, you will not be processing any of the blinking code. (or handling any scoring button presses)

There are a couple of ways to approach it: You could build all or part of the blinking into the servo loop. But that would not address the issue with the scoring buttons.

Or you could handle the servo move with a state machine approach similar to the blinking. That would involve a bit of restructuring of the code.

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

Re: sound effect player connecting to arduino mega

by roadshark on Sun Nov 10, 2019 5:53 am

So I would be using millis to see if the servo has moved since the last check and if it has not moved then move it a degree? So it is like the blinking of the LED is a degree turn of the servo?

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Sun Nov 10, 2019 7:07 am

That is correct. There is an example of how to do that here:

https://learn.adafruit.com/multi-taskin ... lean-sweep

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

Re: sound effect player connecting to arduino mega

by roadshark on Mon Nov 11, 2019 7:40 am

I hope you are enjoying pushing the knobby tyres (tires). ThX for the link. I am not sure how to incorporate this into the slave program. I was thinking some thing like this;
case 0.. If serial message M go to case 1. If serial message H go to case 2.
case 1 turn servoM 180 play sound and go to case 3.
case 2 turn servoH from 180 to 0, play sound then go to case 4.
case 3 is light sequence when finished go to case 5.
case 4 when light sequence finished go to case 6.
case 5 turn servo back change the letter to K to stop servo turning and then go to case 0.
case 6 turn servo back change the letter to K to stop servo turning and then go to case 0.

I am planning to use a 9 volt 3 am regulated power supply.
From my reading the mega is able to take 9 volts.
I have an itsy to power.
I have the 4 led lit push buttons rated at 6 volts.
I have a DC high torque motor rated from 4 volts to 16 volts. ( I am planning to send about 6 volts via a voltage regulator???) for slow speed and when the score reaches 220 we turn over to use 9 volts directly.
I have 3 micro servo's to power and they run on 4 to 6 volts.
I have 4 leds to power in the start up sequence and then individual leds to indicate scores.
I have a sound effect player to power. I think this is 5 volts.
So constant power would be the mega, the DC head turning motor, the 4 push button leds, the sound effects player and the itsy.
The maximum number of servos on at any one time would be 2 but this would be a rare occurrence.
I plan to use capacitors (the ones I got from you guys last time) to place with the servo's. (220uf 16 volts).
I have to make 6 sets as I have 6 head games.
Do you think 3 amps is enough and how do I step down from 9 volts to 6 volts....(Voltage regulators ???? The ones I looked at are out of stock at the moment or is there something better and cost effective?

Can you ask the head honchos to consider for the benefit of overseas buyers being able to buy goods when in stock but wait until out of stock items are in stock to fill the order then send. ( I used this with euro rail enthusiasts where I pay up front for the whole order and they send it when complete) I wanted 3 itsys but the regulators were out of stock. So I waited so as to combine postage which is quite expensive. Now the itsys are out of stock as well as blue leds.

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Nov 11, 2019 8:18 am

To incorporate in your code, start by adding the "Sweeper" class to your code.
Then replace each instance of Servo with an instance of Sweeper.
Do the attach() for each one in setup.
And call update() for each one in your loop.

To get 5v from your 9v supply, you can use a UBEC: https://www.adafruit.com/product/1385
You should be able to find 6v UBECs also at vendors specializing in RC hobby gear.

I will forward your request regarding backorders to the folks that manage that.

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

Re: sound effect player connecting to arduino mega

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

I will wait to the itsy's are back in stock then try the converters. I will get on the job of the sweep.
In this piece of code a led lights and stays on. Is this because the program enters case 4 turns the led on and then goes to state 6.
Code: Select all | TOGGLE FULL SIZE
 case 4:
      digitalWrite(blinkPin, LOW);  // cancel any ongoing blinking
      blinksLeft = 4;  // start a series of blinks
      blinkPin = A5;  // new pin to blink
      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 = 6;
      break;

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Nov 11, 2019 8:37 am

Yes. Unless you have code in case 6 to continue the blinking, it will stop.

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

Re: sound effect player connecting to arduino mega

by roadshark on Mon Nov 25, 2019 6:51 am

Hi Bill,
Going from 40 degrees one day to 15 degress the next. Love riding in Melbourne:) A couple of questions. I am planning to use the sound effects player with the 2.2 Watts built in amplifier. Can I power this off the mega or should I use a separate power feed. if so does the ground on the SFX player need to be connected to the GND on the mega? Would a capacitor be a good idea? If I need to use an external power feed with the SFX player can it handle 6 volts.
I am using an itsy as a slave and a 1.2" display. Will I need to put pull up resistors on the SCL and SDA lines. Would I use a capacitor here as well?
I plan to trigger some sound files from the mega and some from the itsy. Would this be an issue?
At the moment I plan to use a 9Volt 3.5 amp regulated power supply. It will need to run.
A mega
An itsy
A SFX with 2.2 Watt amplifier (Stereo)
3 micro servos. (One from the mega the other two from the itsy)
5 X 6 volt push button switches.
1 x 1.2" 4 digit seven segment display,
I am planning to use a buck converter to drop the voltage from 9 down to 6 for the LED switches.

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Mon Nov 25, 2019 9:25 am

To assure good power to the amp on the sound board, you will want to have a 5v supply with at least 1A of power. I believe that the 5v pin on the Mega has a 500mA polyfuse on it, so you are better off running separate power to the sound board. But you will need a ground connection between sound board and Mega. The sound board can handle up to 5.5v.

To use the ItsyBitsy as a slave, you will need pullup resistors on SDA and SCL. 10K should work.

You should be able to trigger from either Mega or Itsy as long as you have a common ground connection for all the boards.

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

Re: sound effect player connecting to arduino mega

by roadshark on Tue Nov 26, 2019 6:26 am

I plan to use this 16mm latching illuminated switch(PRODUCT ID: 1443) as the power on and off switch. I looked at the details. The LED has no built in resistor and I am planning to power it with 5 volts with a resistor (250 ohms) but it gives no details on the voltage the switch can handle. I want to use 9 volts DC. Should this be OK?
I plan to use the itsy to drive two servos (not at the same time). Would it be wise to wire in a capacitor on the servo power feeds?

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Tue Nov 26, 2019 7:55 am

That switch will handle 9v fine. A capacitor on the power feeds for the servos is always good insurance.

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

Re: sound effect player connecting to arduino mega

by roadshark on Fri Nov 29, 2019 6:09 am

In the game I am turning a huge head. I am using a 3 - 12 volt DC geared motor to do this. I am powering it with 9 volts. I remember you spoke about PWM to control the speed of a motor, In testing the motor turns fine at 9 volts and at 5 volts it slows considerable. I want the speed of the motor to increase as the players score increases.
Here is the code I have worked out so far. Will 255 give me 9 volts? and the lower value give me about 7 volts?
Code: Select all | TOGGLE FULL SIZE
int motorPin = 12;
int playerScore = 0;
void setup() {
  pinMode (motorPin,OUTPUT);
}

void loop() {
  analogWrite(motorPin, 190);
  if (playerScore >= 210){
    analogWrite(motorPin, 255);
  }
 

}

Can you check out the wiring for me?
Attachments
motor wire.jpg
motor wire.jpg (544.97 KiB) Viewed 64 times

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

Re: sound effect player connecting to arduino mega

by adafruit_support_bill on Fri Nov 29, 2019 7:56 am

By my calculations, a PWM value of 255 gives you 9v (minus a fraction of a volt drop through the transistor). And 210 would give you about 7.4v. (9v * 210) / 255 = 7.41v)

But you can just tweak that lower number until you get the speed you are looking for.

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

Re: sound effect player connecting to arduino mega

by roadshark on Sat Nov 30, 2019 1:33 am

I was planning to use your PN 2222 NPN transistor to switch on the motor. In the motor specs it stated that Full load current was 1.38 amps

Motor Type Gearhead
Motor Nominal Voltage 12V
No load current 70mA
Shaft diameter 5mm
Shaft length 9mm
No load RPM 36RPM
Full load current 1.38A
Full load torque 12kg.cm

Will I need to 'beef' up the transistor?

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

Please be positive and constructive with your questions and comments.