/**************************************/
const int lowestPin = 1;//the lowest one attach to
const int speaker = 13;
int ts = 4;//how many beats you want
int count = 1;
const int keyPin = 12;
int buttonState = 0;
int bpm = 120;
/**************************************/
void setup()
{
for (int thisPin = 1; thisPin <= ts; thisPin++)
{
pinMode(thisPin, OUTPUT); //initialize thisPin as an output
pinMode(6, INPUT);
}
pinMode(keyPin, INPUT);
}
/****************************************/
void loop()
{
//iterate over the pins
//turn the led on from lowest to the highest
if (digitalRead(keyPin) == HIGH && ts == 2) {
ts = 3;
delay(2000);
}
else if (digitalRead(keyPin) == HIGH && ts == 3) {
ts = 4;
delay(2000);
}
else if (digitalRead(keyPin) == HIGH && ts == 4)
{
ts = 2;
delay(2000);
}
for (int thisPin = 1; thisPin <= ts; thisPin++)
{
bpm = pulseIn(6, HIGH);
pinMode(13, OUTPUT);
if (ts > 4)
{
if (count > 3)
for (int a = 4; a <= ts; a++) {
digitalWrite(a - 3, HIGH);
}
}
else
digitalWrite(thisPin, HIGH);
if (count == 1)
tone(13, 1500, 100);
else
tone(13, 1000, 100);
delay(bpm - 52);
pinMode(13, INPUT);
digitalWrite(thisPin, LOW);
count++;
if (count > ts)
count = 1;
}
}
I am trying to make a metronome using Arduino Uno, with 4 LEDs, each LED is a beat. I want to change the time signature every time I press the pushbutton, so that there are different number of beats per measure, but when I do press the button, it runs the code for 2 and 3 beats per measure once, and then goes back to its default, 4 beats per measure.
Sometimes, it even changes without pressing the button, randomly. I am using a 220 ohm resister instead of 10, as 10 starts much more randomly than the 220.
