Metronome with TimerOne using a10k pot to change the bmp.

Hello Erveryone

I am trying to make a metronome with TimerOne for my midi studio. As of now everything is working as I can see. I have a red led and a green led. the red blinks on the frist beat and the green follows in sequence 3 times to cover 4 beats.

When i reached this point i noticed the time is set in the setup portion of the sketch and would not be changed if i twist my pot on the A2 analog pin. everything i have tryed has failed..

Also for some reason i can not return a value from A2 only 1's 2's and 3's pop up in the serial monitor every now and again when i try to print back the mval or metVal. which is odd because i designed it to catch the values after the other potentiometers with a switch case.

So at this point im clueless on how to geting the vaule of my 10k Pot and magicly translating it to the bpm to control the leds? but from what i kind of understand would it have to do with the converstion of milliseconds and within an interupt perhaps? i dont know.. :'(

http://pastebin.com/embed_js.php?i=Ycac9Smw

That's a lot of code. It seems to be quite complicated for a metronome. I see that a lot of it is unused. For example, function GETMetVAL() is never called, function noteOn() consists only of comments, and several functions do nothing but call noteOn(). In the first 50 lines of the code, there seem to be a dozen declarations that are never referenced again. You can leave it this way if you like, but you'll get more and better responses if you clean up this code. You'll get even more and better responses if you post a small sketch that demonstrates the problem, rather than hundreds of lines of code.

That said,

ReggieRetro: Also for some reason i can not return a value from A2 ...

I don't find anything that tries to read an analog from A2. Can you tell us where that is in the code?

I understand it is a lot but the last time i posted parts of a sketch the person that gave me a hand said it was hard to track and i should post the whole thing.. So this is the whole midi controller sketch.

I removed what wasnt working it looked a lot worse. i will get it back to a state when i think it should be receiving information from A3.

A3 is being read in the GetmetVal fuction

mval = analogRead(A3);

i believe its line 253

Thank you for responding! tmd3.

i stripped down the code to its simplest form hope that helps.

http://pastebin.com/bCe0ry6a

My mistake i was testing to see if it was the pin that was giving me issues. anywhere A3 appears should be changed to A2.

and as for commenting the noteOn is a whole other beast. for some reason it plays all the notes/ the whole octave that are located on channel 1 or where ever they may be comming from im not sure. i figured i would wait to deal with that once i get the metronome working the way i need it to.

I have a couple of reasons for asking that you trim your posted code to something more manageable. First, it's a recommended practice, described in the sticky post, "How to use this forum - please read," shown near the top of the listing for each of the sections of the forum. See item #11, "Tips for getting the most out of your post," which says,

Post a complete sketch (program code)! If you don't you waste time while people ask you to do that. However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code. If the problem goes away in the minimal sketch, it wasn't where you thought it was.

It often happens that someone posts a portion of their code - a snippet that doesn't compile. When they do that, they usually get a veritable chorus of responses that say, "Post all your code!" When people post uncompileable snippets, we typically find that any analysis we do is a waste of time, because the poster then tells us that he takes care of whatever issues we find elsewhere in his code, or something.

When you post a complete, compileable sketch that demonstrates a specific problem, though, everything that we need to see is in the sketch, and we don't have to ask you for more. And, there's the added benefit that the sketch isn't overwhelmingly complicated, and a lot more readers will be willing to examine the code, and provide their recommendations. The conditions, of course, are that the sketch compiles, and that it demonstrates the problem that you're trying to solve.

Also, when someone tries to isolate the problem, it often turns out that the actual problem isn't what he thought it was. In the process of isolating coding problems, we learn troubleshooting skills.

So, before I delve into the 138 lines of code that remain, tell me: have you tested the new code that you posted? Does it compile, and demonstrate the problem? From your first post, it seems that the problem is that the readings from pin A2 don't seem to be correct. Is that right? If not, can you describe what that problem is?

Thank you for the clarification. I understand where you are coming from tho i was informed to post the whole sketch in the past which was due to the issue at hand. but that dosent matter now as you have enlightened me on how to do it the proper way or what is acceptable in the rules.

