Pages: [1]   Go Down
Author Topic: frequency to period question  (Read 418 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 23, 2010, 01:18:26 pm by stephen_t » Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: