Go Down

Topic: frequency to period question (Read 461 times) previous topic - next topic

modius13

Hey all,

ok, I know that the frequency of a note is cycles/second and to implement that with the Arduino (turning a speaker on/off a certain number of times every second) you need to do a:
Code: [Select]

digitalWrite(notePin, HIGH);
delay x;
digitalWrite(notePin, LOW);
delay x;


repeat ad nauseum where x is half of the period, unless you're doing some fun stuff with the pulse width and don't want a 50% duty cycle... but I get ahead of myself...

Now, in theory, I can do the calculations to get the period. and I can convert it to microseconds so it's easier to work with in the program and divide by two to get how much on and how much off.

I'm not getting the same numbers as used in the Arduino Synth page on the playground http://www.arduino.cc/playground/Main/PbSynthCode

here's the equations I'm using:
period = 1/frequency
period in microseconds = period * 1000000
time on = time off = period in microseconds / 2

Am I doing something wrong or missing something?

pluggy

#1
Apr 23, 2010, 08:14 pm Last Edit: Apr 23, 2010, 08:18 pm by stephen_t Reason: 1
If you use Arduino 0018, it has a tone function that makes this stuff very easy, you just tell it which pin, the frequency and the duration.

http://arduino.cc/en/Reference/Tone
http://pluggy.is-a-geek.com/index.html

modius13

Aye, tone is a nice idea, but is there a way to do PWM with the tone function?

Go Up