<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: tone()</title>
		<link>http://forums.leaflabs.com/topic.php?id=1291</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:07:07 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=1291" rel="self" type="application/rss+xml" />

		<item>
			<title>manitou on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-20767</link>
			<pubDate>Sun, 04 Nov 2012 10:26:47 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">20767@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Here is v2 of a snippet of code for tone that follows the Arduino implementation more closely.  It uses a timer/interrupt and any digital pin.  It also uses the funky duration logic of Arduino.&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;volatile static int toggle_count=0;
static int tone_pin;

HardwareTimer timer(2);

// freq in Hz    duration in ms
void tone(int pin, int frequency, int duration) {
	timer.pause();
	pinMode(pin,OUTPUT);
	tone_pin = pin;
	if (duration &#38;gt; 0 ) toggle_count = 2 * frequency * duration / 1000;
	 else toggle_count = -1;
//	timer.setPeriod(2*1000000/freq);   // in microseconds
        timer.setPrescaleFactor(CYCLES_PER_MICROSECOND);  // microseconds
        timer.setOverflow(1000000/frequency/2);
	timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
	timer.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
	timer.attachCompare1Interrupt(handler_tone);
	timer.refresh(); // start it up
        timer.resume();
}

void noTone(int pin) {
    timer.pause();
    digitalWrite(pin,LOW);
}

// ISR
void handler_tone(void) {
	if (toggle_count != 0){
    	togglePin(tone_pin);
	if (toggle_count &#38;gt; 0)  toggle_count--;
	} else noTone(tone_pin);
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>manitou on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-11191</link>
			<pubDate>Sat, 09 Jun 2012 20:11:48 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">11191@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Note that the pin/timer/channel mappings differ based on the version of Maple you  are using. My example was for an RET6.  Check the pin mappings for your Maple model.  (As noted, a more robust version of tone() could use the pin number to look up the timer/channel ....)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>compuslave on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-11183</link>
			<pubDate>Sat, 09 Jun 2012 07:53:25 +0000</pubDate>
			<dc:creator>compuslave</dc:creator>
			<guid isPermaLink="false">11183@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the code snippet, gbulmer. It would not build, though. I have been pretty busy/preoccupied lately as my wife and I's home burnt. We live in a motorhome, so it doesn't take much of a fire to set you back lol. Once we get our new living quarters, I'll be back on it, though. Thanks again! &#60;/p&#62;
&#60;p&#62;manitou, great info, thanks! I'll try this as well. I'm not sure why I have become so side tracked by a homegrown tone() function, but I have. I bet one thing for sure, when it's all done and tone() works, I'll know more about this board than when I started and I'll know things I doubt I would have learned doing any other project.&#60;/p&#62;
&#60;p&#62;Anyway, thanks guys! I didn't just leave, just been busy with thangs.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>manitou on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-10940</link>
			<pubDate>Mon, 28 May 2012 18:17:36 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">10940@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You chose to define TonePin as 9.  That won't work.  it has to be pin 24 so that the timer channel matches up with the pin.  You can choose another pin, but you'll need to look up the associated timer and channel and modify Tone() accordingly.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-10895</link>
			<pubDate>Sat, 26 May 2012 19:28:51 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">10895@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Have you tested the pin+speaker to prove it works?&#60;/p&#62;
&#60;p&#62;For example:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;void loop() {
    pinMode(TonePin,OUTPUT);
    for (int i=100; i&#38;lt;1000; i+= 50) {
        for (int j=0; j&#38;lt;500; j++) {
              digitalWrite(TonePin, HIGH);
              delayMicroseconds(i);
              digitalWrite(TonePin, LOW);
              delayMicroseconds(i);
         }
    }
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Does that make a falling tone?&#60;/p&#62;
&#60;p&#62;(WARNING: That code is not tested, or even compiled)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>compuslave on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-10876</link>
			<pubDate>Fri, 25 May 2012 18:42:54 +0000</pubDate>
			<dc:creator>compuslave</dc:creator>
			<guid isPermaLink="false">10876@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Well I tried a speaker, same thing. One tone no matter what I change. Changing pins changes the tone a little, but nothing huge. I have changed everything I can think of to make it sound different, and nothing. Duration variable seems useless as well. The pin variable is the only one that makes any change.&#60;/p&#62;
&#60;p&#62;Thanks for your help, though.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>compuslave on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-10874</link>
			<pubDate>Fri, 25 May 2012 17:28:00 +0000</pubDate>
			<dc:creator>compuslave</dc:creator>
			<guid isPermaLink="false">10874@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I suspected that may be the case and it appears so. &#60;a href=&#34;http://www.radioshack.com/product/index.jsp?productId=2062397&#34; rel=&#34;nofollow&#34;&#62;http://www.radioshack.com/product/index.jsp?productId=2062397&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Thanks for your help
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mikep on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-10873</link>
			<pubDate>Fri, 25 May 2012 16:59:37 +0000</pubDate>
			<dc:creator>mikep</dc:creator>
			<guid isPermaLink="false">10873@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Do you have a link to the RadioShack part? Most piezo's have the tone generator built in, they run off DC, and have a constant frequency. So you won't hear any change in tone with those. Also, piezos have usually a limited frequency range. You need a real speaker like this: &#60;a href=&#34;http://www.radioshack.com/product/index.jsp?productId=2062406&#34; rel=&#34;nofollow&#34;&#62;http://www.radioshack.com/product/index.jsp?productId=2062406&#60;/a&#62;&#60;br /&#62;
But don't hook it up the to the Maple directly, 8 Ohm resistance is too much current to handle for the maple, you need a transistor in between to amplify the Maple output current.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>compuslave on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-10860</link>
			<pubDate>Thu, 24 May 2012 22:44:32 +0000</pubDate>
			<dc:creator>compuslave</dc:creator>
			<guid isPermaLink="false">10860@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The only speaker I have is one of those 0-30V piezos from radio shack. I get the same tone, same duration no matter what I do with the variables. Hints?&#60;/p&#62;
&#60;p&#62;The sketch&#60;br /&#62;
[quote]&#60;br /&#62;
// for maple pwm pins associated with specific timer/channel&#60;br /&#62;
// pin 24   timer 4 channel 4&#60;br /&#62;
#define TonePin 9&#60;br /&#62;
#define DUTY 2    //2 50%   4 25% hi&#60;/p&#62;
&#60;p&#62;HardwareTimer timer(4);&#60;/p&#62;
&#60;p&#62;void setup() {&#60;br /&#62;
  }&#60;/p&#62;
&#60;p&#62;// freq in Hz    duration in ms&#60;br /&#62;
void tone(int pin, int freq, int duration) {&#60;br /&#62;
    timer.pause();&#60;br /&#62;
    pinMode(pin,PWM);&#60;br /&#62;
        timer.setPrescaleFactor(72);  // microseconds&#60;br /&#62;
        timer.setOverflow(1000000/freq);&#60;br /&#62;
    timer.setMode(TIMER_CH4,TIMER_PWM);&#60;br /&#62;
    timer.setCompare(TIMER_CH4,1000000/freq/DUTY);&#60;br /&#62;
        timer.setCount(0);      // probaby doesn't matter&#60;br /&#62;
    timer.refresh(); // start it up&#60;br /&#62;
        timer.resume();&#60;br /&#62;
    delay(duration);&#60;/p&#62;
&#60;p&#62;    timer.pause();&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop(){&#60;/p&#62;
&#60;p&#62;tone(9, 2000, 200);&#60;br /&#62;
delay(500);&#60;br /&#62;
}&#60;br /&#62;
[/quote]
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-9815</link>
			<pubDate>Sun, 18 Mar 2012 14:47:39 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">9815@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;feurig - strongly agree with you.&#60;br /&#62;
A few 'Goldilocks' examples (not too big, not too small, not too hard, not too easy :-)  that do interesting things motivate learning.&#60;/p&#62;
&#60;p&#62;good += HUGE_GOODNESS
&#60;/p&#62;</description>
		</item>
		<item>
			<title>feurig on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-9006</link>
			<pubDate>Sat, 17 Mar 2012 01:37:32 +0000</pubDate>
			<dc:creator>feurig</dc:creator>
			<guid isPermaLink="false">9006@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thats a lot nicer than arduino/wirings tone which arbitrarily steals timer resources in the background. I think examples like this which are small enough though still requiring a little work to understand are vastly better than everything hidden. &#60;/p&#62;
&#60;p&#62;++good.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-8654</link>
			<pubDate>Thu, 15 Mar 2012 13:47:29 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">8654@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;manitou, nice!&#60;/p&#62;
&#60;p&#62;hardware tone() is the way to go.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>manitou on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-8592</link>
			<pubDate>Mon, 12 Mar 2012 13:49:10 +0000</pubDate>
			<dc:creator>manitou</dc:creator>
			<guid isPermaLink="false">8592@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Here's a snippet of code for tone() that I used to test some of the arduino sketches on the RET6.  A more general version would check that pin provided is PWM and then lookup the corresponding timer/channel&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;// for maple pwm pins associated with specific timer/channel
// pin 24   timer 4 channel 4
#define TonePin 24
#define DUTY 2    //2 50%   4 25% hi

HardwareTimer timer(4);

// freq in Hz    duration in ms
void tone(int pin, int freq, int duration) {
    timer.pause();
    pinMode(pin,PWM);
        timer.setPrescaleFactor(72);  // microseconds
        timer.setOverflow(1000000/freq);
    timer.setMode(TIMER_CH4,TIMER_PWM);
    timer.setCompare(TIMER_CH4,1000000/freq/DUTY);
        timer.setCount(0);      // probaby doesn&#38;#39;t matter
    timer.refresh(); // start it up
        timer.resume();
    delay(duration);

    timer.pause();
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>gbulmer on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-7812</link>
			<pubDate>Fri, 20 Jan 2012 14:44:48 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">7812@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;kenji - you could try to use the timer setPeriod function described at&#60;br /&#62;
&#60;a href=&#34;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer&#60;/a&#62;&#60;br /&#62;
with setCount.&#60;/p&#62;
&#60;p&#62;there are multiple timers, so you could have independent tones (one frequency per timer) on different pins.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>kenji on "tone()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1291#post-7798</link>
			<pubDate>Thu, 19 Jan 2012 20:14:47 +0000</pubDate>
			<dc:creator>kenji</dc:creator>
			<guid isPermaLink="false">7798@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi,&#60;/p&#62;
&#60;p&#62;when implement tone() Schedule? &#60;/p&#62;
&#60;p&#62;Thanks
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
