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.
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
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
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 .... )?
*/
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