Pages: [1]   Go Down
Author Topic: PIEZO SENSORS FOR SIMPLE DRUM MACHINE  (Read 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
LUCKYLARRY.CO.UK - Very basic drum machine/ knock sensors and chromatic scale
We measure the force at which the piezo speakers are hit, the maximum on time is 1024 so when
this is lower than a set threshold we execute our code. In order to take the peak reading we
continously monitor the values and everytime it's lower we save that value. At the end of the loop
we reset the value.
The soundwave function uses a custom monitor for counting microseconds as this seems to be
a bit more accurate for measuring the delays needed. We produce our count by making a value
of microseconds, 35,000, which seems to give a good range of note lengths when multiplied by
how hard the sensor was hit.
We output the sound to another speaker, creating a soundwave based on the Hertz of any given
note in the chromatic scale. Taking this value we calculate the times in microseconds needed
to oscillate and generate the required frequency. Time = (1 / Hz) * 1,000,000.
*/
int CleftPadPin = 2;                                             // set the pins for the piezo speakers
int DrightPadPin = 5;                                            // these are analog pins for input
int EPadPin = 3;
int FPadPin = 4;
int CleftLEDPin = 9;                                             // set the LED pins which are digital
int DrightLEDPin = 10;
int ELEDPin = 8;
int FLEDPin = 11;
int speakerPin = 6;                                             // set the speaker pin on digital pin 6
int padLimit = 400;                                            // the limit/ threshold at which to activate the logic
int howHard = 1024;                                             // variable to store how hard the sensor is hit, the lower the value the harder the hit
int  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!
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8958
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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:
    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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: