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: 556
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: 74951
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: 556
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: 74951
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: 556
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: 74951
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: 556
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: 74951
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.