Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 4 FirstFirst 1 2 3 4 LastLast
Results 26 to 50 of 82

Thread: test code for piezo

  1. #26
    Attachment 12249 I get random numbers with or without jumper0 196532 229464 2192

  2. #27
    Attachment 12249
    I get random numbers with or without jumper

    0 1965
    32 2294
    64 2192
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	DSC.jpg 
Views:	32 
Size:	146.8 KB 
ID:	12250  

  3. #28
    Senior Member manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,258
    I asked if you had soldered your header pins to the T3.6 .... the photo seems to suggest you have tied the teensy to the pins?? No solder. No workee. you must solder the pins

    you can order teensy with pins already soldered https://www.pjrc.com/store/teensy36_pins.html
    Last edited by manitou; 12-11-2017 at 10:05 PM.

  4. #29
    OK, I did check the connections with a meter and the buttons sketch worked with all the analog inputs but I could see it may be an issue. I soldered the pins, when I change the jumper from DACO to ground I still get
    0 2166
    32 2143
    64 2302
    I'll try the pot next
    Last edited by seayaker; 12-11-2017 at 10:58 PM.

  5. #30
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,205
    Quote Originally Posted by seayaker View Post
    Thanks, I appreciate your help, I tried the first test connecting A21 to A0 and running your code The serial monitor just keeps printing 2 rows of 4 digit numbers, It makes no difference when I disconnect the jumper. fried?
    There is a world of difference between telling us what you're seeing versus actually showing us the result.

    It's ok to be a beginner. We want to help you. But please try to understand you need to SHOW us the results. To put an image into you message, click this button:

    Click image for larger version. 

Name:	sc.png 
Views:	12 
Size:	8.0 KB 
ID:	12251

    EDIT: ah, I see more messages since this reply. Just catching up now.

  6. #31
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,205
    Your jumper wire is connected in the wrong location!

    Name:  DSC2.jpg
Views: 69
Size:  27.1 KB

  7. #32
    Click image for larger version. 

Name:	DSC3.jpg 
Views:	13 
Size:	133.9 KB 
ID:	12254Click image for larger version. 

