Hi all,
I’ve been working a lot on arduino lately and would like to thank all people helping on my previous questions.
I am trying to do a first “real” project that is trying to use everything I’ve learnt for the moment.
concept : 4 piezo sensors that triggers midi notes on a Midi Drum machine. I haven’t implemented the Midi part yet because of the impossibility to use the terminal to debugg the code.
- a sort of menu with 3 switches so that each piezo (visually selected with 4 leds) can trigger 4 different sounds.
- switch 1 and 2 are used to select the piezo i want to change the sound of.
- switch 3 is to change the sound.
I have a for loop that gets the analogRead of each sensor and when one piezo is hit the sound is on (represented for by a Serial.print - the piezo X is playing Y sound).
Everything is working but the accuracy of the trigger by the piezo sensor (I tried to plug them directly without resistors and with resistors. the result is the same).
I think something is wrong in my code because :
- I have coded a timelapse in bewteen which you can’t play the sound twice. (playTime > maxPlayTime) BUT I have more than one value coming out when I hit the piezo once.
- I don’t understand but for the moment I have hooked up only one piezo but the result on the terminal gives me some triggering from more than one analog input.
I believe something is wrong in this code :
for (int pin=0; pin<piezoCount; pin++) {
piezoVal [pin] = analogRead (piezo [pin]) ;
if (piezoVal[pin] > piezoThreshold[pin] && playTime[pin] > maxPlayTime [pin]) {
Serial.print (piezo [pin]+1); Serial.print (" is on with noteOn "); Serial.println (drumKit [piezoPos]);
playTime[pin] = 0;
}
playTime[pin]++ ;
}
it gives me on the terminal for one hit on the piezo different input Pins where it should be only one… Like if the for loop increases the pin number but keeps the values of the previous read.
here’s the terminal result for one hit :
1 is on with noteOn 40
1 is on with noteOn 40
1 is on with noteOn 40
4 is on with noteOn 40
4 is on with noteOn 40
1 is on with noteOn 40
4 is on with noteOn 40
1 is on with noteOn 40
4 is on with noteOn 40
2 is on with noteOn 40
And here’s the full code… If you could let me know where I’m doing it wrong and also if you see any possible general ameliorations, it would be much appreciated.
// Notes from drumkits I have available. each line is : kickdrum, snare, hithat, ride.
int drumSet [4][4] = {
{36, 40, 42, 53},
{35, 38, 17, 51},
{61, 60, 66, 67},
{23, 24, 25, 20}};
// the drumkit i finally use by changing it with the drumSet values available.
int drumKit [4] = {36, 40, 42, 53};
// 3 switches
int buttonSet [] = {11, 12, 13};
int buttonCount = 3;
// 4 leds that will represent the type of sound i will change : kickdrum, snare, hithat, ride.
int ledPin [] = {4,5,6,7};
int ledCount = 4;
int soundSelect = 0 ;
// switchstates change
int buttonState [] = {0, 0, 0};
int lastButtonState [] = {0,0,0};
// 4 piezos as sensors
int piezo [] = {0, 1, 2, 3};
int piezoCount = 4;
int piezoVal [] = {0, 0, 0, 0};
// Threshold for the piezo to put a sound ON.
int piezoThreshold [] = { 600, 600, 600, 600};
// 2 notes can't be played before playtime > maxplaytime
int playTime [] = { 0, 0, 0, 0};
int maxPlayTime [] = {90, 90, 90, 90};
// when initialized the piezo that can be changed is piezo 1
int piezoPos = 0;
int lastPiezopos;
void setup () {
Serial.begin (9600);
delay (1000);
Serial.println ("INITIALIZING");
delay (300);
// start set all buttons as inputs
Serial.println ("SETTING BUTTONS");
delay(300);
for (int i= 0; i<buttonCount ; i++) {
pinMode (buttonSet[i], INPUT) ;
Serial.print ("button "); Serial.print (buttonSet[i]) ; Serial.println (" is set ");
delay (300); }
Serial.println ("ALL BUTTONS ARE SET");
delay(300);
// end set all buttons as inputs
// start set all leds as outputs
Serial.println ("SETTING LEDS AS OUTPUTS");
delay(300);
for (int j= 0; j<ledCount ; j++) {
pinMode (ledPin[j], OUTPUT);
Serial.print ("ledPin "); Serial.print (ledPin[j]) ; Serial.println (" is set ");
delay (300); }
Serial.println ("ALL LEDS ARE SET");
// end set all leds as outputs
delay(300);
Serial.print ("READY"); delay(100); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.print (". "); Serial.println (". ");
}
void loop () { // Start loop
// ----------------------------------------------------------
// check value of piezo
// ----------------------------------------------------------
for (int pin=0; pin<piezoCount; pin++) {
piezoVal [pin] = analogRead (piezo [pin]) ;
if (piezoVal[pin] > piezoThreshold[pin] && playTime[pin] > maxPlayTime [pin]) {
Serial.print (piezo [pin]+1); Serial.print (" is on with noteOn "); Serial.println (drumKit [piezoPos]);
playTime[pin] = 0;
}
playTime[pin]++ ;
}
// ----------------------------------------------------------
// end check if piezo has a value
// ----------------------------------------------------------
// ----------------------------------------------------------
// check if a button is pressed and state change
// ----------------------------------------------------------
for (int i=0; i<buttonCount; i++) {
buttonState[i] = digitalRead (buttonSet[i] ) ;
if (buttonState[i] != lastButtonState[i]) {
if (buttonState[i] == HIGH) {
// if the state has changed + increment the counter
if (i == 0) {
piezoPos--; }
// if the state has changed - decrement the counter
else if ( i == 1 ) {
piezoPos++ ; }
else if (i==2 ) {
soundSelect++;
if (soundSelect > 3) { soundSelect = 0;}
drumKit [piezoPos] = drumSet [soundSelect] [piezoPos];
}
// verify I'm changing the right piezo sound
Serial.print ("You are on Piezo ");
Serial.print (piezoPos+1); Serial.print (" // ");
Serial.print ("and sound "); Serial.println (drumKit [piezoPos]);
// Write on terminal what I have stored as the drumKit to be used.
Serial. print (drumKit [0]); Serial. print (" "); Serial. print (drumKit [1]); Serial. print (" ");
Serial. print (drumKit [2]); Serial. print (" "); Serial. print (drumKit [3]); Serial. println (" ");
} // end if buttonState === HIGH
// save the current state as the last state,
//for next time through the loop
lastButtonState[i] = buttonState[i];
} // end if buttonState != lastbuttonstate
} //end for int i=0
// turn ON the corresponding led and turn off the others
digitalWrite (ledPin[piezoPos], HIGH);
digitalWrite (ledPin[piezoPos-1], LOW);
digitalWrite (ledPin[piezoPos+1], LOW);
// ----------------------------------------------------------
// end check of button press
// ----------------------------------------------------------
} // end loop