Creating a Tap Tempo (help needed)

Hello,
I have a arduino Deicimila board, and i have great fun using it.
Now i would like to make a LFO (through PWM) using Tap Tempo.

That is that you have to tap the momantary switch twice to callculate the time for the PWM.
I have a good LFO running from pin9 (PWM) and i have a Potentiometer for the speed/rate control.
But now i would like to do that with a Tap Tempo switch.
How should i set this up in the software?
Could anybody give some idea's.

best regards
Ronald

you’ll have to take the time between two presses to calculate the differnce in Milliseconds.
so the first thing is to distinguish between the 1st and the 2nd press.
use a variable to count them.

int numberOfButtonPresses = 0;
long int time_1;
long int time_2;

long int time_difference; //thats what your looking for

now in your main loop wait for the action.

if (buttonPressed && numberOfButtonPresses== 0 ) { //first action
//maybe reset millis … resetMilis();
//store the “start time”
time_1 = millis();
numberOfButtonPresses++;
}

if (buttonPressed && numberOfButtonPresses== 1 ) { //SECOND action
//store the “end time”
time_2 = millis();
time_difference=time_2 - time_1;
numberOfButtonPresses=0; //reset button counter for the next time
}

you'll have to take the time between two presses to calculate the differnce in Milliseconds.
so the first thing is to distinguish between the 1st and the 2nd press.
use a variable to count them.

int numberOfButtonPresses = 0;
long int time_1;
long int time_2;

long int time_difference; //thats what your looking for

now in your main loop wait for the action.

if (buttonPressed && numberOfButtonPresses== 0 ) { //first action
//maybe reset millis ... resetMilis();
//store the "start time"
time_1 = millis();
numberOfButtonPresses++;
}

if (buttonPressed && numberOfButtonPresses== 1 ) { //SECOND action
//store the "end time"
time_2 = millis();
time_difference=time_2 - time_1;
numberOfButtonPresses=0; //reset button counter for the next time
}

HEllo,
Thank you very much, that's a big help.

Now i have an other question. I read that the millis() starts on startup. So on the first buttonPressed i need to reset the millis(), right?
But (sorry if this is a dump question) how do i reset the millis() and asign time_1 to the start time?
The same for time_2 and end time?

Is this also tru that you don't need an interrupt on overflow (because the millis() overflows on 9 hours .... )?

Thanks is advance.

regards
Ronald

Oke i have put some work in it to try things out and make things work and understand it better, this is what i got so far:

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • TTLFO - an Arduino-based Tap Tempo LFO
  • RonaldB, 20-08-2008
*/

const int pinPwmout = 9;
int buttonPressed = 2;
int ledPin = 13;
int val;
int numberOfButtonPresses = 0;
long int startTime;
long int endTime;

long int tempo; //time between tabs

void setup ()
{
pinMode(pinPwmout, OUTPUT);
pinMode(buttonPressed, INPUT);
pinMode(ledPin, OUTPUT);
}

extern volatile unsigned long timer0_overflow_count;

void resetMillis()
{
cli(); // disable interrupts
timer0_overflow_count = 0;
sei(); // enable interrupts
} 

void loop()
{
if(buttonPressed && numberOfButtonPresses == 0){
resetMillis();
startTime = millis(); //store start time
numberOfButtonPresses++;
}
if(buttonPressed && numberOfButtonPresses == 1) {
endTime = millis(); //store the "end time"
tempo = 2000;
numberOfButtonPresses=0; //reset button counter for the next time

digitalWrite (pinPwmout, HIGH); //square wave output on PWM pin 9
delay (tempo);
digitalWrite (pinPwmout, LOW);
delay (tempo);
}
}

I have put a delay of 2000 in the tempo just to see if that is working.
Well the PWM is working at 2000ms on and off, so thats working.

Only when I put in this line:

tempo = startTime - endTime

The LED is flashing really fast (at least i think). Could this be a debounce thing?

How would i get this thing work so it reads the correct tempo between tabs.

thanks in advance

best regards
Ronald

Anyone can help me please???

long int debounce_time = 5; // some small number

if( buttonPressed && numberOfButtonPresses == 1 && ( millis() - startTime > debounce_time) ) { 
...

of course you can also ‘hardware debounce’ your switch with an RC lowpass filter.