Go Down

Topic: Arduino metronome, blinks fast on "zero" (Read 109 times) previous topic - next topic

joecool85

Hi guys,

I'm working on creating an 8 bit binary metronome.  All works well with one exception, when all toggles are set to 0, it should register as BPM() == 0 and not run the metronome.  Instead, it blinks at a rate of about 200bpm (haven't measured yet).

Am I missing something in my code?  I thought that if you set the int to 0 and then only added when toggles were set to "HIGH", if all are "LOW", you should still have 0.

Here is my code:

Code: [Select]


#define SPKR  12      // speaker pin
#define LED  13      // speaker pin

void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure input pins as an input and enable the internal pull-up resistor
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(SPKR, OUTPUT);  // output to amp/speaker
  pinMode(LED, OUTPUT); //LED
}

int BPM() {
  int Sum = 0;
  if (digitalRead(3) == HIGH)
    Sum += 1;
  if (digitalRead(4) == HIGH)
    Sum += 2;
  if (digitalRead(5) == HIGH)
    Sum += 4;
  if (digitalRead(6) == HIGH)
    Sum += 8;
  if (digitalRead(8) == HIGH)
    Sum += 16;
  if (digitalRead(10) == HIGH)
    Sum += 64;
  if (digitalRead(11) == HIGH)
    Sum += 128;
  return(Sum);
}


void loop() {
  // Make metronome noises!
    if (BPM() == 0) {
      noTone(SPKR);
      digitalWrite(LED, HIGH);
    } else {
      tone(SPKR, 440, 50);   // frequency of tick, length of tick
      digitalWrite(LED, HIGH);
      delay(100);
      digitalWrite(LED, LOW);
      delay((60000 / BPM()) - 100);     // time between beats
    }
}


UKHeliBob

What do you see when you print the value returned by the BPM() function ?

Check how many pins you are reading in the BPM() function
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

joecool85

As I only have a speaker and an LED for output, I'm not sure how I would see the print output.  Is there a way to run this as a simulation in IDE?

Montmorency

Silly question, but are you sure you are not confusing "all toggles 0" state with "all toggles 1" state?

joecool85

Silly question, but are you sure you are not confusing "all toggles 0" state with "all toggles 1" state?
Not a silly question as that could happen.  However, when all toggles are set HIGH, they respond as they should and give me 255 BPM (binary 11111111).  Also, all "in between" numbers work as well, like 96 BPM (01100000).

UKHeliBob

Quote
I'm not sure how I would see the print output.
Using Serial.print() and the Serial Monitor would be the obvious way
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

joecool85

Using Serial.print() and the Serial Monitor would be the obvious way
I'm new, but it's my understanding I would need to utilize FTDI for this.  I have an Adafruit Pro Trinket and am only using the onboard USB.

Nothing in the code stands out as being an issue for you guys?

joecool85

I figured it out.  It was my wiring.  I was using SPDT toggles for the binary input.  I had ground down the center pins, then input to the board on one side of the switch and ground to the board on the other.  My concept was that I could turn the board on by enabling the ground connection any time a switch was set to "1".  This would work except I didn't realize that when you have an input pin set to "LOW" input or "0", it sinks to ground.  This meant that even when all switches were set to zero, it was trying to fire up the board, but didn't have a valid ground connection.  I have added a power switch to switch the positive lead and connected ground directly to the battery negative and things work as they should now.

In retrospect, I could make my original idea by using a DPDT so that the input and negative to ground connections were on separate poles.  For now this works though.  Maybe next time!

Go Up