Go Down

Topic: Help needed to replace a button with a piezo (Read 915 times) previous topic - next topic

marco_c

The main difference is the piezo returns an analog value instead of a digital, so you will need to deal with values that are 0-1023 rather than HIGH/LOW.

One approch may be to use a threshold value (x) that over which you treat it as HIGH and below which it is LOW. That means you can reduce the number of changes to your current code.

Alternatively, you can use the additional information from a piezo to include 'loudness' of the drum. In this case you could use the value 0-1023 as an indication of how hard the drum is hit and, therefore, how loud it would be.

I would suggest you experiment with one piezo and the serial monitor, printing the values, to get a feel for what hitting the piezo does.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

BGONZA200


marco_c

#7
Mar 17, 2013, 11:41 pm Last Edit: Mar 17, 2013, 11:45 pm by marco_c Reason: 1
A couple of other suggestions:
* Your code has a lot of repetition in it. This means that you should be able to define a functin to do one drum (passing in the right parameters) and then call it with different parameters to do the other drums. This will make maintaining the code easier (eg, to change the way all drums work you only need to change the one function).
* Consider using arrays to store repetitive data and implify processing your code (see below for an example)
From
Code: [Select]
// define the pins we use
#define switchAPin 10
#define switchBPin 9
#define switchCPin 8
#define switchDPin 7
#define switchEPin 6
#define switchFPin 5
#define switchGPin 4
#define switchHPin 3
int switchAState = LOW;
int switchBState = LOW;
int switchCState = LOW;
int switchDState = LOW;
int switchEState = LOW;
int switchFState = LOW;
int switchGState = LOW;
int switchHState = LOW;

to
Code: [Select]
#define NUMNOTES 8
const uint8_t switchPin[NUMNOTES] = { 10, 9, 8, 7, 6, 5, 4, 3 };
int switchState[NUMNOTES] = { LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW };


Get the idea? You can do the same with the drum notes. The you can go through the whole lot using a 'for' loop in a few statements, as long as the array indices (ie, the 0, 1, etc) for the arrays are congruent for all arrays.

Code: [Select]

for (uint8_t i=0; i<NUM_NOTES; i++)
{
  currentSwitchState = digitalRead(switchPin[i]);
  if( currentSwitchState == LOW && switchState[i] == HIGH ) // push
    noteOn(drumchan, note_drum[i], 100);
  else if( currentSwitchState == HIGH && switchState[i] == LOW ) // release
    noteOff(drumchan, note_drum[i], 0);
  switchState[i] = currentSwitchState;
}
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Go Up