Go Down

Topic: Tap to start Metronome (Read 153 times) previous topic - next topic

Hello! I'm very new to using Arduinos and have mostly just used it to interface some sensors into Puredata in the past. So this is my first Arduino only project.

The ultimate goal is to build an LED metronome (no sound) with a button, when tapped, will start the metronome cycle. So if my LED is blinking every 1000ms, and I tap the button at half way in between, it starts the 1000ms cycle from the top immediately.

That is the most important function.

What is the most confusing to me right now, is trying to detect that button tap at any given millisecond, while the blinking loop is running. Any suggestions would be great!


rogerClark

In the examples, look at 02.Digital->BlinkWithoutDelay

BulldogLowell

#2
Jul 19, 2014, 02:13 pm Last Edit: Jul 19, 2014, 02:17 pm by BulldogLowell Reason: 1

... trying to detect that button tap at any given millisecond,...


You can look also at the Button example and learn how to detect the button press.

rogerClark's recommendation along with Button will get you a long way, but remember you have two time fences.  The first is 1000ms to start the illumination of the LED and the second is the duration of the LED's illumination (liteTime).

pseudo code:

Code: [Select]

void setup()
//
start metronome timer with 1000ms interval WITHOUT DELAYS
//
void loop()
//
if (digitalRead(yourButtonPin) == HIGH)
 reset the millis() timer
//
calculate elapsedTime each time loop() executes
if (elapsedTime < liteTime) turnOnLED;
else turnOffLED
if (elapsedTime>= 1000)
 Reset the millis() timer
//

Thank you both for your replies!

I have looked at BlinkWithoutDelay, and I sort of understand it.  :~

One big problem with it is (which I failed to mention before), is that I need the light pulses to be 50ms long. So not 1000ms on and then 1000ms off, back and forth. I need 50ms on, then 950ms off, repeating (which I've managed to do with Delay). This is much easier as a musician to follow. Is that what you meant, Bulldog, by two time fences?

Also, Bulldog, in your pseudocode, you have the blinking starting before "void loop". How do you do that?

I guess to figure this out I need to do:

1) Get the BlinkWithoutDelay to restart it's cycle when the button is pressed
2) Get the BlinkWithoutDelay to run with these two time windows (LED on time at 50ms and LED off time at 950ms)

BulldogLowell

#4
Jul 19, 2014, 06:22 pm Last Edit: Jul 19, 2014, 06:55 pm by BulldogLowell Reason: 1
something like:
Code: [Select]

int ledPin = 13; //your on-board LED
unsigned long startTime;
unsigned long duration = 50UL;
unsigned long meter = 1000UL;

void setup()
{
 startTime = millis();
  pinMode(ledPin, OUTPUT);
}

void loop()
{
 //check the pin here
 unsigned long elapsedTime =  millis() - startTime;
 if (elapsedTime <= duration) digitalWrite(ledPin, HIGH); // this lights the LED only for the first 50ms of the timer
 else digitalWrite(ledPin, LOW);
 if (elapsedTime >= meter) startTime += meter; // this sets the meter to 1000ms
}

Go Up