Airsoft Soundboard

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

Your conditional statement is a bit off. As it's written, it will probably turn the sound off as soon as you release the trigger (the button pin is no longer low, which triggers the else{} clause, and ACT will be low while the sound is playing).

This should do it:

Code: Select all

uint8_t trigger = 0;

void loop () {
    trigger = ( trigger << 1 ) + digitalRead( buttonPin );
    
    if ( 0x80 == trigger ) {                    //  new button press
        if ( LOW == digitalRead( ACTpin ) ) {   //  if a sound is playing
            sxf.stop();                         //  stop it 
            delay( 10 );                        //  give it time to stop
        }
        sfx.play( "T00     WAV" );              //  play a new sound
    }
    delay( 10 );
}
The variable 'trigger' works kind of like a low-resolution oscilloscope, keeping a running trace of the last 8 readings of the trigger button. That's convenient because the events we want to see appear as numbers:

- 0x80: one HIGH followed by seven LOWs: the button has just been pressed
- 0x00: eight LOWs: the button is being held down
- 0x01: seven LOWs followed by one HIGH: the button has just been released
- 0xFF: eight HIGHs: the button is sitting open

In this case, we want to know when the trigger has been pulled, so we use 0x80.

Once we know the trigger has been pulled, we can check the ACT pin to see if a track is already playing. If so, we can stop it. Then, whether we've stopped a track or not, we play a new one.

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

thanks for taking the time to put that together!

It still isn't working but I think I may have found a possible culprit. I tried having the LOW state of the act pin trigger some other things like LED on the trinket and another sound after a delay and those didn't work. I'm wondering if its an issue with the ACT pin? Is there any other code I can use that will verify that is working correctly.

I made a few spelling and formatting corrections to the code you sent over. just sfx spelled wrong and I changed the sfx.play to sfx.playTrack Let me know if this still looks correct to you. Also I put the uint8_t trigger = 0; under int ACTpin = 4; above the setup. Is that a correct place for that?

Code: Select all

    trigger = ( trigger << 1 ) + digitalRead( buttonBpin );
    
    if ( 0x80 == trigger ) {                    //  new button press
        if ( LOW == digitalRead( ACTpin ) ) {   //  if a sound is playing
            sfx.stop();                         //  stop it 
            delay( 10 );                        //  give it time to stop
        }
        sfx.playTrack( "T00     WAV" );              //  play a new sound
    }
    delay( 10 );

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

the ACT light on the sfx board does light up everytime I hear a sound.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

Post a photo showing your hardware and connections and we'll take a look. The ACT pin does go low when a sound is playing, so my first guess would be a weak connection.

800x600 images usually work best.

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

Image

Image

Image


Heres the current configuration.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

That all looks correct.

Check the ACT pin's voltage at both ends of the jumper when the sound is and is not playing. Let's see what the signals have to say for themselves.

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

Here are the readings I got. Seems to be working fine and sending the right signals.

Positve-ACT PIN
Negative-GROUND
2.982v when not pressed .049v when pressed

Positive-TRINKET GPIO 4
Negative-GROUND
2.954v when not pressed .032v when pressed

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

Okay, that suggests a software problem.

Double-check the code to make sure it's reading pin 4 when it looks for the ACT signal. As a sanity check, connect the jumper to the 3.3V and GND rails and see if that makes any difference.

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

In the code the act button is setup like this

int ACTpin = 4;

and then this in the setup. Same as a button press. I saw a different way to set it up with #define SFX_ACT 4 but I didn't know how exactly that worked so I didn't use it.

pinMode(ACTpin, INPUT_PULLUP);


And then called up with

( LOW == digitalRead( ACTpin ) )



when hooked into the rails it gave me 3.05v without press and .06v with press so approx the same.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

Try using INPUT instead of INPUT_PULLUP.

Pins 3 and 4 are used by the bootloader's USB connection, so they already have pull-ups on the board.

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

Hi Mike!

I changed that to input and then also changed the blaster sound to a longer file and it worked!

My only issue is that the time it takes to read a button press is longer than the original sound so that's why it was never cutting it off.

I tried taking the delays out and it still cuts off sound on the longer wav but not on the short one. The short one is about 1.5 seconds long. Am I out of luck for getting less delay with this setup. If so is there any other microcontroller that would give me less delay on the button press or is the issue the speed that the SFX board can read the signals?

thanks again for all your help! The concept is at least working now. :)

Here is my current code that is working.

Code: Select all

//// the setup routine runs once when you press reset:

#include <SoftwareSerial.h>
#include "Adafruit_Soundboard.h"

#define SFX_TX 1
#define SFX_RX 0
#define SFX_RST 3

SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);

Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);

int buttonBpin = 2;
int ACTpin = 4;
uint8_t trigger = 0;




void setup() {

pinMode(SFX_TX, OUTPUT);
pinMode(buttonBpin, INPUT_PULLUP);
pinMode(ACTpin, INPUT);

ss.begin(9600);

sfx.playTrack("T01     WAV");
delay(1000);


}






void loop(){

    trigger = ( trigger << 1 ) + digitalRead( buttonBpin );
    
    if ( 0x80 == trigger ) {                    //  new button press
        if ( digitalRead(ACTpin) == LOW ) {   //  if a sound is playing
            sfx.stop();                         //  stop it 
                                              //  give it time to stop
        }
        sfx.playTrack( "T00     WAV" );              //  play a new sound
    }
 
}

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

You need at least a few milliseconds of delay on each pass through loop().

Buttons don't make simple on/off connections. There's a period of a few milliseconds where the contacts aren't quite touching but are really close together, and during those times the current will arc. The arcs don't carry enough energy to do any damage (at least not until you open and close the switch a few million times), but they produce bursts of random high-speed on/off noise called 'contact bounce'.

If you read the pin value with a microcontroller taking readings as fast as it can, you'll see hundreds of button presses instead of just one.

The "shift the bits, add a new reading, wait for a few milliseconds" code I posted reduces the noise to a single press by waiting for the last high/low transition to occur (a 1 followed by seven 0s). It only works if there are no high/low periods in the noise that last long enough to produce that pattern though. If you collect readings as fast as possible, you'll get dozens of "got a button press, no wait, cancel that, we got another" events within a few milliseconds.

Contact bounce usually lasts between 5ms and 50ms, so at 10ms per pass through loop() the code I posted should detect the end of the bounce within 80ms. Humans generally can't identify "this came first, that came second" events with less than 100ms between the two events, so it should feel like an instant response.

If it's taking more than a second to identify a button press, the cause is probably something outside the code.

How are you powering the circuit?

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

I added the delays back in. But it is having the same issue.

I am powering it with a PKCELL LP 402025 3.7v 150mAh lipo from adafruit. I plan to get a 500mAh before finishing the build but was waiting to see if I can get the code to work.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Airsoft Soundboard

Post by adafruit_support_mike »

The small LiPo could be related to the problems. It's only designed for continuous current of about 30mA.

Events that draw more current, like the speakers running, could draw enough current to make the LiPo voltage drop.

Refresh my memory please: does the problem occur when you power the circuit from a USB cable too, or only when the circuit is running from battery power?

User avatar
connorfilm
 
Posts: 43
Joined: Thu Mar 12, 2015 1:49 am

Re: Airsoft Soundboard

Post by connorfilm »

when I power the sfx board off a usb battery brick or connect it to my computer it just clicks and won't go through the startup.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”