Help needed with laser activated metronome project!

Hi guys,

Managed to find some working code for a decent metronome which is perfect for my project but i’m looking to replace the reset button on the device with a laser and receiver instead. i managed to get the laser working so that when broken it corresponds in the serial monitor however i’m unsure as to how to convert this reading into a function capable of resetting or stopping the metronome’s functions.

I’ll put the code that i’m using below… any help or advice would be greatly appreciated!

Attached is also a diagram of the metronome without the laser or receiver (sorry for how sketchy it is).

The laser I’m using is a KY-008, cheers.

Thanks again for any help,
Matt

#define SW_LED 13
#define SW_BEEP 12
#define POT_TEMPO A0
#define BUZZER 2

#define LED1 3
#define LED2 5
#define LED3 6
#define LED4 9

int buttonLedState = LOW;
int buttonLedPrevState = LOW;
int ledState = 1;
int buttonBeepState = LOW;
int buttonBeepPrevState = LOW;
int beepState = 1;
int bpm = 60;
int beatPerBar = 4;
int currentBeat = 1;
long lastBeat;
int mode = 1;
int Laser = 4;
int Detector = 7;

unsigned long uptime;

void setup() {
pinMode(SW_LED, INPUT);
pinMode(SW_BEEP, INPUT);
pinMode(BUZZER, OUTPUT);

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);

lastBeat = millis();
tone(BUZZER,1500,100);

 Serial.begin (9600);
 pinMode(Laser, OUTPUT);
 pinMode(Detector, INPUT);
}

void loop() {
// Update uptime;
uptime = millis();

// Read Digital & Analog inputs
int potTempo = analogRead(POT_TEMPO);
buttonLedState = digitalRead(SW_LED);
buttonBeepState = digitalRead(SW_BEEP);

// Delay to stop button weirdness
delay(1);

// Toggle LED State if LED Button Pressed
if((buttonLedState == HIGH) && (buttonLedPrevState == LOW)) {
mode++;
}
buttonLedPrevState = buttonLedState;

// Toggle Beeper State if BEEP Button Pressed
if((buttonBeepState == HIGH) && (buttonBeepPrevState == LOW)) {
beatPerBar++;
}
buttonBeepPrevState = buttonBeepState;

if(mode>4) mode = 1;
switch(mode) {
case 1:
ledState = 1;
beepState = 1;
break;
case 2:
ledState = 0;
beepState = 1;
break;
case 3:
ledState = 1;
beepState = 0;
break;
default:
ledState = 0;
beepState = 0;
}

if(beatPerBar > 6) beatPerBar = 3;

// Map Tempo Potentiometer to BPM Between 60 and 120
bpm = map(potTempo,0,1023,60,320);
long msPerBeat = (1000.0/((long)bpm/60.0));

if((uptime - lastBeat) > msPerBeat)
{
currentBeat++;
if(currentBeat > beatPerBar) currentBeat = 1;
lastBeat = uptime;
if(beepState) {
if(currentBeat == 1) {
tone(BUZZER,400,20);
} else {
tone(BUZZER,200,20);
}
}
}

if(ledState) {
int ledLight = map((uptime - lastBeat),0,msPerBeat,255,0);
switch(currentBeat)
{
case 1:
analogWrite(LED1, ledLight);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
break;
case 2:
digitalWrite(LED1, LOW);
analogWrite(LED2, ledLight);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
break;
case 3:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
analogWrite(LED3, ledLight);
digitalWrite(LED4, LOW);
break;
default:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
analogWrite(LED4, ledLight);

}
} else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}

 digitalWrite(Laser, HIGH);
 boolean val = digitalRead(Detector);
 Serial.println(val);
  val = digitalRead(Detector);
  digitalWrite(BUZZER, LOW);
}

Matt, you are a bad boy. Go read the forum guidelines, edit & fix your post. Then maybe you will get some help.

PaulRB:
Matt, you are a bad boy. Go read the forum guidelines, edit & fix your post. Then maybe you will get some help.

Sorry about that, i’m new here as you can tell and have only used a small number of forums. I think that’s most of my post fixed now :slight_smile:

Much better, thanks. But please re-post your sketch after pressing ctrl-T to indent it properly. Also re-post your diagram but include the laser and detector. Use the schematic view, not the breadboard view.

Please explain what you mean by replacing the reset button with the laser detector. Do you mean the Arduino's reset button?

Hi,

Will do and yes. If possible i would like to remap the reset button as seen to a laser switch to that when the beam is broken the metronome will simply cease to function. I'm not sure if this is the most effective way of going about this but it seems to be a simple method at least?

Thanks for the help.

Why not just hook up the laser detector to another digital pin and have the code check it in loop()?

Post a link to the laser and detector you want to use. Remember forum guidelines about posting links.