<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hmmm.... &#187; piezo</title>
	<atom:link href="http://michael.thegrebs.com/tag/piezo/feed/" rel="self" type="application/rss+xml" />
	<link>http://michael.thegrebs.com</link>
	<description></description>
	<lastBuildDate>Thu, 27 Oct 2011 18:48:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Playing a Tone Through an Arduino Connected Piezo</title>
		<link>http://michael.thegrebs.com/2009/03/23/playing-a-tone-through-an-arduino-connected-piezo/</link>
		<comments>http://michael.thegrebs.com/2009/03/23/playing-a-tone-through-an-arduino-connected-piezo/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 02:27:52 +0000</pubDate>
		<dc:creator>mikegrb</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[piezo]]></category>

		<guid isPermaLink="false">http://michael.thegrebs.com/?p=214</guid>
		<description><![CDATA[I&#8217;m working on a top secret project, all I&#8217;ll say is it involves the Arduino, Twitter, and the refrigerator, and needed to play a tone through a piezo connected to the Arduino. I looked around and the closest I could &#8230; <a href="http://michael.thegrebs.com/2009/03/23/playing-a-tone-through-an-arduino-connected-piezo/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a top secret project, all I&#8217;ll say is it involves the Arduino, Twitter, and the refrigerator, and needed to play a tone through a piezo connected to the Arduino.  I looked around and the closest I could find was <a href="http://www.arduino.cc/en/Tutorial/PlayMelody">a tutorial  that included code to play a melody</a>.</p>
<p>This wasn&#8217;t quite what I wanted and the playTone function expects the notes and timing to be in global variables which makes the baby jesus cry.  The premise was simple though.  Take the period and divide by two then bring the output high then low for this amount of time, repeat to make up the desired duration.</p>
<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> pinSpeaker <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">void</span> setup <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; pinMode<span style="color: #009900;">&#40;</span>pinSpeaker<span style="color: #339933;">,</span> OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> loop <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; playTone<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">750</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; delay<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">750</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// duration in mSecs, frequency in hertz</span><br />
<span style="color: #993333;">void</span> playTone<span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> duration<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> freq<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; duration <span style="color: #339933;">*=</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> period <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color:#800080;">1.0</span> <span style="color: #339933;">/</span> freq<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #0000dd;">1000000</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">long</span> elapsed_time <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>elapsed_time <span style="color: #339933;">&lt;</span> duration<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite<span style="color: #009900;">&#40;</span>pinSpeaker<span style="color: #339933;">,</span>HIGH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; delayMicroseconds<span style="color: #009900;">&#40;</span>period <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; digitalWrite<span style="color: #009900;">&#40;</span>pinSpeaker<span style="color: #339933;">,</span> LOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; delayMicroseconds<span style="color: #009900;">&#40;</span>period <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; elapsed_time <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>period<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This simple sketch plays a 500Hz tone for 750 mSec, pauses 750 mSec and repeats.  To use it in your sketch simply define pinSpeaker and set it for output, copy playTone to your sketch and call it with the duration in milliseconds and the frequency in hertz.</p>
]]></content:encoded>
			<wfw:commentRss>http://michael.thegrebs.com/2009/03/23/playing-a-tone-through-an-arduino-connected-piezo/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

