Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 16 of 16

Thread: bug with piezo and wav file

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19

    bug with piezo and wav file

    Hello. I am testing HelloDrum library with Piezos sensors. But it has a bug when using wav files. When you quickly type on a Piezo the wav file freeze a few seconds before working again. It's a bug that the creator of it has recognized. He is using a teensy 3.2 and he hoped it would work with version 4.0 but the bug is still present. Has anyone ever used a wav file and piezos? I do not see where the problem is.
    Thanks

    Code:
    ​/*​
    ​****EXAMPLE*-*Teensy*Simple*WAV*Player*with*Teensy*Audio*Audio*Shield​
    ​
    ​****Library*:*https://github.com/RyoKosaka/HelloDrum-arduino-Library​
    ​****Blog*:*https://open-e-drums.tumblr.com/​
    ​*/​
    
    ​/*​*NOTICE​
    ​
    ​****This*code*is*unstable!​
    ​
    ​*/​
    
    #​include​*​<​Audio.h​>​
    #​include​*​<​Wire.h​>​
    #​include​*​<​SPI.h​>​
    #​include​*​<​SD.h​>​
    #​include​*​<​SerialFlash.h​>​
    #​include​*​<​hellodrum.h​>​
    
    ​int​*PAD1[​5​]*=*{
    ****​100​,*​//​sensitivity*(1-100)​
    ****​10​,**​//​threshold*(1-100)​
    ****​30​,**​//​scan*time*(1-)​
    ****​10​,**​//​mask*time*(1-)​
    ****​38​***​//​note*(0-127)​
    };
    
    ​//​name*your*pad​
    HelloDrum*​pad1​(​7​);*​//​connect*to*A7(21)*pin​
    
    ​//​Teensy*Audio​
    AudioPlaySdWav*playSdWav1;
    AudioPlaySdWav*playSdWav2;
    AudioPlaySdWav*playSdWav3;
    AudioPlaySdWav*playSdWav4;
    AudioPlaySdWav*playSdWav5;
    AudioPlaySdWav*playSdWav6;
    AudioPlaySdWav*playSdWav7;
    AudioPlaySdWav*playSdWav8;
    AudioPlaySdWav*playSdWav9;
    AudioPlaySdWav*playSdWav10;
    AudioPlaySdWav*playSdWav11;
    AudioPlaySdWav*playSdWav12;
    AudioPlaySdWav*playSdWav13;
    AudioPlaySdWav*playSdWav14;
    AudioPlaySdWav*playSdWav15;
    AudioPlaySdWav*playSdWav16;
    AudioMixer4*mixerLeft1;
    AudioMixer4*mixerLeft2;
    AudioMixer4*mixerLeft3;
    AudioMixer4*mixerLeft4;
    AudioMixer4*mixerRight1;
    AudioMixer4*mixerRight2;
    AudioMixer4*mixerRight3;
    AudioMixer4*mixerRight4;
    AudioMixer4*mixerLeftMaster;
    AudioMixer4*mixerRightMaster;
    AudioOutputI2S*i2s1;
    AudioConnection*​patchCord1​(playSdWav14,*​0​,*mixerLeft4,*​1​);
    AudioConnection*​patchCord2​(playSdWav14,*​1​,*mixerRight4,*​1​);
    AudioConnection*​patchCord3​(playSdWav10,*​0​,*mixerLeft3,*​1​);
    AudioConnection*​patchCord4​(playSdWav10,*​1​,*mixerRight3,*​1​);
    AudioConnection*​patchCord5​(playSdWav15,*​0​,*mixerLeft4,*​2​);
    AudioConnection*​patchCord6​(playSdWav15,*​1​,*mixerRight4,*​2​);
    AudioConnection*​patchCord7​(playSdWav11,*​0​,*mixerLeft3,*​2​);
    AudioConnection*​patchCord8​(playSdWav11,*​1​,*mixerRight3,*​2​);
    AudioConnection*​patchCord9​(playSdWav13,*​0​,*mixerLeft4,*​0​);
    AudioConnection*​patchCord10​(playSdWav13,*​1​,*mixerRight4,*​0​);
    AudioConnection*​patchCord11​(playSdWav9,*​0​,*mixerLeft3,*​0​);
    AudioConnection*​patchCord12​(playSdWav9,*​1​,*mixerRight3,*​0​);
    AudioConnection*​patchCord13​(playSdWav6,*​0​,*mixerLeft2,*​1​);
    AudioConnection*​patchCord14​(playSdWav6,*​1​,*mixerRight2,*​1​);
    AudioConnection*​patchCord15​(playSdWav2,*​0​,*mixerLeft1,*​1​);
    AudioConnection*​patchCord16​(playSdWav2,*​1​,*mixerRight1,*​1​);
    AudioConnection*​patchCord17​(playSdWav16,*​0​,*mixerLeft4,*​3​);
    AudioConnection*​patchCord18​(playSdWav16,*​1​,*mixerRight4,*​3​);
    AudioConnection*​patchCord19​(playSdWav12,*​0​,*mixerLeft3,*​3​);
    AudioConnection*​patchCord20​(playSdWav12,*​1​,*mixerRight3,*​3​);
    AudioConnection*​patchCord21​(playSdWav7,*​0​,*mixerLeft2,*​2​);
    AudioConnection*​patchCord22​(playSdWav7,*​1​,*mixerRight2,*​2​);
    AudioConnection*​patchCord23​(playSdWav3,*​0​,*mixerLeft1,*​2​);
    AudioConnection*​patchCord24​(playSdWav3,*​1​,*mixerRight1,*​2​);
    AudioConnection*​patchCord25​(playSdWav5,*​0​,*mixerLeft2,*​0​);
    AudioConnection*​patchCord26​(playSdWav5,*​1​,*mixerRight2,*​0​);
    AudioConnection*​patchCord27​(playSdWav1,*​0​,*mixerLeft1,*​0​);
    AudioConnection*​patchCord28​(playSdWav1,*​1​,*mixerRight1,*​0​);
    AudioConnection*​patchCord29​(playSdWav8,*​0​,*mixerLeft2,*​3​);
    AudioConnection*​patchCord30​(playSdWav8,*​1​,*mixerRight2,*​3​);
    AudioConnection*​patchCord31​(playSdWav4,*​0​,*mixerLeft1,*​3​);
    AudioConnection*​patchCord32​(playSdWav4,*​1​,*mixerRight1,*​3​);
    AudioConnection*​patchCord33​(mixerLeft4,*​0​,*mixerLeftMaster,*​3​);
    AudioConnection*​patchCord34​(mixerLeft2,*​0​,*mixerLeftMaster,*​1​);
    AudioConnection*​patchCord35​(mixerLeft1,*​0​,*mixerLeftMaster,*​0​);
    AudioConnection*​patchCord36​(mixerLeft3,*​0​,*mixerLeftMaster,*​2​);
    AudioConnection*​patchCord37​(mixerRight2,*​0​,*mixerRightMaster,*​1​);
    AudioConnection*​patchCord38​(mixerRight3,*​0​,*mixerRightMaster,*​2​);
    AudioConnection*​patchCord39​(mixerRight4,*​0​,*mixerRightMaster,*​3​);
    AudioConnection*​patchCord40​(mixerRight1,*​0​,*mixerRightMaster,*​0​);
    AudioConnection*​patchCord41​(mixerLeftMaster,*​0​,*i2s1,*​0​);
    AudioConnection*​patchCord42​(mixerRightMaster,*​0​,*i2s1,*​1​);
    AudioControlSGTL5000*sgtl5000_1;
    
    ​//​*Use*these*with*the*Teensy*Audio*Shield​
    #​define​*​SDCARD_CS_PIN​*​10​
    #​define​*​SDCARD_MOSI_PIN​*​7​
    #​define​*​SDCARD_SCK_PIN​*​14​
    
    ​//​*Use*these*with*the*Teensy*3.5*&*3.6*SD*card​
    ​//​#define*SDCARD_CS_PIN****BUILTIN_SDCARD​
    ​//​#define*SDCARD_MOSI_PIN**11**//*not*actually*used​
    ​//​#define*SDCARD_SCK_PIN***13**//*not*actually*used​
    
    ​//​*Use*these*for*the*SD+Wiz820*or*other*adaptors​
    ​//​#define*SDCARD_CS_PIN****4​
    ​//​#define*SDCARD_MOSI_PIN**11​
    ​//​#define*SDCARD_SCK_PIN***13​
    
    ​void​*​setup​()
    {
    ****​//​*Audio*connections*require*memory*to*work.**For*more​
    ****​//​*detailed*information,*see*the*MemoryAndCpuUsage*example​
    ****​AudioMemory​(​20​);
    
    ****​//​*Comment*these*out*if*not*using*the*audio*adaptor*board.​
    ****​//​*This*may*wait*forever*if*the*SDA*&*SCL*pins*lack​
    ****​//​*pullup*resistors​
    ****sgtl5000_1.​enable​();
    ****sgtl5000_1.​volume​(​1.0​);
    
    ****SPI.​setMOSI​(SDCARD_MOSI_PIN);
    ****SPI.​setSCK​(SDCARD_SCK_PIN);
    ****​if​*(!(SD.​begin​(SDCARD_CS_PIN)))
    ****{
    ********​//​*stop*here,*but*print*a*message*repetitively​
    ********​while​*(​1​)
    ********{
    ************Serial.​println​(​"​Unable*to*access*the*SD*card​"​);
    ************​delay​(​500​);
    ********}
    ****}
    
    ****​//​mixer*gain​
    ****mixerLeft1.​gain​(​0​,​0.25​);
    ****mixerLeft1.​gain​(​1​,​0.25​);
    ****mixerLeft1.​gain​(​2​,​0.25​);
    ****mixerLeft1.​gain​(​3​,​0.25​);
    ****mixerLeft2.​gain​(​0​,​0.25​);
    ****mixerLeft2.​gain​(​1​,​0.25​);
    ****mixerLeft2.​gain​(​2​,​0.25​);
    ****mixerLeft2.​gain​(​3​,​0.25​);
    ****mixerLeft3.​gain​(​0​,​0.25​);
    ****mixerLeft3.​gain​(​1​,​0.25​);
    ****mixerLeft3.​gain​(​2​,​0.25​);
    ****mixerLeft3.​gain​(​3​,​0.25​);
    ****mixerLeft4.​gain​(​0​,​0.25​);
    ****mixerLeft4.​gain​(​1​,​0.25​);
    ****mixerLeft4.​gain​(​2​,​0.25​);
    ****mixerLeft4.​gain​(​3​,​0.25​);
    ****mixerRight1.​gain​(​0​,​0.25​);
    ****mixerRight1.​gain​(​1​,​0.25​);
    ****mixerRight1.​gain​(​2​,​0.25​);
    ****mixerRight1.​gain​(​3​,​0.25​);
    ****mixerRight2.​gain​(​0​,​0.25​);
    ****mixerRight2.​gain​(​1​,​0.25​);
    ****mixerRight2.​gain​(​2​,​0.25​);
    ****mixerRight2.​gain​(​3​,​0.25​);
    ****mixerRight3.​gain​(​0​,​0.25​);
    ****mixerRight3.​gain​(​1​,​0.25​);
    ****mixerRight3.​gain​(​2​,​0.25​);
    ****mixerRight3.​gain​(​3​,​0.25​);
    ****mixerRight4.​gain​(​0​,​0.25​);
    ****mixerRight4.​gain​(​1​,​0.25​);
    ****mixerRight4.​gain​(​2​,​0.25​);
    ****mixerRight4.​gain​(​3​,​0.25​);
    ****mixerLeftMaster.​gain​(​0​,​0.25​);
    ****mixerLeftMaster.​gain​(​1​,​0.25​);
    ****mixerLeftMaster.​gain​(​2​,​0.25​);
    ****mixerLeftMaster.​gain​(​3​,​0.25​);
    ****mixerRightMaster.​gain​(​0​,​0.25​);
    ****mixerRightMaster.​gain​(​1​,​0.25​);
    ****mixerRightMaster.​gain​(​2​,​0.25​);
    ****mixerRightMaster.​gain​(​3​,​0.25​);
    }
    
    ​void​*​playFile​(​const​*​char​**filename)
    {
    ****​if​*(playSdWav1.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav1.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav2.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav2.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav3.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav3.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav4.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav4.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav5.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav5.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav6.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav6.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav7.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav7.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav8.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav8.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav9.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav9.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav10.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav10.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav11.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav11.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav12.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav12.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav13.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav13.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav14.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav14.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav15.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav15.​play​(filename);
    ****}
    ****​else​*​if​*(playSdWav16.​isPlaying​()*==*​false​)
    ****{
    ********playSdWav16.​play​(filename);
    ****}
    }
    
    ​void​*​loop​()
    {
    ****pad1.​singlePiezo​(PAD1[​0​],*PAD1[​1​],*PAD1[​2​],*PAD1[​3​]);
    
    ****​if​*(pad1.​hit​*==*​true​)
    ****{
    ********​if​*(pad1.​velocity​*<*​25​)
    ********{
    ************​playFile​(​"​snare1.wav​"​);
    ********}
    ********​if​*(pad1.​velocity​*>=*​25​*&&*pad1.​velocity​*<*​50​)
    ********{
    ************​playFile​(​"​snare2.wav​"​);
    ********}
    ********​if​*(pad1.​velocity​*>=*​50​*&&*pad1.​velocity​*<*​75​)
    ********{
    ************​playFile​(​"​snare3.wav​"​);
    ********}
    ********​if​*(pad1.​velocity​*>=*​75​*&&*pad1.​velocity​*<*​100​)
    ********{
    ************​playFile​(​"​snare4.wav​"​);
    ********}
    ********​if​*(pad1.​velocity​*>=*​100​)
    ********{
    ************​playFile​(​"​snare5.wav​"​);
    ********}
    ****}
    }

  2. #2
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    Hello, I tested with several diagrams and other audio files with lower frequencies. Always the same result. Does anyone have a working example with a wav and a piezo? Is it only me who have this bug? Is this an already known problem?
    I specify that this problem is also encountered with teensy 4.0 and 3. 2. Thanks

    Click image for larger version. 

