test code for piezo

Attachments

  • DSC.jpg
    DSC.jpg
    146.8 KB · Views: 143
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:
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:

sc.png

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

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:
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.
 
Code:
/*
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;
}
}
 
DS8.jpg/*
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;
}
}
 
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
 
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.
 
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;
[B][U]      MakeMidiNote();[/U][/B]  // 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:
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.

DSCN052.jpg
 
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.
 
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?
 
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!
 
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:
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.

piezo.jpg
 
Last edited:
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.
 
Back
Top