Tap tempo with midi

Hi, I've got a pressure pad (simple switch circuit) which I want to use as a tap tempo via midi. Basically whenever the circuit is closed I want to send a midi message out, which I can then midi map to the tap tempo in a DAW. I'm having 2 main problems. Firstly, the midi messages i've been getting have appeared as "invalid" on a midi monitor program i'm using (so obviously my coding is horrendous!) and secondly, I only want one midi message each time the pressure pad is pressed. At the moment it sends hundreds of messages (because of the loop) - but I cant use a delay function as I will eventually have a load of other sensors running along side which need a fast loop. Im sure theres a simple solution but I'm quite new to this and i'm learning as I go :slight_smile:

At the moment it sends hundreds of messages (because of the loop) - but I cant use a delay function as I will eventually have a load of other sensors running along side which need a fast loop. Im sure theres a simple solution

There is. You need to keep track of the previous state of the switch, and send the message only when a transition occurs - the current state is not the same as the previous state - and that transition is to pressed.

Ah brilliant! Cheers PaulS!

Any clues on how to send a simple bang message over midi? I've figured out how to send note commands/values/intensitys but not how to send a simple bang message :confused:

I don't know what a 'simple bang' message is. It's not listed in the MIDI specification that I've seen.

sorry, bang message wouldn't be its official name - im not sure of the name for the message im looking for. I've been looking at the midi specification but I cannot work out which message is the correct one. I just need to send a one byte message that I can midi map to the tap tempo of a DAW (probably ableton live).

You could probably try any of the 'undefined' message numbers.

definitely a good idea :slight_smile: I won't be able to try it out til tomorrow tho cuz i'm not near my arduino! :frowning:

So i've got the following code now:

int tapTempo = 7;
int tempoState = 0;
int lastTempoState = LOW;

void setup() {
  Serial.begin(31250);
  pinMode (7, INPUT);
}


void loop() {
  tempoState = digitalRead (tapTempo);
  
  if (tempoState == LOW) {
    lastTempoState = LOW;
  }
    
  if ((tempoState == HIGH) && (lastTempoState == LOW)) {

    sendMidi (0xF4);
    lastTempoState = HIGH;
  }

}


void sendMidi (int cmd) {
  Serial.write(0xF0);
  Serial.write(cmd);
  Serial.write(0xF7);
}

However, it's still sending more than one message each time I press the pressure pad. Have I done something wrong?

Have I done something wrong?

Possibly. What is connected to pin 7?

This logic is easier to follow:
int currTempoState;
int lastTempoState = LOW;

void loop()
{
  currTempoState = digitalRead (tapTempo);
  
  if (currTempoState != lastTempoState)
  {
     if (tempoState ==LOW)
     {
        sendMidi (0xF4);
     }
  }
  lastTempoState = currTempoState;
}

You may need to account for the switch bouncing. You may need to send a MIDI OFF note when the switch is released.

What is connected to pin 7?

The pressure pad is connected to pin 7 (so it goes HIGH when the circuit is closed).

This is what i’ve developed my code into over the last few hours:

int tapTempo = 7;
int tempoState = 0;
int lastTempoState = LOW;
long previousMillis = 0;
long interval = 100;

void setup() {
  Serial.begin(31250);
  pinMode (7, INPUT);
}

void loop() {
  tempoState = digitalRead (tapTempo);
  unsigned long currentMillis = millis();
  
  if (tempoState == LOW) {
    lastTempoState = LOW;
  }
    
  if ((tempoState == HIGH) && (lastTempoState == LOW) && (currentMillis - previousMillis > interval)) {
    sendMidi (0x90, 0x36, 0x7F);
    lastTempoState = HIGH;
    previousMillis = currentMillis; 
  }
}

void sendMidi (int cmd, int value, int vel) {
  Serial.write(cmd);
  Serial.write(value);
  Serial.write(vel);
}

I added the millis timer as it was the only way to stop more than one message being sent every time the circuit was closed (I think that was because of the switch bouncing that you mentioned). Anyway, this code works, but I don’t know if its the best way of doing it :confused: Whats the advantage of having the if statement within an if statement like you did in your post?

Whats the advantage of having the if statement within an if statement like you did in your post?

Legibility.

In my example, it is clear that nothing happens except at transition times. In your code, one needs to study it for a while to see that this is true.

In my code, most times through loop, one if statement is evaluated. In yours, two are evaluated on every pass.

In mine, the value of previous state is set to current state, unconditionally. In yours, the value of previous state is defined correctly, but it takes some studying of the code to see that this is true.

100 milliseconds is a long time for a switch to bounce. This is one of the few cases where a simple delay(10) could be used, with minimal impact on the program, and with great increase in understandability.

ah cool. thank you for all your help!! :slight_smile:

Hi bamboozle89
Im trying to build my own tap tempo switch for a synth and yours looks exactly like what I need for my first Arduino project!

Im totally new on this so I would much appreciate if you could post a little schematic

Cheers!