I have made additional changes and it dose compile and is as clean as can be in my opinion. there is always room for it to be reworked i know.

So to answer your question yes the code does demonstrate the problem that i am having. I am returning 0's from A2 the switch case is not catching its value of the pot.

here is the code with out unused declarations.

http://pastebin.com/9Ve9RjA9

OK. When I run that code, exactly as is, I find that the output on the serial monitor is 672 when I connect A2 to the 3.3V supply, 1023 when I connect A2 to the 5V supply, and whatever the last non-zero reading was when I connect to GND. Based on my reading of the code, that's what I'd expect.

Since the code appears to work, I suspect that there's a hardware problem. Here's a quick bit of code to test with:

void setup() {
  Serial.begin(9600);
}
void loop() {
  while(1) {
    Serial.println(analogRead(A2));
    delay(100);
  }
}

See what kind of readings you get with the potentiometer, and, if that fails, see what kind you get when you connect A2 directly to GND, 3.3V, and 5V. If you have a voltmeter, you might want to measure between A2 and GND as you rotate the potentiometer. Intuitively, I suspect that one end of the pot is connected to GND, and the other isn't connected to 5V.

Looking deeper into your code, here are some things to consider that I think might improve the code. Please consider your development plans before implementing these, as my suggestions might not be compatible with those plans:

GETMetVAL() sets variable metVal if the reading from A2 is low, or if variable mval is outside the range of an analog reading; if everything is well-behaved, it sets variable metronome to the analog reading. metVal isn't used in this code, and I can't find that it's used in any of the previous versions, either. What's its purpose?

I note that you read analog pin A2 twice in GETMetVAL(). That could lead to unexpected results when the ADC reading is close to 10. If the first reading comes up greater than 10, and the second reading comes up less, you'll assign a too-low value to metronome. If there's not a special reason to examine two ADC readings, you might want to consider storing the ADC reading in a variable, and do your tests and assignments from that variable.

In GETMetVAL(), mval is tested against the range of an analog input reading. mval is initialized to a value within that range, and is never assigned any value except an analog reading, in any of the three versions you posted. Unless you're intending to process mval in other ways as you develop the code, that test could be eliminated.

GETMetVAL() has a switch statement with only two cases - mval is in range, and it's not in range. Unless you want to add some other cases as you continue development, you could use a simple if ... else construction - if you want to keep that test at all. It would be more readable.

ReggieRetro: ... the proper way or what is acceptable in the rules.

"Rules" might be overstating it. If there's a rule, it's "post a complete sketch that demonstrates the problem," while "post a minimal sketch that illustrates the problem" is a "tip for getting the most out of your post." If you want to post a long and complicated sketch, I don't think you'll get complaints - but you might not get many participants, either. You'll note that you and I are the only ones here - others aren't clamoring to get in. Were they put off by the length and complexity of the original code? I can't say for sure. But, they certainly weren't lured here by it.

Thank you for your detailed direction i will be testing this tonight. all is noted thx very much for taking the time to help me out.

"In a perfect world" if i get the pot working how would i go about changing the TimerOne interval within the main loop?

And if you celebrate it Happy Easter!

After all of that... smh I had 3 10k pots in series and thats where i went wrong.. hooked directly to a common 5v and ground on the bread board it works. Thanks a bunch..

Now to change the interval to said reading from the pot for TimerOne still has me confused. not sure how to stop/resume initialize the timer in the main loop.

please shed some light oh wise and powerfull tmd3! lol

Joking aside honestly thanks!

Look into the TimerOne library, in Timer1.cpp. You’ll see that TimerOne:initialize() stops the timer, sets its mode, and then calls TimerOne:setPeriod(). setPeriod() makes calculations, then sets the timer registers, and then sets the clock select bits, which starts the timer.

By my reading of those functions, you can use either one to change the interval. You can call initialize() with a new setting, or set the period directly using setPeriod(). I’d pick setPeriod() for this purpose. Note that these ideas are based on only a cursory look at the code.