Go Down

### Topic: PIEZO SENSORS FOR SIMPLE DRUM MACHINE (Read 1 time)previous topic - next topic

#### bzoleta

##### Apr 07, 2011, 01:18 am
Hi,

Anyone have any advice on the sensitivity of piezo sensors?  I am trying to build a drum machine with simple tones activated by piezo sensors.  The sensors seem to be activating the tones, but then the tones sustain for a random amount of time.  Can anyone take a look at the code and fill me in?

Code: [Select]
`/*LUCKYLARRY.CO.UK - Very basic drum machine/ knock sensors and chromatic scaleWe measure the force at which the piezo speakers are hit, the maximum on time is 1024 so whenthis is lower than a set threshold we execute our code. In order to take the peak reading wecontinously monitor the values and everytime it's lower we save that value. At the end of the loopwe reset the value.The soundwave function uses a custom monitor for counting microseconds as this seems to bea bit more accurate for measuring the delays needed. We produce our count by making a valueof microseconds, 35,000, which seems to give a good range of note lengths when multiplied byhow hard the sensor was hit.We output the sound to another speaker, creating a soundwave based on the Hertz of any givennote in the chromatic scale. Taking this value we calculate the times in microseconds neededto oscillate and generate the required frequency. Time = (1 / Hz) * 1,000,000.*/int CleftPadPin = 2;                                             // set the pins for the piezo speakersint DrightPadPin = 5;                                            // these are analog pins for inputint EPadPin = 3;int FPadPin = 4;int CleftLEDPin = 9;                                             // set the LED pins which are digitalint DrightLEDPin = 10;int ELEDPin = 8;int FLEDPin = 11;int speakerPin = 6;                                             // set the speaker pin on digital pin 6int padLimit = 400;                                            // the limit/ threshold at which to activate the logicint howHard = 1024;                                             // variable to store how hard the sensor is hit, the lower the value the harder the hitint  Cnote = 3915;                                              // set the pulse on/off time for each note.int  Dnote = 3700;int  Enote = 3519;int  Fnote = 3432;void setup(){  pinMode(speakerPin, OUTPUT);                                  // set the speaker as output  pinMode(CleftLEDPin, OUTPUT);                                  // set the LEDs as outputs  pinMode(DrightLEDPin, OUTPUT);  pinMode(CleftPadPin, INPUT);                                   // set the piezo speakers as inputs  pinMode(DrightPadPin, INPUT);}void soundwave(int note, int howHard ) {                        // function that takes 2 parameters, the note we want to play and how hard the sensor has been hit  unsigned long endSoundWave = micros() + (2 * howHard);    // start a count from the microseconds currently registered since the program first ran. And add on an arbitary value multiplied by howHard value  while(micros() < endSoundWave){                               // while the count is not reached    analogWrite(speakerPin, 1000);                              // set the speaker to on/ high    delayMicroseconds(note);                                    // wait the number of microseconds for the note    analogWrite(speakerPin, 0);                                 // set the speaker to off/ low    delayMicroseconds(note);                                    // wait for the same number again to complete our oscillation/ soundwave.  }                                                             // repeat for the length of time.}void loop(){   if (analogRead(EPadPin) < padLimit) {                     // if the sensor is hit and the reading is less than our threshold     digitalWrite(ELEDPin, HIGH);                            // turn an LED on.     while (analogRead(EPadPin) < padLimit) {                // while the analog reading is less than the limit       if (analogRead(EPadPin) < howHard) {                  // if the reading valueis less than our howHard value (1024)         howHard = padLimit - analogRead(EPadPin);           // then rewrite howHard with the new value       }                                                        // this allows us to capture the peak value.       soundwave(Enote, howHard);                               // now generate the soundwave with our note and how had the sensor was hit    }   }     if (analogRead(CleftPadPin) < padLimit){     digitalWrite(CleftLEDPin, HIGH);     while (analogRead(CleftPadPin) < padLimit) {       if (analogRead(CleftPadPin) < howHard) {       howHard = padLimit - analogRead(CleftPadPin);       }       soundwave(Cnote, howHard);      }     }   if (analogRead(FPadPin) < padLimit){     digitalWrite(FLEDPin, HIGH);     while (analogRead(FPadPin) < padLimit) {       if (analogRead(FPadPin) < howHard) {       howHard = padLimit - analogRead(FPadPin);       }       soundwave(Fnote, howHard);      }  }   if (analogRead(DrightPadPin) < padLimit){     digitalWrite(DrightLEDPin, HIGH);     while (analogRead(DrightPadPin) < padLimit) {       if (analogRead(DrightPadPin) < howHard) {       howHard = padLimit - analogRead(DrightPadPin);       }       soundwave(Dnote, howHard);    }  }  digitalWrite(DrightLEDPin, LOW);  digitalWrite(CleftLEDPin, LOW);                                // set the LED's back to low  digitalWrite(ELEDPin, LOW);  digitalWrite(FLEDPin, LOW);  howHard = 1024;                                               // set the variable back to the original value}`

Thank you!

#### johnwasser

#1
##### Apr 07, 2011, 02:33 pm
Code: [Select]
`    analogWrite(speakerPin, 1000);                              // set the speaker to on/ high`

Uh, PWM values only go up to 255.  You probably don't want to use 490Hz PWM on an audio output anyway.  I think you want:

Code: [Select]
`    digitalWrite(speakerPin, HIGH);                              // set the speaker to on/ high    delayMicroseconds(note);                                    // wait the number of microseconds for the note    digitalWrite(speakerPin, LOW);                                 // set the speaker to off/ low    delayMicroseconds(note);                                    // wait for the same number again to complete our oscillation/ soundwave.`
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up

Please enter a valid email to subscribe