Name:	Ds1.jpg 
Views:	12 
Size:	113.0 KB 
ID:	12253

    I had to knock the resolution down to 75% to get the photos to upload, It's not really that great of a camera. This is what I'm trying to get working. The pads have all been tested and I've been using the pedals with another unit for some time. The kick pedal is just another pad and the hi hat is a force sensing resistor. I built this because my other unit only had 8 pads and can only use 16 bit sound files so this would be a huge upgrade. I guess I had a dyslexic moment with the board, which is encouraging in that I may not have fried it. I soldered the pins but have to Quit for tonight. I thank you for your help and will try again tomorrow.
    Last edited by seayaker; 12-12-2017 at 01:34 AM.

  8. #33
    Works for me. For some reason I felt compelled to hook it up and run it. When properly wired DAC0 to A0 the provided code does indeed work on the T_3.6.

  9. #34
    As noted in post #8
    Quote Originally Posted by defragster View Post
    @seayaker - ... If you click the "#" hashtag icon it adds HTML markers in the post to inset and format the code as shown in above posts to be more readable.
    ...

  10. #35
    /*
    Arduino-Drum
    Project's decription can be found here: http://arduinodrumkit.blogspot.com/
    */

    #include <MIDI.h>
    MIDI_CREATE_DEFAULT_INSTANCE();

    //analog Arduino's inputs
    const int Pad1 = A0;
    const int Pad2 = A1;
    const int Pad3 = A2;
    const int Pad4 = A3;
    const int Pad5 = A4;

    //volume threshold variable, being used to determine
    //if user stikes the drum pad
    const int volumeThreshold = 380;

    //variables for volume magnitude of each drum pad
    int Drum1_val = 0;
    int Drum2_val = 0;
    int Drum3_val = 0;
    int Drum4_val = 0;
    int Drum5_val = 0;

    //variables used in program logic
    //variables used in proces of separation of vibrtion from
    //exact force impact
    int signalTest2 = 0;
    int signalTest1 = 0;
    int signalTest3 = 0;
    int signalTest4 = 0;
    int signalTest5 = 0;

    //midi mapping - addictive drums mapping
    byte snareDrum = 38;
    byte kick = 36;
    byte hihatClosed = 53;
    byte crash = 77;
    byte hihatOpened = 56;

    void setup()
    {
    MIDI.begin(4);
    //starts serial communication between arduino na serial USB port
    Serial.begin(115200);
    }

    void loop()
    {
    /*1st pad*/
    //checks if magnitude of analoginput is higher than volumeThreshold
    if(analogRead(Pad1) >= volumeThreshold)
    {
    signalTest1 = 1;
    }
    //if magnitude of analoginput was higher than volumeThreshold
    //program checks again if the signal is decreasing
    //if so, it means that drum pad was hit
    if(signalTest1 == 1 && analogRead(Pad1) <= (volumeThreshold-100))
    {
    //sets the velocity
    Drum1_val= analogRead(Pad1);
    int velocity = Drum1_val/3 + 78;
    if(velocity >= 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    //sends midi signal over USB serial port
    MIDI.sendNoteOn(hihatOpened, velocity, 1);
    MIDI.sendNoteOff(hihatOpened, 0, 1);
    //sets signalTest again to 0
    signalTest1 = 0;
    }

    /*2nd pad*/
    if(analogRead(Pad2) >= volumeThreshold)
    {
    signalTest2 = 1;
    }
    Drum2_val = analogRead(Pad2);
    if(signalTest2 == 1 && analogRead(Pad2) <= (volumeThreshold-95))
    {
    Drum2_val= analogRead(Pad2);
    int velocity = Drum2_val/3 + 35;

    if(velocity >= 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(snareDrum, velocity, 1);
    MIDI.sendNoteOff(snareDrum, 0, 1);
    signalTest2 = 0;
    }

    /*3rd pad*/
    if(analogRead(Pad3) >= volumeThreshold)
    {
    signalTest3 = 1;
    }
    Drum3_val = analogRead(Pad3);
    if(signalTest3 == 1 && analogRead(Pad3) <= (volumeThreshold-95))
    {
    Drum3_val= analogRead(Pad3);
    int velocity = Drum3_val/2 + 77;

    if(velocity >= 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(hihatClosed, velocity, 1);
    MIDI.sendNoteOff(hihatClosed, 0, 1);
    signalTest3 = 0;
    }

    /*4th pad*/
    if(analogRead(Pad4) >= volumeThreshold)
    {
    signalTest4 = 1;
    }
    if(signalTest4 == 1 && analogRead(Pad4) <= (volumeThreshold-95))
    {
    Drum4_val= analogRead(Pad4);
    int velocity = Drum4_val/3 + 75;

    if(velocity > 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(crash, velocity, 1);
    MIDI.sendNoteOff(crash, 0, 1);
    signalTest4 = 0;
    }

    /*5th pad*/
    if(analogRead(Pad5) >= volumeThreshold)
    {
    signalTest5 = 1;
    Drum5_val = analogRead(Pad5);
    }
    if(signalTest5 == 1 && analogRead(Pad5) <= (volumeThreshold-95))
    {
    int velocity = Drum5_val;
    if(velocity >= 12)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(kick, velocity, 1);
    MIDI.sendNoteOff(kick, 0, 1);
    signalTest5 = 0;
    }
    }

  11. #36
    Click image for larger version. 

Name:	DS8.jpg 
Views:	22 
Size:	150.6 KB 
ID:	12258/*
    Arduino-Drum
    Project's decription can be found here: http://arduinodrumkit.blogspot.com/
    */

    #include <MIDI.h>
    MIDI_CREATE_DEFAULT_INSTANCE();

    //analog Arduino's inputs
    const int Pad1 = A0;
    const int Pad2 = A1;
    const int Pad3 = A2;
    const int Pad4 = A3;
    const int Pad5 = A4;

    //volume threshold variable, being used to determine
    //if user stikes the drum pad
    const int volumeThreshold = 380;

    //variables for volume magnitude of each drum pad
    int Drum1_val = 0;
    int Drum2_val = 0;
    int Drum3_val = 0;
    int Drum4_val = 0;
    int Drum5_val = 0;

    //variables used in program logic
    //variables used in proces of separation of vibrtion from
    //exact force impact
    int signalTest2 = 0;
    int signalTest1 = 0;
    int signalTest3 = 0;
    int signalTest4 = 0;
    int signalTest5 = 0;

    //midi mapping - addictive drums mapping
    byte snareDrum = 38;
    byte kick = 36;
    byte hihatClosed = 53;
    byte crash = 77;
    byte hihatOpened = 56;

    void setup()
    {
    MIDI.begin(4);
    //starts serial communication between arduino na serial USB port
    Serial.begin(115200);
    }

    void loop()
    {
    /*1st pad*/
    //checks if magnitude of analoginput is higher than volumeThreshold
    if(analogRead(Pad1) >= volumeThreshold)
    {
    signalTest1 = 1;
    }
    //if magnitude of analoginput was higher than volumeThreshold
    //program checks again if the signal is decreasing
    //if so, it means that drum pad was hit
    if(signalTest1 == 1 && analogRead(Pad1) <= (volumeThreshold-100))
    {
    //sets the velocity
    Drum1_val= analogRead(Pad1);
    int velocity = Drum1_val/3 + 78;
    if(velocity >= 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    //sends midi signal over USB serial port
    MIDI.sendNoteOn(hihatOpened, velocity, 1);
    MIDI.sendNoteOff(hihatOpened, 0, 1);
    //sets signalTest again to 0
    signalTest1 = 0;
    }

    /*2nd pad*/
    if(analogRead(Pad2) >= volumeThreshold)
    {
    signalTest2 = 1;
    }
    Drum2_val = analogRead(Pad2);
    if(signalTest2 == 1 && analogRead(Pad2) <= (volumeThreshold-95))
    {
    Drum2_val= analogRead(Pad2);
    int velocity = Drum2_val/3 + 35;

    if(velocity >= 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(snareDrum, velocity, 1);
    MIDI.sendNoteOff(snareDrum, 0, 1);
    signalTest2 = 0;
    }

    /*3rd pad*/
    if(analogRead(Pad3) >= volumeThreshold)
    {
    signalTest3 = 1;
    }
    Drum3_val = analogRead(Pad3);
    if(signalTest3 == 1 && analogRead(Pad3) <= (volumeThreshold-95))
    {
    Drum3_val= analogRead(Pad3);
    int velocity = Drum3_val/2 + 77;

    if(velocity >= 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(hihatClosed, velocity, 1);
    MIDI.sendNoteOff(hihatClosed, 0, 1);
    signalTest3 = 0;
    }

    /*4th pad*/
    if(analogRead(Pad4) >= volumeThreshold)
    {
    signalTest4 = 1;
    }
    if(signalTest4 == 1 && analogRead(Pad4) <= (volumeThreshold-95))
    {
    Drum4_val= analogRead(Pad4);
    int velocity = Drum4_val/3 + 75;

    if(velocity > 120)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(crash, velocity, 1);
    MIDI.sendNoteOff(crash, 0, 1);
    signalTest4 = 0;
    }

    /*5th pad*/
    if(analogRead(Pad5) >= volumeThreshold)
    {
    signalTest5 = 1;
    Drum5_val = analogRead(Pad5);
    }
    if(signalTest5 == 1 && analogRead(Pad5) <= (volumeThreshold-95))
    {
    int velocity = Drum5_val;
    if(velocity >= 12)
    velocity = 125;
    else if(velocity < 0)
    velocity = 0;

    MIDI.sendNoteOn(kick, velocity, 1);
    MIDI.sendNoteOff(kick, 0, 1);
    signalTest5 = 0;
    }
    }

  12. #37
    So back to square A, what can I do to trigger a note with the piezo?

  13. #38
    I've never looked at Midi - but PJRC has this: https://www.pjrc.com/teensy/td_midi.html

    See if you can trigger it in code after a delay() or something and work from there following the any examples - there are probably more pre-coded installed with Teensy Duino

    <edit> - look under : Files / Examples / MIDI Lilbrary >> T:\arduino-1.8.5\hardware\teensy\avr\libraries\MIDI\examples\ Basic_IO

  14. #39
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,205
    Your circuit is missing the GND connection!

    Click image for larger version. 

Name:	DS8.jpg 
Views:	11 
Size:	43.7 KB 
ID:	12266

  15. #40
    I've seen this, I was able to trigger notes with the buttons sketch, thought I was on my way till I tried it with a piezo.

  16. #41
    OK so how do I get the piezo to trigger a midi note?
    Attached Images Attached Images  

  17. #42
    Assuming you have GND connected now - added picture is clipped in my view.

    Using Paul's sketch from PRIOR post I've added MakeMidiNote() - put in that empty function the code used in your button test sketch that produced a MidiNote:

    Code:
    void MakeMidiNote() {
    
      Serial.println("Midi Note to follow!");
      // Add working test code here
    
    }
    
    
    const int thresholdMin = 12;  // minimum reading, avoid "noise"
    const int aftershockMillis = 60; // time of aftershocks & vibration
    
    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
    
    void setup() {
      Serial.begin(115200);
      //pinMode(A0, INPUT_DISABLE);
      while (!Serial && millis() < 4500) /* wait for serial monitor */ ;
      Serial.println("Piezo Peak Capture");
    }
    
    void loop() {
      int piezo = analogRead(A0);
    
      if (state == 0) {
        // IDLE state: if any reading is above a threshold, begin peak
        if (piezo > thresholdMin) {
          //Serial.println("begin state 1");
          state = 1;
          peak = piezo;
          msec = 0;
          MakeMidiNote();  // When a value over thresholdMin is seen it will call the function to execute the NOTE code above
        }
      } 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
        }
      }
      
    }
    Last edited by defragster; 12-14-2017 at 08:18 AM.

  18. #43
    That's actually Paul's picture, I guess he assumed I would know to connect the ground but I assumed the picture was complete. Anyway I did connect the gnd. so can I copy the code above and try it? I really do appreciate your help, I hit # last time hope that worked, can't tell from my end.

    Click image for larger version. 

Name:	DSCN052.jpg 
Views:	16 
Size:	142.9 KB 
ID:	12274

  19. #44
    Yes, above code should be tried.

    There is an 'Go Advanced' button beside the POST reply button on screen - it will allow PREVIEW of the post. And you can EDIT your own posts. If you Edit and select the code and then hit "#" it will put 'CODE' around it - but the formatting may already be lost as it is above.

  20. #45
    I copied and pasted the code as is and still nothing, I changed the 470 resistor with a pot because Paul said I may have to experiment with it, I have to his connected a DAW that all keys should make a sound, I don't know how this is mapped but if its the same as the buttons sketch it should trigger something. When I change to midi port says (emulated serial) in light gray. is that anything?

  21. #46
    There is a program called free piano- http://freepiano.tiwb.com/en/ it is very simple to use and can be used with any midi device including teensy. Free beer for anyone who can show me how to trigger 1 note with T3.6 and a piezo!

  22. #47
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,205
    Quote Originally Posted by seayaker View Post
    and still nothing
    .....
    I have to his connected a DAW that all keys should make a sound, I don't know how this is mapped but if its the same as the buttons sketch it should trigger something.
    The code I gave you in message #11 prints to the serial monitor. It does not matter which USB Type you have selected. But you must open the Arduino Serial Monitor to see the results.

    It does not send MIDI to your DAW. I hope you can understand to first get the code working with data in the serial monitor BEFORE you try to also add MIDI output.

    The code in setup() prints "Piezo Peak Capture". So you should not see "nothing" in the Arduino Serial Monitor. Even if the analog input is shorted to ground, at the very least you should see those 3 words printed!

  23. #48
    Paul, I do not see "Piezo Peak Capture" It does print peak=47 slowly like every 15 sec. or so the number changes I put a pot in place of the 470 resistor with the center to gnd. and the right side to the A0, when I turn it from the right just slightly back, it prints I can go back and forth all the way right, slightly back and it will print every time.
    Last edited by seayaker; 12-14-2017 at 07:40 AM.

  24. #49
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,205
    Maybe you have the resistors mixed up?

    In message #9 I wrote:

    This 10K resistor gives the piezo a load to drive. The result is much lower noise at the analog pin. In this test I used 10K, but you can adjust this resistor for more or less sensitivity.
    But you're saying:

    I changed the 470 resistor with a pot because Paul said I may have to experiment with it
    and

    so the number changes I put a pot in place of the 470 resistor with the center to gnd
    But the 470 ohm resistor does not connect to GND at all.

    This is the way they should be.

    Click image for larger version. 

Name:	piezo.jpg 
Views:	10 
Size:	61.2 KB 
ID:	12283
    Last edited by PaulStoffregen; 12-14-2017 at 08:19 AM.

  25. #50
    After checking your wiring ...

    Post #42 above has a copy of Paul's post #11 code. Minor changes:
    > I increased the wait for Serial to 4.5 seconds - that should give time for Serial Monitor to catch the first message
    > When the peak is detected it calls as noted the function to activate Midi tone. For now it will print, add the Midi code there that worked for your button and it should play.

    I don't know how midi works without looking - but that function could pass the PEAK value and respond accordingly based on how high the peak value is.

Posting Permissions

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