Name:	7dbd52ac-9f9d-4eba-bff0-dc584ac1a246.jpg 
Views:	5 
Size:	105.4 KB 
ID:	19912
    Last edited by SPIRIT; 04-30-2020 at 08:46 PM.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    I also did the test with buttons and it works perfectly. I think there are a few things that don't work with piezos and wav. This is why I ask if others have the same bug.

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,041
    Please add sourcecode that can be used. Remove the stars.

    If it works with buttons, the problem is the connection with the piezo, not the code.

  5. #5
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    Sorry, I hadn't seen for the stars in the code. The person who wrote the code says it also has a problem with wav files. He thought that the Teensy 4.0 version would correct the problem. I don't think there is a problem with my piezos.
    Code:
    /*
        EXAMPLE - Teensy Simple WAV Player with Teensy Audio Audio Shield
        Library : https://github.com/RyoKosaka/HelloDrum-arduino-Library
        Blog : https://open-e-drums.tumblr.com/
    */
    
    /* NOTICE
        This code is unstable!
    */
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <hellodrum.h>
    
    int PAD1[5] = {
        100, //sensitivity (1-100)
        10,  //threshold (1-100)
        30,  //scan time (1-)
        10,  //mask time (1-)
        38   //note (0-127)
    };
    
    //name your pad
    HelloDrum pad1(7); //connect to A7(21) pin
    
    //Teensy Audio
    AudioPlaySdWav playSdWav1;
    AudioPlaySdWav playSdWav2;
    AudioPlaySdWav playSdWav3;
    AudioPlaySdWav playSdWav4;
    AudioPlaySdWav playSdWav5;
    AudioPlaySdWav playSdWav6;
    AudioPlaySdWav playSdWav7;
    AudioPlaySdWav playSdWav8;
    AudioPlaySdWav playSdWav9;
    AudioPlaySdWav playSdWav10;
    AudioPlaySdWav playSdWav11;
    AudioPlaySdWav playSdWav12;
    AudioPlaySdWav playSdWav13;
    AudioPlaySdWav playSdWav14;
    AudioPlaySdWav playSdWav15;
    AudioPlaySdWav playSdWav16;
    AudioMixer4 mixerLeft1;
    AudioMixer4 mixerLeft2;
    AudioMixer4 mixerLeft3;
    AudioMixer4 mixerLeft4;
    AudioMixer4 mixerRight1;
    AudioMixer4 mixerRight2;
    AudioMixer4 mixerRight3;
    AudioMixer4 mixerRight4;
    AudioMixer4 mixerLeftMaster;
    AudioMixer4 mixerRightMaster;
    AudioOutputI2S i2s1;
    AudioConnection patchCord1(playSdWav14, 0, mixerLeft4, 1);
    AudioConnection patchCord2(playSdWav14, 1, mixerRight4, 1);
    AudioConnection patchCord3(playSdWav10, 0, mixerLeft3, 1);
    AudioConnection patchCord4(playSdWav10, 1, mixerRight3, 1);
    AudioConnection patchCord5(playSdWav15, 0, mixerLeft4, 2);
    AudioConnection patchCord6(playSdWav15, 1, mixerRight4, 2);
    AudioConnection patchCord7(playSdWav11, 0, mixerLeft3, 2);
    AudioConnection patchCord8(playSdWav11, 1, mixerRight3, 2);
    AudioConnection patchCord9(playSdWav13, 0, mixerLeft4, 0);
    AudioConnection patchCord10(playSdWav13, 1, mixerRight4, 0);
    AudioConnection patchCord11(playSdWav9, 0, mixerLeft3, 0);
    AudioConnection patchCord12(playSdWav9, 1, mixerRight3, 0);
    AudioConnection patchCord13(playSdWav6, 0, mixerLeft2, 1);
    AudioConnection patchCord14(playSdWav6, 1, mixerRight2, 1);
    AudioConnection patchCord15(playSdWav2, 0, mixerLeft1, 1);
    AudioConnection patchCord16(playSdWav2, 1, mixerRight1, 1);
    AudioConnection patchCord17(playSdWav16, 0, mixerLeft4, 3);
    AudioConnection patchCord18(playSdWav16, 1, mixerRight4, 3);
    AudioConnection patchCord19(playSdWav12, 0, mixerLeft3, 3);
    AudioConnection patchCord20(playSdWav12, 1, mixerRight3, 3);
    AudioConnection patchCord21(playSdWav7, 0, mixerLeft2, 2);
    AudioConnection patchCord22(playSdWav7, 1, mixerRight2, 2);
    AudioConnection patchCord23(playSdWav3, 0, mixerLeft1, 2);
    AudioConnection patchCord24(playSdWav3, 1, mixerRight1, 2);
    AudioConnection patchCord25(playSdWav5, 0, mixerLeft2, 0);
    AudioConnection patchCord26(playSdWav5, 1, mixerRight2, 0);
    AudioConnection patchCord27(playSdWav1, 0, mixerLeft1, 0);
    AudioConnection patchCord28(playSdWav1, 1, mixerRight1, 0);
    AudioConnection patchCord29(playSdWav8, 0, mixerLeft2, 3);
    AudioConnection patchCord30(playSdWav8, 1, mixerRight2, 3);
    AudioConnection patchCord31(playSdWav4, 0, mixerLeft1, 3);
    AudioConnection patchCord32(playSdWav4, 1, mixerRight1, 3);
    AudioConnection patchCord33(mixerLeft4, 0, mixerLeftMaster, 3);
    AudioConnection patchCord34(mixerLeft2, 0, mixerLeftMaster, 1);
    AudioConnection patchCord35(mixerLeft1, 0, mixerLeftMaster, 0);
    AudioConnection patchCord36(mixerLeft3, 0, mixerLeftMaster, 2);
    AudioConnection patchCord37(mixerRight2, 0, mixerRightMaster, 1);
    AudioConnection patchCord38(mixerRight3, 0, mixerRightMaster, 2);
    AudioConnection patchCord39(mixerRight4, 0, mixerRightMaster, 3);
    AudioConnection patchCord40(mixerRight1, 0, mixerRightMaster, 0);
    AudioConnection patchCord41(mixerLeftMaster, 0, i2s1, 0);
    AudioConnection patchCord42(mixerRightMaster, 0, i2s1, 1);
    AudioControlSGTL5000 sgtl5000_1;
    
    // Use these with the Teensy Audio Shield
    #define SDCARD_CS_PIN 10
    #define SDCARD_MOSI_PIN 7
    #define SDCARD_SCK_PIN 14
    
    // Use these with the Teensy 3.5 & 3.6 SD card
    //#define SDCARD_CS_PIN    BUILTIN_SDCARD
    //#define SDCARD_MOSI_PIN  11  // not actually used
    //#define SDCARD_SCK_PIN   13  // not actually used
    
    // Use these for the SD+Wiz820 or other adaptors
    //#define SDCARD_CS_PIN    4
    //#define SDCARD_MOSI_PIN  11
    //#define SDCARD_SCK_PIN   13
    
    void setup()
    {
        // Audio connections require memory to work.  For more
        // detailed information, see the MemoryAndCpuUsage example
        AudioMemory(20);
    
        // Comment these out if not using the audio adaptor board.
        // This may wait forever if the SDA & SCL pins lack
        // pullup resistors
        sgtl5000_1.enable();
        sgtl5000_1.volume(1.0);
    
        SPI.setMOSI(SDCARD_MOSI_PIN);
        SPI.setSCK(SDCARD_SCK_PIN);
        if (!(SD.begin(SDCARD_CS_PIN)))
        {
            // stop here, but print a message repetitively
            while (1)
            {
                Serial.println("Unable to access the SD card");
                delay(500);
            }
        }
    
        //mixer gain
        mixerLeft1.gain(0,0.25);
        mixerLeft1.gain(1,0.25);
        mixerLeft1.gain(2,0.25);
        mixerLeft1.gain(3,0.25);
        mixerLeft2.gain(0,0.25);
        mixerLeft2.gain(1,0.25);
        mixerLeft2.gain(2,0.25);
        mixerLeft2.gain(3,0.25);
        mixerLeft3.gain(0,0.25);
        mixerLeft3.gain(1,0.25);
        mixerLeft3.gain(2,0.25);
        mixerLeft3.gain(3,0.25);
        mixerLeft4.gain(0,0.25);
        mixerLeft4.gain(1,0.25);
        mixerLeft4.gain(2,0.25);
        mixerLeft4.gain(3,0.25);
        mixerRight1.gain(0,0.25);
        mixerRight1.gain(1,0.25);
        mixerRight1.gain(2,0.25);
        mixerRight1.gain(3,0.25);
        mixerRight2.gain(0,0.25);
        mixerRight2.gain(1,0.25);
        mixerRight2.gain(2,0.25);
        mixerRight2.gain(3,0.25);
        mixerRight3.gain(0,0.25);
        mixerRight3.gain(1,0.25);
        mixerRight3.gain(2,0.25);
        mixerRight3.gain(3,0.25);
        mixerRight4.gain(0,0.25);
        mixerRight4.gain(1,0.25);
        mixerRight4.gain(2,0.25);
        mixerRight4.gain(3,0.25);
        mixerLeftMaster.gain(0,0.25);
        mixerLeftMaster.gain(1,0.25);
        mixerLeftMaster.gain(2,0.25);
        mixerLeftMaster.gain(3,0.25);
        mixerRightMaster.gain(0,0.25);
        mixerRightMaster.gain(1,0.25);
        mixerRightMaster.gain(2,0.25);
        mixerRightMaster.gain(3,0.25);
    }
    
    void playFile(const char *filename)
    {
        if (playSdWav1.isPlaying() == false)
        {
            playSdWav1.play(filename);
        }
        else if (playSdWav2.isPlaying() == false)
        {
            playSdWav2.play(filename);
        }
        else if (playSdWav3.isPlaying() == false)
        {
            playSdWav3.play(filename);
        }
        else if (playSdWav4.isPlaying() == false)
        {
            playSdWav4.play(filename);
        }
        else if (playSdWav5.isPlaying() == false)
        {
            playSdWav5.play(filename);
        }
        else if (playSdWav6.isPlaying() == false)
        {
            playSdWav6.play(filename);
        }
        else if (playSdWav7.isPlaying() == false)
        {
            playSdWav7.play(filename);
        }
        else if (playSdWav8.isPlaying() == false)
        {
            playSdWav8.play(filename);
        }
        else if (playSdWav9.isPlaying() == false)
        {
            playSdWav9.play(filename);
        }
        else if (playSdWav10.isPlaying() == false)
        {
            playSdWav10.play(filename);
        }
        else if (playSdWav11.isPlaying() == false)
        {
            playSdWav11.play(filename);
        }
        else if (playSdWav12.isPlaying() == false)
        {
            playSdWav12.play(filename);
        }
        else if (playSdWav13.isPlaying() == false)
        {
            playSdWav13.play(filename);
        }
        else if (playSdWav14.isPlaying() == false)
        {
            playSdWav14.play(filename);
        }
        else if (playSdWav15.isPlaying() == false)
        {
            playSdWav15.play(filename);
        }
        else if (playSdWav16.isPlaying() == false)
        {
            playSdWav16.play(filename);
        }
    }
    
    void loop()
    {
        pad1.singlePiezo(PAD1[0], PAD1[1], PAD1[2], PAD1[3]);
    
        if (pad1.hit == true)
        {
            if (pad1.velocity < 25)
            {
                playFile("snare1.wav");
            }
            if (pad1.velocity >= 25 && pad1.velocity < 50)
            {
                playFile("snare2.wav");
            }
            if (pad1.velocity >= 50 && pad1.velocity < 75)
            {
                playFile("snare3.wav");
            }
            if (pad1.velocity >= 75 && pad1.velocity < 100)
            {
                playFile("snare4.wav");
            }
            if (pad1.velocity >= 100)
            {
                playFile("snare5.wav");
            }
        }
    }

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,041
    What means "a problem". Are they played backwards?
    AudioMemory(20);
    seems a little low

  7. #7
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    It works fine, but sometimes it becomes unusable because it sometimes becomes unresponsive or crashes with the worst sound. It is easy to crash if you hit repeatedly.

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,041
    You said this does happen with piezos only, not with buttons.
    Do you use the same code for the buttons? If yes, still, the piezos are the problem. maybe voltages spikes?
    Can you post the missing hellodrum.h file?

    too late here now. good night.

  9. #9
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    Thanks for your help. I don't use the same code for buttons and piezos. I use the examples available with the Teensy Audio library. Here is the helloDrum.h file and a link to the entire library for more details.
    https://github.com/RyoKosaka/HelloDrum-arduino-Library

    Code:
    /*
      " HELLO DRUM LIBRARY" Ver.0.7.5
      
      by Ryo Kosaka
      GitHUb : https://github.com/RyoKosaka/HelloDrum-arduino-Library
      Blog : https://open-e-drums.tumblr.com/
    */
    
    #ifndef HelloDrum_h
    #define HelloDrum_h
    
    #include "Arduino.h"
    
    #ifdef ESP32
    #include "EEPROM_ESP.h"
    #else
    #include "EEPROM.h"
    #endif
    
    static char *item[] = {
        "SENSITIVITY", //0 0
        "THRESHOLD",   //1 1
        "SCAN TIME",   //2 2
        "MASK TIME",   //3 3
        "CURVE TYPE",  //6 4
        "NOTE",        //7 5
    };
    
    static char *itemD[] = {
        "SENSITIVITY", //0
        "THRESHOLD",   //1
        "SCAN TIME",   //2
        "MASK TIME",   //3
        "RIM SENS",    //4
        "RIM THRE",    //5
        "CURVE TYPE",  //6
        "NOTE HEAD",   //7
        "NOTE RIM",    //8
        "NOTE CROSS",  //9
    };
    
    static char *itemCY2[] = {
        "SENSITIVITY", //0 0
        "THRESHOLD",   //1 1
        "SCAN TIME",   //2 2
        "MASK TIME",   //3 3
        "EDGE THRE",   //4 4
        "CURVE TYPE",  //6 5
        "NOTE BOW",    //7 6
        "NOTE EDGE",   //8 7
    };
    
    static char *itemCY3[] = {
        "SENSITIVITY", //0
        "THRESHOLD",   //1
        "SCAN TIME",   //2
        "MASK TIME",   //3
        "EDGE THRE",   //4
        "CUP THRE",    //5
        "CURVE TYPE",  //6
        "NOTE BOW",    //7
        "NOTE EDGE",   //8
        "NOTE CUP",    //9
    };
    
    static char *itemHH[] = {
        "SENSITIVITY", //0 0
        "THRESHOLD",   //1 1
        "SCAN TIME",   //2 2
        "MASK TIME",   //3 3
        "CURVE TYPE",  //6 4
        "NOTE OPEN",   //7 5
        "NOTE CLOSE",  //8 6
    };
    
    static char *itemHH2[] = {
        "SENSITIVITY", //0 0
        "THRESHOLD",   //1 1
        "SCAN TIME",   //2 2
        "MASK TIME",   //3 3
        "EDGE THRE",   //4 4
        "CURVE TYPE",  //6 5
        "NOTE OPEN",   //7 6
        "NOTE CLOSE",  //8 7
    };
    
    static char *itemHHC[] = {
        "SENSITIVITY",  //0 0
        "THRESHOLD",    //1 1
        "SCAN START",   //2 2
        "SCAN END",     //3 3
        "PEDAL SENS",   //4 4
        "CURVE TYPE",   //6 5
        "NOTE PEDAL",   //7 6
        "NOTE OPEN E",  //8 7
        "NOTE CLOSE E", //9 8
    };
    
    static char *showInstrument[] = {
        "Pad 1",
        "Pad 2",
        "Pad 3",
        "Pad 4",
        "Pad 5",
        "Pad 6",
        "Pad 7",
        "Pad 8",
        "Pad 9",
        "Pad 10",
        "Pad 11",
        "Pad 12",
        "Pad 13",
        "Pad 14",
        "Pad 15",
        "Pad 16",
    };
    
    static bool push;
    static bool showLCD;
    static bool showFlag;
    
    static byte showVelocity;
    static byte nameIndex;
    static byte nameIndexMax;
    static byte showValue = 0;
    static byte padIndex = 0;
    static byte muxIndex = 0;
    static byte HHCnum = 255;
    static byte HHnum = 255;
    static byte HH2num = 255;
    static byte CY2num = 255;
    static byte CY3num = 255;
    static byte Dnum = 255;
    static byte Snum = 255;
    
    static bool edit;
    static bool editCheck;
    static bool editdone;
    static bool change;
    static byte itemNumber;
    static byte itemNumberShow;
    static bool buttonState;
    static bool buttonState_set;
    static bool button_set;
    static bool button_up;
    static bool button_down;
    static bool button_next;
    static bool button_back;
    static byte UP[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    
    #ifdef __AVR_ATmega328P__
    static int rawValue[16]; //2 * 8chanel Mux
    #else
    static int rawValue[64]; //8 * 8chanel Mux
    #endif
    
    class HelloDrum
    {
    public:
      HelloDrum(byte pin1, byte pin2);
      HelloDrum(byte pin1);
    
      void singlePiezoMUX(byte sens, byte thre, byte scan, byte mask);
      void singlePiezoMUX();
      void dualPiezoMUX(byte sens, byte thre, byte scan, byte mask, byte rimSens, byte rimThre);
      void dualPiezoMUX();
      void HHMUX(byte sens, byte thre, byte scan, byte mask);
      void HHMUX();
      void HH2zoneMUX(byte sens, byte thre, byte scan, byte mask, byte edgeThre);
      void HH2zoneMUX();
      void cymbal3zoneMUX(byte sens, byte thre, byte scan, byte mask, byte edgeThre, byte cupThre);
      void cymbal3zoneMUX();
      void cymbal2zoneMUX(byte sens, byte thre, byte scan, byte mask, byte edgeThre);
      void cymbal2zoneMUX();
      void TCRT5000MUX(byte sens, byte thre1, byte thre2, byte scan);
      void TCRT5000MUX();
      void FSRMUX(byte sens, byte thre, byte scanStart, byte scanEnd, byte pedalSens);
      void FSRMUX();
      void hihatControlMUX(byte sens, byte thre, byte scanStart, byte scanEnd, byte pedalSens);
      void hihatControlMUX();
    
      void singlePiezo(byte sens, byte thre, byte scan, byte mask);
      void singlePiezo();
      void dualPiezo(byte sens, byte thre, byte scan, byte mask, byte rimSens, byte rimThre);
      void dualPiezo();
      void HH(byte sens, byte thre, byte scan, byte mask);
      void HH();
      void HH2zone(byte sens, byte thre, byte scan, byte mask, byte edgeThre);
      void HH2zone();
      void cymbal3zone(byte sens, byte thre, byte scan, byte mask, byte edgeThre, byte cupThre);
      void cymbal3zone();
      void cymbal2zone(byte sens, byte thre, byte scan, byte mask, byte edgeThre);
      void cymbal2zone();
      void TCRT5000(byte sens, byte thre1, byte thre2, byte scan);
      void TCRT5000();
      void FSR(byte sens, byte thre, byte scanStart, byte scanEnd, byte pedalSens);
      void FSR();
      void hihatControl(byte sens, byte thre, byte scanStart, byte scanEnd, byte pedalSens);
      void hihatControl();
    
      void setCurve(byte curveType);
    
      void settingName(char *instrumentName);
      void settingEnable();
    
      void loadMemory();
      void initMemory();
    
      int velocity;
      int velocityRim;
      int velocityCup;
      byte pedalCC;
    
      //  int exValue;
      byte exTCRT = 0;
      byte exFSR = 0;
      bool hit;
      bool openHH = false;
      bool closeHH = false;
      bool hitRim;
      bool hitCup;
      bool choke;
      bool sensorFlag;
      bool moving;
      bool pedalVelocityFlag = false;
      bool pedalFlag = true;
      bool settingHHC = false;
      bool chokeFlag;
    
      char *GetItem(byte i);
    
      byte value;
      byte padNum;
    
      byte note;
      byte noteRim;
      byte noteCup;
      byte noteEdge;
      byte noteOpen;
      byte noteClose;
      byte noteOpenEdge;
      byte noteCloseEdge;
      byte noteCross;
      byte threshold1;
      byte threshold2;
      byte scantime;
      byte masktime;
      byte sensitivity;
      byte curvetype;
      byte rimThreshold;
      byte rimSensitivity;
    
    private:
      byte pin_1;
      byte pin_2;
      int piezoValue;
      int RimPiezoValue;
      int sensorValue;
      int TCRT;
      int fsr;
      int firstSensorValue;
      int lastSensorValue;
      int loopTimes = 0;
      unsigned long time_hit;
      unsigned long time_end;
      unsigned long time_choke;
      unsigned long time_hit_pedal_1;
      unsigned long time_hit_pedal_2;
    
      void singlePiezoSensing(byte sens, byte thre, byte scanTime, byte maskTime);
      void dualPiezoSensing(byte sens, byte thre, byte scanTime, byte maskTime, byte rimSens, byte rimThre);
      void cymbal2zoneSensing(byte sens, byte thre, byte scanTime, byte maskTime, byte edgeThre);
      void cymbal3zoneSensing(byte sens, byte thre, byte scanTime, byte maskTime, byte edgeThre, byte cupThre);
      void TCRT5000Sensing(byte sens, byte thre1, byte thre2, byte scanTime);
      void FSRSensing(byte sens, byte thre, byte scanStart, byte scanEnd, byte pedalSens);
      int curve(int velocityRaw, int threshold, int sensRaw, byte curveType);
    };
    
    class HelloDrumMUX_4051
    {
    public:
      HelloDrumMUX_4051(byte pin1, byte pin2, byte pin3, byte pinA);
      void scan();
      byte selectPins[3];
      byte muxNum;
    
    private:
      byte pin_1;
      byte pin_2;
      byte pin_3;
      byte pin_A;
    };
    
    class HelloDrumMUX_4067
    {
    public:
      HelloDrumMUX_4067(byte pin1, byte pin2, byte pin3, byte pin4, byte pinA);
      void scan();
      byte selectPins[4];
      byte muxNum;
    
    private:
      byte pin_1;
      byte pin_2;
      byte pin_3;
      byte pin_4;
      byte pin_A;
    };
    
    class HelloDrumButton
    {
    public:
      HelloDrumButton(byte pin1, byte pin2, byte pin3, byte pin4, byte pin5);
    
      void readButtonState();
      void readButton(bool button_set, bool button_up, bool button_down, bool button_next, bool button_back);
    
      byte GetSettingValue();
      byte GetVelocity();
      bool GetEditState();
      bool GetEditdoneState();
      bool GetPushState();
      bool GetDisplayState();
      char *GetPadName();
      char *GetSettingItem();
      char *GetHitPad();
    
      int UPDOWN;
      int NEXTBACK;
    
    private:
      byte pin_1;
      byte pin_2;
      byte pin_3;
      byte pin_4;
      byte pin_5;
    };
    
    class HelloDrumButtonLcdShield
    {
    public:
      HelloDrumButtonLcdShield(byte pin1);
    
      void readButtonState();
      void readButton(bool button_set, bool button_up, bool button_down, bool button_next, bool button_back);
    
      byte GetSettingValue();
      byte GetVelocity();
      bool GetEditState();
      bool GetEditdoneState();
      bool GetPushState();
      bool GetDisplayState();
      char *GetPadName();
      char *GetSettingItem();
      char *GetHitPad();
    
      int UPDOWN;
      int NEXTBACK;
    
    private:
      int buttonValue;
      byte pin_1;
    };
    
    class HelloDrumKnob
    {
    public:
      HelloDrumKnob(byte pin1);
      void read();
      void readMUX();
      int knobValue;
    
    private:
      byte pin_1;
    };
    
    #endif

  10. #10
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    I did a test with a different version without the wav of the SD card but with an h file. So I used the SamplePlayer example from the library that I modified to use it with a Piezo. Just to be sure that it is not an SD card that is corrupted. I always have the same result.
    Has anyone ever tried to use an audio file and a Piezo?

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <hellodrum.h>
    
    int PAD1[5] = {
        100, //sensitivity (1-100)
        60,  //threshold (1-100)
        30,  //scan time (1-)
        10,  //mask time (1-)
        38   //note (0-127)
    };
    
    
    //name your pad
    HelloDrum pad1(A0);
    
    // WAV files converted to code by wav2sketch
    #include "AudioSampleSnare.h"        // http://www.freesound.org/people/KEVOY/sounds/82583/
    
    
    // GUItool: begin automatically generated code
    AudioPlayMemory          sound0;         //xy=636.75,91
    AudioSynthWaveformDc     dc1;            //xy=642,172
    AudioEffectMultiply      multiply1;      //xy=786,133
    AudioMixer4              mix1;           //xy=937.25,161.25
    AudioOutputAnalog        dac;            //xy=1134,185.25
    AudioOutputI2S           headphones;     //xy=1157.75,110
    AudioConnection          patchCord1(sound0, 0, multiply1, 0);
    AudioConnection          patchCord2(dc1, 0, multiply1, 1);
    AudioConnection          patchCord3(multiply1, 0, mix1, 0);
    AudioConnection          patchCord4(mix1, 0, headphones, 0);
    AudioConnection          patchCord5(mix1, 0, headphones, 1);
    AudioConnection          patchCord6(mix1, dac);
    AudioControlSGTL5000     audioShield;    //xy=945.75,252.25
    // GUItool: end automatically generated code
    
    
    
    void setup() {
    
      dc1.amplitude(0);
     
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(50);
    
      // turn on the output
      audioShield.enable();
      audioShield.volume(0.6);
    
      // by default the Teensy 3.1 DAC uses 3.3Vp-p output
      // if your 3.3V power has noise, switching to the
      // internal 1.2V reference can give you a clean signal
      //dac.analogReference(INTERNAL);
    
      // reduce the gain on mixer channels, so more than 1
      // sound can play simultaneously without clipping
      mix1.gain(0, 0.4);
      mix1.gain(1, 0.4);
      mix1.gain(2, 0.4);
      mix1.gain(3, 0.4);
    
    }
    
    
    void loop()
    {
        pad1.singlePiezo(PAD1[0], PAD1[1], PAD1[2], PAD1[3]);
    
        if (pad1.hit == true)
        {
            if (pad1.velocity < 25)
            {
                dc1.amplitude(0.2, 5);
                sound0.play(AudioSampleSnare);
            }
            if (pad1.velocity >= 25 && pad1.velocity < 50)
            {   
                dc1.amplitude(0.4, 5);
                sound0.play(AudioSampleSnare);
            }
            if (pad1.velocity >= 50 && pad1.velocity < 75)
            {
                dc1.amplitude(0.6, 5);
                sound0.play(AudioSampleSnare);
            }
            if (pad1.velocity >= 75 && pad1.velocity <100)
            {
               dc1.amplitude(0.8, 5);
               sound0.play(AudioSampleSnare);
            }
            if (pad1.velocity >= 100)
            {
                dc1.amplitude(1.0, 5);
                sound0.play(AudioSampleSnare);
            }
        }
    }

  11. #11
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    I used the PiezoDrum example in the Teensy library. I deleted the MIDI code and adapted it for the audio. I also deleted the HelloDrum library. I manage to play an audio file with the Piezo but I would like to add velocity to it. What should I change?

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    
    const int analogPin = A0;
    const int thresholdMin = 60;  // minimum reading, avoid noise and false starts
    const int peakTrackMillis = 12;
    const int aftershockMillis = 25; // aftershocks & vibration reject
    
    
    // WAV files converted to code by wav2sketch
    #include "AudioSampleSnare.h"        // http://www.freesound.org/people/KEVOY/sounds/82583/
    
    
    // GUItool: begin automatically generated code
    AudioPlayMemory          sound0;         //xy=636.75,91
    AudioSynthWaveformDc     dc1;            //xy=642,172
    AudioEffectMultiply      multiply1;      //xy=786,133
    AudioMixer4              mix1;           //xy=937.25,161.25
    AudioOutputAnalog        dac;            //xy=1134,185.25
    AudioOutputI2S           headphones;     //xy=1157.75,110
    AudioConnection          patchCord1(sound0, 0, multiply1, 0);
    AudioConnection          patchCord2(dc1, 0, multiply1, 1);
    AudioConnection          patchCord3(multiply1, 0, mix1, 0);
    AudioConnection          patchCord4(mix1, 0, headphones, 0);
    AudioConnection          patchCord5(mix1, 0, headphones, 1);
    AudioConnection          patchCord6(mix1, dac);
    AudioControlSGTL5000     audioShield;    //xy=945.75,252.25
    // GUItool: end automatically generated code
    
    
    
    void setup() {
    
      Serial.begin(115200);
      while (!Serial && millis() < 2500) /* wait for serial monitor */ ;
      Serial.println("Piezo Peak Capture");
    
      dc1.amplitude(0);
     
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(50);
    
      // turn on the output
      audioShield.enable();
      audioShield.volume(0.6);
    
      // by default the Teensy 3.1 DAC uses 3.3Vp-p output
      // if your 3.3V power has noise, switching to the
      // internal 1.2V reference can give you a clean signal
      //dac.analogReference(INTERNAL);
    
      // reduce the gain on mixer channels, so more than 1
      // sound can play simultaneously without clipping
      mix1.gain(0, 0.4);
      mix1.gain(1, 0.4);
      mix1.gain(2, 0.4);
      mix1.gain(3, 0.4);
    
    }
    
    
    void loop()
    {
    
      int piezo = analogRead(analogPin);
      peakDetect(piezo);
    
    
      }
    
      ////////////////////////////////////
    void peakDetect(int voltage) {
      // "static" variables keep their numbers between each run of this function
      static int state;  // 0=idle, 1=looking for peak, 2=ignore aftershocks
      static int peak;   // remember the highest reading
      static elapsedMillis msec; // timer to end states 1 and 2
    
      switch (state) {
        // IDLE state: wait for any reading is above threshold.  Do not set
        // the threshold too low.  You don't want to be too sensitive to slight
        // vibration.
        case 0:
          if (voltage > thresholdMin) {
           Serial.print("begin peak track ");
           Serial.println(voltage);
            peak = voltage;
            msec = 0;
            state = 1;
          }
          return;
    
        // Peak Tracking state: capture largest reading
        case 1:
          if (voltage > peak) {
            peak = voltage;     
          }
          if (msec >= peakTrackMillis) {
            Serial.print("peak = ");
            Serial.println(peak);
            int velocity = map(peak, thresholdMin, 1023, 1, 127);
            { 
              dc1.amplitude(0.1);
              sound0.play(AudioSampleSnare);   
            }
            msec = 0;
            state = 2;
          }
          
          return;
    
        
        // Ignore Aftershock state: wait for things to be quiet again.
        default:
          if (voltage > thresholdMin) {
            msec = 0; // keep resetting timer if above threshold
          } else if (msec > aftershockMillis) 
            {
              // soundOff
            state = 0; // go back to idle when
          }
      }
      
     }

  12. #12
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    Well, I have this result. It is not very beautiful but it works. I have a problem with the velocity which is very bad. If I tap the Piezo with my hand, I don't have a volume variation, but with a pencil it works. What should I change? Thank you

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    const int piezo = analogRead(A0);
    const int thresholdMin = 60;  // minimum reading, avoid noise and false starts
    const int peakTrackMillis = 12;
    const int aftershockMillis = 25; // aftershocks & vibration reject
    int state=0; // 0=idle, 1=looking for peak, 2=ignore aftershocks
    int peak;    // remember the highest reading
    elapsedMillis msec; // timer to end states 1 and 2
    
    // WAV files converted to code by wav2sketch
    #include "AudioSampleSnare.h"        // http://www.freesound.org/people/KEVOY/sounds/82583/
    
    
    // GUItool: begin automatically generated code
    AudioPlayMemory          sound0;         //xy=636.75,91
    AudioSynthWaveformDc     dc1;            //xy=642,172
    AudioEffectMultiply      multiply1;      //xy=786,133
    AudioMixer4              mix1;           //xy=937.25,161.25
    AudioOutputAnalog        dac;            //xy=1134,185.25
    AudioOutputI2S           headphones;     //xy=1157.75,110
    AudioConnection          patchCord1(sound0, 0, multiply1, 0);
    AudioConnection          patchCord2(dc1, 0, multiply1, 1);
    AudioConnection          patchCord3(multiply1, 0, mix1, 0);
    AudioConnection          patchCord4(mix1, 0, headphones, 0);
    AudioConnection          patchCord5(mix1, 0, headphones, 1);
    AudioConnection          patchCord6(mix1, dac);
    AudioControlSGTL5000     audioShield;    //xy=945.75,252.25
    // GUItool: end automatically generated code
    
    
    
    void setup() {
    
      Serial.begin(115200);
      while (!Serial && millis() < 2500) /* wait for serial monitor */ ;
      Serial.println("Piezo Peak Capture");
    
      dc1.amplitude(0);
     
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(50);
    
      // turn on the output
      audioShield.enable();
      audioShield.volume(0.6);
    
    
      // reduce the gain on mixer channels, so more than 1
      // sound can play simultaneously without clipping
      mix1.gain(0, 0.4);
      mix1.gain(1, 0.4);
      mix1.gain(2, 0.4);
      mix1.gain(3, 0.4);
    
    }
             
    void loop()
    {
    int piezo = analogRead(piezo);
    int velocity = map(peak, thresholdMin, 1023, 1, 127);
    
    
     if (state == 0)
     
     { 
        // IDLE state: if any reading is above a threshold, begin peak
         if (piezo >= thresholdMin){
          float ampLevel = piezo;
          Serial.println("begin state 1");
          sound0.play(AudioSampleSnare);
          peak = piezo;
          state = 1;
          msec = 0;
            dc1.amplitude(0.2);
            mix1.gain(0, ampLevel / 127);
            mix1.gain(1, ampLevel / 127); 
        }
      }
       
      
      else if (state == 1) {
        // Peak Tracking state: for 10 ms, capture largest reading
        if (piezo > peak) {
          peak = piezo;
        }
        if (msec >= 10) {
          Serial.print("peak = ");
          Serial.println(peak);
          Serial.println("begin state 2");
          state = 2;
          msec = 0;
        }
      } else {
        // Ignore Aftershock state: wait for things to be quiet again
        if (piezo > thresholdMin) {
          msec = 0; // keep resetting timer if above threshold
        } else if (msec > 30) {
          //Serial.println("begin state 0");
          state = 0; // go back to idle after 30 ms below threshold
        }
      }
      
    }

  13. #13
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,041
    Code:
    void loop() {
    int piezo = analogRead(piezo);
    This works

  14. #14
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,041

  15. #15
    Junior Member
    Join Date
    Apr 2020
    Location
    France
    Posts
    19
    Better like this ?

    Code:
    void loop()
    {
    int piezo = analogRead(A0);
    int velocity = map(peak, thresholdMin, 1023, 1, 127);
    I changed that and I have better sensitivity. The volume changes depending on whether I hit low or a little louder on the piezo. But I imagine it is not very good.

    Code:
     if (state == 0)
     
     { 
      
        // IDLE state: if any reading is above a threshold, begin peak
          if (piezo >= 60 && piezo < 65){
          float ampLevel = piezo;
          Serial.println("begin state 1");
          sound0.play(AudioSampleC12);
          peak = piezo;
          state = 1;
          msec = 0;
            dc1.amplitude(0.1);
            mix1.gain(0, ampLevel / 127);
            mix1.gain(1, ampLevel / 127); 
        }
    
          if (piezo >= 65 && piezo < 70){
          float ampLevel = piezo;
          Serial.println("begin state 2");
          sound0.play(AudioSampleC12);
          peak = piezo;
          state = 1;
          msec = 0;
            dc1.amplitude(0.3);
            mix1.gain(0, ampLevel / 127);
            mix1.gain(1, ampLevel / 127); 
        }
    
              if (piezo >= 70 && piezo < 75){
          float ampLevel = piezo;
          Serial.println("begin state 3");
          sound0.play(AudioSampleC12);
          peak = piezo;
          state = 1;
          msec = 0;
            dc1.amplitude(0.4);
            mix1.gain(0, ampLevel / 127);
            mix1.gain(1, ampLevel / 127); 
        }
      }
    
       
      else if (state == 1) {
        // Peak Tracking state: for 10 ms, capture largest reading
        if (piezo > peak) {
          peak = piezo;
        }
        if (msec >= 10) {
          Serial.print("peak = ");
          Serial.println(peak);
          Serial.println("begin state 4");
          state = 2;
          msec = 0;
        }
      } else {
        // Ignore Aftershock state: wait for things to be quiet again
        if (piezo > thresholdMin) {
          msec = 0; // keep resetting timer if above threshold
        } else if (msec > 30) {
          //Serial.println("begin state 0");
          state = 0; // go back to idle after 30 ms below threshold
        }
      }


    Before going further with the piezo sensors, I would like to know if it is worth it. I would like (I try) to do a handpan or something similar. I saw the tutorial on the sparkfun website Digital Handpan, but I would like to have velocity on the pads. So I turned to Piezos sensors but do you think that FSR sensors should not be more suitable for this project? I'm afraid that at the slightest shock, all the piezos will respond. I saw that FRS sensors exist in different forms. Even in the form of a button. FSR button
    What do you think ? Piezo or FSR?

    Thanks

  16. #16
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,041
    Sory, if have no experienc with piezos. I'd use OP-Amps as amplifiers, maybe.
    I think others can answer this better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •