talofer99 icon

druns analog logic

talofer99 | PRO | 03/22/19 01:56:08 PM UTC | 0 ⭐ | 333 👁️ | Never ⏰ | []
C |

1.81 KB

|

None

|

0 👍

/

0 👎

// PROCCESS DRUMS
void processDrums() {
  // read value of curernt piezo
  currentPiezoValue = analogRead(currentPiezo);
 
  // state machine of the piezo
  switch (state[currentPiezo]) {
    // IDLE
    case 0:
      // if we are over the threshold
      if (currentPiezoValue > threshold[currentPiezo] && currentStateMillis[currentPiezo] + MILLIS_BETWEEN_TWO_OF_THE_SAME_NOTE <= millis()) {
        // if current read is higher, we are still in the rise ...
        while (currentPiezoValue > peekPiezoLevel[currentPiezo]) {
          // set the peek value
          peekPiezoLevel[currentPiezo] = currentPiezoValue;
          currentPiezoValue = analogRead(currentPiezo);
        }
 
        velocityLevel[currentPiezo] = map(peekPiezoLevel[currentPiezo], threshold[currentPiezo], threshold[currentPiezo] * 3, 0, 127);
        if (velocityLevel[currentPiezo] > MAX_MIDI_VELOCITY)
          velocityLevel[currentPiezo] = MAX_MIDI_VELOCITY;
        // play note
        midiNoteOn(note[currentPiezo], velocityLevel[currentPiezo]);
        // set state to 2
        state[currentPiezo] = 1;
        // set new state millis
        currentStateMillis[currentPiezo] = millis();
 
      }
      break;
    // looking for the break
    case 1:
      if (currentStateMillis[currentPiezo] + MILLIS_BETWEEN_PICKS_AND_OFF <= millis()) {
        // stop note
        midiNoteOff(note[currentPiezo], velocityLevel[currentPiezo]);
        //set peek to zero
        peekPiezoLevel[currentPiezo] = 0;
        velocityLevel[currentPiezo] = 0;
        // set state to 2
        state[currentPiezo] = 0;
        // set new state millis
        currentStateMillis[currentPiezo] = millis();
      }
      break;
 
  } //end switch
 
 
  // move to next piezo
  currentPiezo = (currentPiezo + 1) % NUM_PIEZOS;
 
} //end processDrums

Comments