Project 1: Metronome

This project was based on tap metronomes used by guitarists, the idea is the user can input a speed by tapping the button twice, the gap between the two button presses dictates the gap between the light flashes.

Components:

  • 11 wires
  • 1 led
  • 1 push button
  • 1 piezo speaker
  • 2 2.2k ohm resistors
  • 2 560 ohm resistors

Circuit:

Metronome

First Code:

int button = 2;
int led = 13;
int speaker = 11;
int tones = 523;
int ledvalue = LOW;
int ledvalue2 = HIGH;
long ledstarttime = 0;
long ledblinkinterval = 1000;
long buttonpresstime = 0;
int buttonvalue = HIGH;
int led2 = 4;
void setup (){
pinMode(led, OUTPUT);
pinMode(speaker,OUTPUT);
pinMode(button,INPUT);
pinMode(led2,OUTPUT);
Serial.begin(9600);
}

void loop(){
buttonvalue = digitalRead(button);
if (buttonvalue==LOW && buttonpresstime==0){
buttonpresstime = millis();
}
if (buttonvalue == HIGH && buttonpresstime!=0){
ledblinkinterval = millis() – buttonpresstime;
buttonpresstime = 0;
}
if(millis() – ledstarttime > ledblinkinterval) {
ledstarttime = millis();

digitalWrite(led,HIGH);
Serial.println(ledblinkinterval);
delayMicroseconds(ledblinkinterval);
digitalWrite(led,LOW);
delayMicroseconds(ledblinkinterval);
}
}

This Code however caused the led to barely light an the speaker to make clicking noises, so i tried a different approach:

Final Code:

int button = 2;
int led = 13;
int speaker = 11;
int tones = 523;
int ledvalue = LOW;
int ledvalue2 = HIGH;
long ledstarttime = 0;
long ledblinkinterval = 1000;
long buttonpresstime = 0;
int buttonvalue = HIGH;

void setup (){
pinMode(led, OUTPUT);
pinMode(speaker,OUTPUT);
pinMode(button,INPUT);
}

void loop(){
buttonvalue = digitalRead(button);
if (buttonvalue==LOW && buttonpresstime==0){
buttonpresstime = millis();
}
if (buttonvalue == HIGH && buttonpresstime!=0){
ledblinkinterval = millis() – buttonpresstime;
buttonpresstime = 0;
}
if(millis() – ledstarttime > ledblinkinterval) {
ledstarttime = millis();

if (ledvalue == LOW)
ledvalue = HIGH;
else
ledvalue = LOW;

digitalWrite(led, ledvalue);

if (ledvalue2 == HIGH)
ledvalue2 = LOW;
else
ledvalue2 = HIGH;

tone (speaker, tones, ledvalue2);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s