<?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>Ayarafun Factory &#187; Arduino-Tutorial</title>
	<atom:link href="http://www.ayarafun.com/category/embeded-system/arduino-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ayarafun.com</link>
	<description>Make , Creative and Let &#039;s fun</description>
	<lastBuildDate>Thu, 17 May 2012 00:00:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>[Sciguy14] Arduino Tutorial 11: SD Cards and Datalogging</title>
		<link>http://www.ayarafun.com/2011/05/sciguy14-arduino-tutorial-11-sd-cards-and-datalogging/</link>
		<comments>http://www.ayarafun.com/2011/05/sciguy14-arduino-tutorial-11-sd-cards-and-datalogging/#comments</comments>
		<pubDate>Thu, 05 May 2011 02:53:42 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[datalogger]]></category>
		<category><![CDATA[datalogging]]></category>
		<category><![CDATA[fat32]]></category>
		<category><![CDATA[SCIGUY14]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3543</guid>
		<description><![CDATA[Sd-card ทำง่ายๆ บอกได้ว่า arduino นิ ติดต่อ sd-card ได้ง่ายมากๆ เพราะว่า arduino มี libary ที่ทดสอบมาแล้ว 
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">มาแล้วครับ Tutorial อันที่ 11 รอบนี้ เป็นคิวของ sd-card ครับ สำหรับคนที่กำลังตามหา datalogger อยู่ ผมบอกได้ว่า arduino นิ ทำ datalogger ง่ายมากๆ เพราะว่าภายใน arduino มันมี libary มาแล้ว และรับรองโดยตัว website เขาเองสักด้วย ถ้ามีคนทำเป็นชุดสำเร็จได้ นี้น่าจะดี</p>
<p style="text-align: justify;">สำหรับใครที่มองหาวิธีอยู่ ลองดูวีดีโอนี้ก่อนครับ ผมว่าจะช่วยให้เห็นว่าใช้ arduino ไปบันทึกข้อมูลจาก sd-card ได้อย่างไง และเอาข้อมูลไปใช้งานได้อย่างไง</p>
<p>โอเคครับ สำหรับวันว่างนี้ ลองเอาไปเล่นกันดูครับ เดี่ยวทางผมจะเอาลองเลยเล่น แล้วจะมาเล่าให้ฟังอีกรอบครับ</p>
<p style="text-align: center;">
<p><a href="http://www.youtube.com/watch?v=5v5A3j7Rrco">http://www.youtube.com/watch?v=5v5A3j7Rrco</a></p>
</p>
<p style="text-align: justify;">สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/05/sciguy14-arduino-tutorial-11-sd-cards-and-datalogging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Tutorial 3 : Digital input ,Debounce</title>
		<link>http://www.ayarafun.com/2011/04/arduino-tutorial-3-digital-input-and-debounce/</link>
		<comments>http://www.ayarafun.com/2011/04/arduino-tutorial-3-digital-input-and-debounce/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 17:44:42 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[bounce]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[debounce]]></category>
		<category><![CDATA[Pull down resister]]></category>
		<category><![CDATA[Pull Up resistor]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3501</guid>
		<description><![CDATA[มาถึงตอนที่ 3 arduino กับสวิตซ์ สวิตซ์มักดูเป็นของง่ายๆ แต่ใช้งานจริงมักจะเจอปัญหา มาลองอ่านบทความชุดนี้ดูดีกว่า เผื่อจะไขปริศนาปัญหา กับไมโครฯ ให้กับพวกท่านได้ครับ
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">สำหรับในตอนนี้ ผมจะเพิ่มเติมวงจรอิเล็กทรอนิกส์ลงไปบ้างครับ การเขียนโปรแกรมไมโครคอนโทรลเลอร์ มันจะต่างจากการเขียนโปรแกรมบนคอมพิวเตอร์ ตรงส่วนนี้ล่ะครับ คือเราจะได้เรียนรู้เรื่อง hardware อิเล็กทรอนิกส์ด้วยครับ ในโลกไมโครอิเล็กทรอนิกส์ เป็นการเรียนรู้ ไม่รู้จบจริง มันจะมีเทคโนโลยีใหม่ มาให้เราต้องไปเรียนรู้เรื่อยๆ (อยู่ใช้มาหลายปี ผมบอกได้ว่าตามได้ ไม่หมดครับ แต่สนุก)</p>
<p style="text-align: justify;">วันนี้เราจะพาเพื่อนชาวเวป นำไมโครคอนโทรลเลอร์ไปใช้งานกับ ปุ่มกดกันนะครับ สำหรับปุ่มหรือสวิตซ์ เป็นสิ่งที่คุ้นเคยกันดี ในอุปกรณ์อิเล็กทรอนิกส์ที่เห็น อย่างเช่น มือถือเราก็มีปุ่ม  ตู้ ATM ก็มีปุ่มหรือสวิตซ์ ซึ่งก็ถือว่าสวิตซ์เป็นเรื่องพื้นฐานมากๆ การสร้างเครื่องมือทางอิเล็กทรอนิกส์ครับ ได้ใช้งานแน่ๆ</p>
<p style="text-align: justify;"><img class="aligncenter size-full wp-image-3503" title="button-pic1" src="http://www.ayarafun.com/wp-content/uploads/2011/04/button-pic1.jpg" alt="" width="600" height="150" />สวิตซ์ คือ อุปกรณ์ที่ ตัด-ต่อ ไฟฟ้า เข้าถึงกัน เหมือนเป็นสะพานครับ ยกขึ้น ไฟไหลผ่านไม่ได้ แต่เหมือนกดลงไฟไหลผ่านได้ ซึ่งมีหลายแบบ ไม่ว่าแบบกด แบบโยก มาดูตัวอย่างสวิตซ์ ที่ใช้กันบ่อยๆ นะครับ</p>
<ul>
<li><strong>Button </strong>หรือปุ่มกดแบบโรงงาน มันจะมีขนาดใหญ่ และ ทนทานมาก</li>
<li><strong>Tact switch </strong>เป็นปุ่มกด พบได้บนแผงวงจรอิเล็กทรอนิกส์ บนบอร์ด arduino จะมีเป็นปุ่มสำหรับ Reset</li>
<li><strong>Micro Switch </strong>หรือ บางที่เรียกว่า Limit Switch อันนี้จะเห็นมากที่ใช้ในเครื่องจักร</li>
</ul>
<p style="text-align: left;"><img class="aligncenter size-full wp-image-3504" title="button-pic2" src="http://www.ayarafun.com/wp-content/uploads/2011/04/button-pic2.jpg" alt="" width="600" height="150" />และ ยังมี อุปกรณ์อื่นๆ ที่ทำงานเหมือนเป็นสวิตซ์ ได้อีกด้วย เช่น</p>
<ul>
<li><strong>Mercury Switch</strong> หรือบางที่เรียกสวิตซ์ปรอท จะใช้ปรอทเป็นตัวนำไฟฟ้า สำหรับสวิตซ์แบบนี้ ถูกประยุกต์ไปใช้ตรวจสอบความเอียงได้ครับ คือเอียงมายังตำแหน่ง ปรอทไหลไปชนขั้วไฟฟ้าให้เชื่อมติดกัน ทำให้ไฟฟ้าไหลผ่านได้</li>
<li><strong>Reed Switch</strong> ตัวนี้จะเป็นสวิตซ์แม่เหล็ก คือใช้หลักการง่ายๆ ถ้า Switch นี้อยู่ใกล้แม่เหล็ก ขั้วเหล็กจะติดกัน เห็นในประตูหรืออุปกรณ์กันขโมยใช้บ่อยๆ</li>
<li>เรายังดัดแปลง อุปกรณ์หลายๆอย่างที่ทำงาน อย่างเช่นว่าเราสามารถใช้เหรียญบาท มาดัดแปลงเป็น สวิตซ์หาความเอียง ได้ครับ</li>
</ul>
<h4><span id="more-3501"></span><strong>เอาสวิตซ์ไปใช้งานได้อย่างไง</strong><strong> </strong></h4>
<ul>
<li>สิ่งแรกที่เราต้องทำความเข้าใจ เราจะแบ่งสถานะทางดิจิตอล ได้เพียงสองสถานะ  คือ High หรือ Low หรือ ON &#8211; OFF , หรือ บางที่จะเรียกสัญญาณทาง ดิจิตอลว่า  “0” กับ “1”</li>
<li>ในทางกายภาพแล้ว เราจะแบ่งสถานะทางดิจิตอล ด้วยแรงดันไฟ</li>
<li>สำหรับ Arduino จะใช้แรงดัน คือ “HIGH” คือ ไฟระดับ 5 Volt และ “LOW”  คือ ไฟระดับ 0 Volt</li>
<li>ดังนั้น สิ่งที่เราต้องทำ คือ เปลี่ยนการกดสวิตซ์ ให้เป็นการเปลี่ยนแรงดันสักก่อน</li>
</ul>
<h4><strong>วงจร Pull-up ,Pull-down<br />
</strong></h4>
<p style="text-align: justify;">สถานะที่กำหนดให้ขาของอุปกรณ์อิเล็กทรอนิกส์ รอรับอินพุท (INPUT) ขาพอร์ตจะเป็น High Impedance คือมีความต้านทานสูงมากต่ออยู่ ทำให้ขาพอร์ตนั้น เสมือนถูกปล่อยลอย ค่าอินพุทที่อ่านกลับมาได้ มันไม่แน่นอน</p>
<p style="text-align: justify;">ดังนั้นในงานขาพอร์ตอินพุท วงจรของสวิตซ์ จำเป็นมากที่ต้องมี <strong>Pull-up Resistor</strong> หรือ<strong> Pull-down Resistor</strong> เพื่อที่จะกำหนดสภาวะดิจิตอลที่แน่นอน ให้กับอุปกรณ์อิเล็กทรอนิกส์ (ไม่ได้ เฉพาะเจาะจงว่าเป็นแค่ arduino นะครับ ทุกอุปกรณ์อิเล็กทรอนิกส์เลยที่บอกว่าขาอินพุทเป็น High Impedance)</p>
<p>ตามปกติ ตัวต้านทานที่ใช้ในวงจร  Pull-up หรือ Pull-down จะใช้ประมาณ 5k Ohm -20k Ohm</p>
<p style="text-align: justify;"><img class="aligncenter size-full wp-image-3527" title="Pullup_Resistor" src="http://www.ayarafun.com/wp-content/uploads/2011/04/Pullup_Resistor.png" alt="" width="222" height="314" /><strong>Pull-up Resistor</strong> คือการนำตัวต้านทานต่อเข้ากับ Vcc (+5V) เพื่อให้แรงดันอยู่คงที่ ทำให้อยู่ในสถานะ &#8220;HIGH&#8221; หรือ “1” ตลอดเวลา และเมื่อกดสวิตซ์ กระแสไฟฟ้าจะไหลลง Ground ทันที ซึ่งทำให้สถานะเป็นลอจิก &#8220;LOW&#8221; หรือ “0” และ การทำงานลักษณะนี้ จะ<strong>เรียกว่า Active Low </strong>เพราะว่าจะเขียนโปรแกรมที่ทำงาน เมื่อลอจิกเป็น &#8220;LOW&#8221; ส่วนใหญ่ เราจะเห็นต่อสวิตซ์ นิยมใช้แบบ Pull-up มากกว่า</p>
<p><img class="size-full wp-image-3526 alignnone aligncenter" title="Pulldown_Resistor" src="http://www.ayarafun.com/wp-content/uploads/2011/04/Pulldown_Resistor.png" alt="" width="226" height="297" /></p>
<p style="text-align: justify;"><strong>Pull-down Resistor</strong> โดยใน Pull-down จะมีลักษณะคล้ายกับ Pull-up Resistor แตกต่าง ตรงที่ สภาวะปกติของ Pull-down จะเป็นลอจิก &#8220;LOW&#8221; หรือ “0”  เมื่อมีการกดปุ่ม กระแสไฟจะไหลเข้าขาอินพุท ทำให้ ลอจิกเป็น &#8220;HIGH&#8221; หรือ &#8220;1&#8243; ได้ การทำงานในลักษณะนี้ <strong>จะเรียกว่า Active High</strong></p>
<h3><strong>ทำการทดลองกันครับ</strong></h3>
<h6><strong>&lt;Lab-7&gt; สวิตซ์เบื้องต้น</strong></h6>
<p>Sketch แรกสำหรับตอนนี้ เราจะลองทำสวิตซ์ไฟแบบ กดติดปล่อยดับ ง่ายก่อน โดยวงจรสวิตซ์ เราจะต่อแบบ Pull Up resistor เอาไว้ ดังรูป</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-3505" title="switch_bb" src="http://www.ayarafun.com/wp-content/uploads/2011/04/switch_bb.png" alt="" width="609" height="271" /></p>
<p style="text-align: justify;">โดยวงจรที่ใช้ จะมีการ Pull-Up ตัวต้านทาน เอาไว้ ระดับแรงดัน ในสภาวะปกติ จะอยู่ที่ 5 โวลต์ หรือ ลอจิก “1” แต่ถ้ามีการกดปุ่ม แรงดันจะอยู่ที่ 0 โวลต์ หรือ ลอจิก “0” หรือ เรียกว่า active Low ครับ</p>
<p style="text-align: justify;">เราสามารถจากสถานะการกดสวิตซ์นี้ เราสามารถเอาไปทำโปรแกรม ได้ดังตัวอย่างนี้ครับ</p>
<pre class="brush: cpp">int buttonPin = 2;
int ledPin =  13;
boolean buttonState = 0;

void setup() {
      pinMode(ledPin, OUTPUT);
      pinMode(buttonPin, INPUT);
}

void loop()
      buttonState = digitalRead(buttonPin);

      if (buttonState == HIGH) {
         digitalWrite(ledPin, LOW);
      } else {
         digitalWrite(ledPin, HIGH);
      }
}</pre>
<p style="text-align: justify;">ตามตัวอย่าง ผมได้เขียน การปกติเมื่อไม่มี การกดปุ่ม ไฟ LED จะไม่สว่าง แต่ถ้ามีการปุ่มกด LED จะติดขึ้นครับ แต่เมื่อปล่อยมันก็ดับ</p>
<p style="text-align: justify;">เราต้องกำหนดขาพอร์ต buttonPin ให้เป็น อินพุท (INPUT) ด้วย (ตามปกติ ตอนเริ่มต้น ถ้าไม่มีการกำหนดใดๆ ในโปรแกรม บอร์ด Arduino จะกำหนดให้ทุกขา อินพุท(INPUT) อยู่แล้ว เราจะใส่หรือไม่ใส่คำสั่งนี้ก็ได้ครับ)</p>
<p>โดยใช้คำสั่ง pinMode ();</p>
<div>
<table width="588" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="31">[1]</td>
<td valign="top" width="120"><strong>pinMode(pin) </strong></td>
<td valign="top" width="312">ปกติพอร์ตจะถูกกำหนดให้เป็น INPUT อ่านค่าสถานะ จาก pin ส่งค่ากลับมาเป็น HIGH หรือ LOW</td>
</tr>
</tbody>
</table>
</div>
<p>การอ่านสถานะดิจิตอล จะใช้คำสั่ง</p>
<div>
<table width="588" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="31">[ 2]</td>
<td valign="top" width="120"><strong>int digitalRead(pin)</strong></td>
<td valign="top" width="312">อ่านค่าสถานะ จาก pin ส่งค่ากลับมาเป็น HIGH หรือ LOW</td>
</tr>
</tbody>
</table>
</div>
<p style="text-align: justify;">คำสั่ง <strong>digitalRead</strong> จะส่งค่ากลับมาครับ (หรือเรียกว่า จะ Return ค่ากลับมา) เวลาใช้งานเราก้อควรจะสร้างตัวแปร ไว้รับค่าด้วย ตามตัวอย่าง ผมจะสร้าง ตัวแปรชื่อ buttonState ไว้รับค่า(return)</p>
<div>
<table width="587" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="463">
<p style="text-align: center;">buttonState = digitalRead(buttonPin);</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>เราจะได้สถานะการกดหรือปล่อย สวิตซ์มาแล้วอยู่ใน ตัวแปร buttonState ซึ่งในคราวนี้ก้ออยู่ที่ว่าเราสถานะนี้เอาทำงานอะไรต่อไป</p>
<h6><strong>&lt;Lab-8&gt; สวิตซ์ควมคุมความเร็วไฟกระพริบ</strong></h6>
<p style="text-align: justify;">หลายคนคงคิดถึงวงจรไฟกระพริบ รอบนี้ผมเลยเอาตัวอย่างไฟกระพริบมาประยุกต์ เพิ่มเติมกับสวิตซ์ ในครั้งนี้ เราจะเอาค่าสถานะการกด-ปล่อย มาใช้ในควบคุมความเร็วของไฟกระพริบ กดปุ่มจะกระพริบเร็วขึ้น ปล่อยจะกระพริบด้วยความเร็วปกติ สำหรับวงจร เราใช้ต่อเนื่องจากแล๊ปทดลองที่ 7 ต่อได้เลยครับ</p>
<pre class="brush: cpp">int ledPin =  13;
int buttonPin = 2;

boolean buttonState;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  int timeDelay;
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH)  {
      timeDelay =1000;
   }  else  {
      timeDelay = 100;
   }
   blinky(timeDelay);

}

void Blinky(int time) {
   digitalWrite(ledPin ,HIGH);
   delay(time);
   digitalWrite(ledPin ,LOW);
   delay(time);
}</pre>
<p style="text-align: justify;">จากเห็นว่าโครงสร้างโปรแกรมจะคล้ายกับแล๊ป 7 นะครับ คืออ่านค่าสถานะสวิตซ์กลับมา แต่ในรอบนี้เราเอาสถานะสวิตซ์ไป เปลี่ยนค่าเวลาที่ใช้ delay ในโปรแกรมไฟกระพริบครับ หวังว่าจะเห็นภาพการอ่านค่าสถานะของการกดสวิตซ์ไปใช้งานกันแล้วนะครับ</p>
<p style="text-align: justify;">
<h6><strong>&lt;Lab-9&gt; Toggle Switch </strong></h6>
<p style="text-align: justify;">มาลองทำสวิตซ์แบบขั้นสูง (Advance) กันบ้าง สำหรับสวิตซ์ที่จะทำเป็นแบบ Toggle ครับ คือ กดติด กดดับ การทำงานคือกดหนึ่งครั้ง ไฟ LED จะติด และพอปล่อย ไฟจะยังไม่ดับ ยังติดต่อไป แต่ถ้าเรากดปุ่มอีกครั้ง ไฟจะดับ และก็ยังดับต่อไป</p>
<p>Toggle เป็นสวิตซ์ที่จำค่าสถานะการกด ไว้นะครับ กดหนึ่งครั้งเปลี่ยนจากดับเป็นติด และ กดอีกหนึ่งครั้งจะเปลี่ยนจากติดเป็นดับ วิธีการเขียนตามตัวอย่างเลย</p>
<pre class="brush: cpp">int ledPin = 13;
int buttonPin = 2;

boolean buttonState;
boolean lastState;
boolean state = HIGH;

void setup() {
  pinMode (buttonPin,INPUT);
  pinMode (ledPin,OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if ( ( buttonState == LOW) &amp;&amp; (lastState == HIGH) ) state = !state;

  digitalWrite(ledPin,state);

  lastState = buttonState;
 }</pre>
<p style="text-align: justify;">เห็นว่า Sketch ไม่ได้ Advance อะไรมากครับ เพียงแค่เปลี่ยนจากการกดแล้วเอาไปเปิด-ปิด ไฟ LED เอาไป เก็บค่าสถานะ ของหลอดไฟ</p>
<p style="text-align: justify;">โดยใน Sketch นี้ ผมจะสร้างตัวแปรที่ชื่อ state เพิ่มมาอีกหนึ่งตัว เมื่อใดที่มีการกดปุ่ม state จะถูกกลับเป็น state ตรงข้าม จาก High เปลี่ยนเป็น Low หรือ จาก Low เปลี่ยนเป็น High โดยคำสั่ง</p>
<div>
<table width="605" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="568">
<p style="text-align: center;"><strong>state = !state;</strong><strong> </strong></p>
</td>
</tr>
</tbody>
</table>
</div>
<p style="text-align: justify;">และเก็บค่า buttonState สุดท้ายไว้ใน lastState ด้วย เพื่อใช้ในการตรวจสอบว่า ค่าที่เกิดจากการกดในรอบนี้ ที่เป็นสัญญาณลอจิก &#8220;LOW&#8221; เกิดจากสภาวะครั้งที่แล้ว ที่ยังไม่มีการกดปุ่ม สถานะจะเป็น &#8220;HIGH&#8221; อย่างแน่นอน เพื่อให้อ่านค่าการกดปุ่มเพียงครั้งเดียวครับ</p>
<p style="text-align: justify;">หลังจากที่ upload sketch เข้าไปทดลอง จะเห็นว่า เราสามารถทำสวิตซ์แบบ toggle ได้แล้ว คือกดติด กดดับ</p>
<p style="text-align: justify;"><strong>แต่ช้าก่อนนะครับ หลังจากทดลองจะเห็นว่า มันไม่ได้ทำงานเหมือน </strong><strong>sketch ที่เราเขียนเลย หรือเราว่าเขียนโปรแกรมผิค</strong></p>
<p style="text-align: justify;">ความจริงการทำงานโค๊ดนี้ เขียนถูกต้องครับ แต่เกิดสิ่งที่เราไม่ได้คาดไว้คือ สวิตซ์ทุกชนิด ที่มีหน้าสัมผัส (contact) เป็นโลหะ มักจะมีปัญหาการ bounce ของสัญญาณ คืออาการไกว่ของสัญญาณ ซึ่งจะเกิดในสั้นๆ 5-50 นาโนวินาที สาเหตุหนึ่งที่ทำให้เกิด ในช่วงที่กดปุ่ม หน้า contact จะสัมผัสไม่แนบสนิท จะเกิดสัญญาณ bounce คืออาการที่สัญญาณจะสลับ เป็น High หรือ Low อย่างรวดเร็ว สังเกตจากกราฟ  ก่อนที่จะเข้าสู่สภาวะเสถียร</p>
<p style="text-align: justify;"><a href="http://www.cs.brown.edu/~tld/courses/cs148/02/sensors.html"><img class="aligncenter size-full wp-image-3502" title="รูปอาการ bounce ของสัญญาณ  " src="http://www.ayarafun.com/wp-content/uploads/2011/04/bounce.gif" alt="" width="432" height="317" /></a> sketch นี้จึงทำงานเห็น กดติดบ้าง ไม่ติดบ้าง เนื่องจากไม่ได้ เขียนส่วนป้องกันการ bounce ไว้ จะเห็นได้ว่าการเขียนโปรแกรมบนไมโครฯ จะไม่เหมือนเขียนใช้งานคอมพิวเตอร์ ในส่วนนี้ล่ะครับ เพราะว่าจะมีส่วนของ Hardware มาเกี่ยวข้องด้วย</p>
<p style="text-align: justify;">
<h6><strong>&lt;Lab-10&gt; Toggle Switch แบบปรับปรุง</strong></h6>
<p style="text-align: justify;">เรามาหา วิธีการแก้ปัญหา bounce ของสัญญาณกันดีกว่า สำหรับ อาการ bounce คือการที่สัญญาณยังไม่นิ่ง เกิดการเหวี่ยงในช่วงสั่นๆ ระยะเวลาการเกิด 5-50 นาโนวินาที และ เกิดช่วงเริ่มต้นของการกดสวิตซ์</p>
<p style="text-align: justify;"><strong>วิธีการแก้ไขที่ง่ายที่สุด</strong> คือ หลังจากหน้า contact หรือกดปุ่มในครั้งแรก arduino จะรับสัญญาณการกดปุ่มได้ แต่มันยังไม่แน่ใจว่า สวิตซ์โดนกดจริงหรือป่าว ให้รออีกสัก 5-50 นาโนวินาที แล้วตรวจสอบสถานะการกดปุ่มอีกรอบ ถ้ามีการกดจริง สัญญาณดิจิตอลที่อ่านได้ อยู่ในช่วงที่เสถียร ซึ่งค่าที่ออกมาจะบอกได้ว่ากด แต่ถ้าตรวจสอบอีกที่แล้วไม่พบ แสดงว่าสัญญาณที่ได้รับ เป็น noise ในระบบ ดังนั้น sketch ใหม่ จึงได้ดังนี้</p>
<pre class="brush: cpp">int ledPin = 13;
int buttonPin = 2;

boolean buttonState;
boolean lastState;
boolean state = HIGH;

 void setup() {
  pinMode (buttonPin,INPUT);
  pinMode (ledPin,OUTPUT);
}

void loop() {
  reading = digitalRead(buttonPin);

  if ( reading == LOW &amp;&amp; lastState == HIGH ) {
    delay (10);
    if (digitalRead(buttonPin) == LOW) state = !state;
  }

  digitalWrite(ledPin,state);
  lastState = reading;

}</pre>
<p style="text-align: justify;">เห็นว่า เมื่อเราเข้าใจปัญหา เราจะเขียนโปรแกรมได้แก้ง่ายมากๆ ผมน่าจะบอกตั้งแต่แรก ฮ่าๆ ผมเป็นพวกชอบแกล้งคนครับ ให้โดนเยอะๆ จะได้เอาไปโม้เพื่อนๆ ต่อได้<br />
สำหรับรอบนี้ ก็คงพอจะเข้าใจวิธีการใช้ไมโคร และใช้งาน arduino เพิ่มขึ้นอีกนิดแล้ว ก็มีปัญหาไม่เข้าใจส่วนไหน ติชม หรือ comment เข้ามาได้นะครับ เจอกันโอกาสหน้า บายยย ^_^</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/04/arduino-tutorial-3-digital-input-and-debounce/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>[Sciguy14] Arduino Tutorial 10: Interrupts and Hardware Debouncing</title>
		<link>http://www.ayarafun.com/2011/04/sciguy14-arduino-tutorial-10-interrupts-and-hardware-debouncing/</link>
		<comments>http://www.ayarafun.com/2011/04/sciguy14-arduino-tutorial-10-interrupts-and-hardware-debouncing/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 04:44:29 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[SCIGUY14]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3415</guid>
		<description><![CDATA[Jeremy Blum ได้อธิบายการใช้งาน Interupts ใน Arduino ให้เข้าใจง่าย ลองกันครับ
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">วีดีโอ Tutorial ตัวใหม่มาอีกแล้วครับ รอบนี้ Jeremy Blum สอนให้ใช้งาน Interupts ซึ่งเป็นเรื่องที่เข้าใจยาก อย่างหนึ่งในการเขียนโปรแกรมไมโครฯ ครับ แต่สำหรับ Arduino ทำให้เรื่องนี้ดูง่ายมาก ใครตามมาถึง 10 ตอนแล้ว ผมว่า การใช้ Arduino น่าจะเป็นเรื่องสนุกกันแล้วใช่ไหมครับ</p>
<p style="text-align: center;">
<p><a href="http://www.youtube.com/watch?v=CRJUdf5TTQQ&#038;fmt=18">http://www.youtube.com/watch?v=CRJUdf5TTQQ</a></p>
</p>
<blockquote>
<p style="text-align: justify;"><a title="Interrupts on Wikipedia" href="http://en.wikipedia.org/wiki/Interrupt" target="_blank">Interrupts</a> – An extremely useful, yet often feared element of microprocessors.   Interrupts allow you run a program, while still being able to react to  asynchronous input from the outside world.  On many platforms then can  be confusing to implement, but the arduino makes it easy!  In this  week’s episode, I’ll show you how to use a hardware-debounced button to  activate a <a title="Interrupts at arduino.cc" href="http://www.arduino.cc/en/Reference/AttachInterrupt" target="_blank">hardware interrupt on the arduino</a>.  I’d suggest you go check out <a title="Arduino Tutorial 2" href="http://jeremyblum.com/2011/01/10/arduino-tutorial-2-now-with-more-blinky-things/" target="_blank">episode 2</a>, where I initially introduced button debouncing, if you haven’t already.  I won’t be covering <a title="Timers at arduino.cc" href="http://www.arduino.cc/playground/Code/Timer1" target="_blank">timer interrupts</a> in this episode, since I recently wrote an <a title="Arduino Timer Interrupts" href="http://jeremyblum.com/2010/09/05/driving-5-speakers-simultaneously-with-an-arduino/" target="_blank">extensive blog post about using them</a>.  Enjoy the video!</p>
</blockquote>
<p style="text-align: justify;">สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/04/sciguy14-arduino-tutorial-10-interrupts-and-hardware-debouncing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Sciguy14] Arduino Tutorial 08: SPI Interfaces</title>
		<link>http://www.ayarafun.com/2011/03/sciguy14arduino-tutorial-8-spi-interfaces/</link>
		<comments>http://www.ayarafun.com/2011/03/sciguy14arduino-tutorial-8-spi-interfaces/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 13:00:09 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[AD5204]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[potentiometer]]></category>
		<category><![CDATA[SCIGUY14]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3158</guid>
		<description><![CDATA[มาศึกษาการใช้งาน SPI เป็น bus กันบ้าง จะช่วยใช้ Arduino ติดต่อกับ Digital Potentiometer, Analog to Digital และ เซ็นเซอร์ต่างๆได้ครับ
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">วีดีโอสอนการใช้ SPI Bus ,คราวนี้อุปกรณ์อิเล็กทรอนิกส์หลายๆตัวที่เป็น SPI เราจะได้ต่อใช้งานได้แล้ว สำหรับวีดีโอนี้เขาจะสอนใช้ Analog Devices 5204 Digital Potentiometer<br />
เอามาควบคุมความสว่างของ LED (ไม่รู้จะเบื่อ LED กันไปหรือยังนะครับ) สำหรับไอซี จะไม่มีในไทยครับ ดูเอาหลักการ กันไปก่อนแล้วกันครับ</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="390" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/1nO2SSExEnQ?fs=1&amp;hl=en_US&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="390" src="http://www.youtube.com/v/1nO2SSExEnQ?fs=1&amp;hl=en_US&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/03/sciguy14arduino-tutorial-8-spi-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Sciguy14] Arduino Tutorial 07: I2C Communication and Processing</title>
		<link>http://www.ayarafun.com/2011/02/sciguy14-arduino-tutorial-07-i2c-communication-and-processing/</link>
		<comments>http://www.ayarafun.com/2011/02/sciguy14-arduino-tutorial-07-i2c-communication-and-processing/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 06:25:27 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wire]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3084</guid>
		<description><![CDATA[ศึกษาการใช้งาน bus I2C เป็นอีก bus ที่ใช้งานในไมโครครับ และใช้งานบน arduino ไม่ได้ยากมาก แนะนำครับ ต้องลองอีกอัน
No related posts.]]></description>
			<content:encoded><![CDATA[<p>วีดีโออันต่อไปครับ สำหรับหัดใช้ arduino มาถึงตอนที่ 7 สำหรับตอนนี้จะเกี่ยวกับ I2C ครับ เป็นการสื่อสารระหว่างไอซี ความเร็วสูงกว่า serial ครับ สำหรับอุปกรณ์ในตัวอย่าง จะเป็นเซ็นเซอร์วัดอุณหภูมิ อาจจะหายากสักนิดนะครับ ก้อดูเป็นแนวทางแล้วกันครับ</p>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="349" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/GJX0BRUagCg?fs=1&amp;hl=en_US&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="349" src="http://www.youtube.com/v/GJX0BRUagCg?fs=1&amp;hl=en_US&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p style="text-align: left;">สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/02/sciguy14-arduino-tutorial-07-i2c-communication-and-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Sciguy14] Arduino Tutorial 6: Serial Comm and Processing</title>
		<link>http://www.ayarafun.com/2011/02/sciguy14-arduino-tutorial-6-serial-comm-and-processing/</link>
		<comments>http://www.ayarafun.com/2011/02/sciguy14-arduino-tutorial-6-serial-comm-and-processing/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 02:24:11 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[SCIGUY14]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3064</guid>
		<description><![CDATA[มาศึกษาการสื่อสารด้วย serial อีกสักรอบ โดยรอบนี้ มีการใช้โปรแกรม processing ควบคุมอุปกรณ์อีกด้วย
No related posts.]]></description>
			<content:encoded><![CDATA[<p>อีกสักรอบ รอบที่แล้วที่ผมลองในเวป อาจจะดูไม่เข้าถึง รอบนี้มาดู sciguy14 ใช้ arduino กับการสื่อสารด้วย serial อีกสักรอบ ในตอนนี้จะมีตัวอย่างการใช้งาน arduino สื่อสารกับคอมพิวเตอร์ โดยใช้ serial ครับ และจะมีตัวอย่างลองใช้โปรแกรม processing ควบคุมอุปกรณ์อีกด้วย ผมว่าถ้าไมโครไม่ต้องหรูมาก ต่อกับคอมได้ สามารถทำโปรแกรมสร้างสรรค์ได้เยอะแยะแล้ว</p>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="349" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/g0pSfyXOXj8?fs=1&amp;hl=en_US&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="349" src="http://www.youtube.com/v/g0pSfyXOXj8?fs=1&amp;hl=en_US&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/02/sciguy14-arduino-tutorial-6-serial-comm-and-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Sciguy14]Arduino Tutorial 5: Motors and Transistors</title>
		<link>http://www.ayarafun.com/2011/02/arduino-tutorial-5-motors-and-transistors/</link>
		<comments>http://www.ayarafun.com/2011/02/arduino-tutorial-5-motors-and-transistors/#comments</comments>
		<pubDate>Tue, 08 Feb 2011 17:42:35 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[SCIGUY14]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=3042</guid>
		<description><![CDATA[มาศึกษาการต่อวงจร ใช้งานมอเตอร์ DC ตัวเล็กๆ กับ ทรานซิสเตอร์ และ ต่อท้ายด้วย RC-Servo ด้วยครับ
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">รวดเร็วมากๆ ตานาย JeremyBlum มีบทเรียนเพิ่มอีกแล้ว สำหรับตอนนี้จะเป็นวิธีการ ใช้งานมอเตอร์ DC ตัวเล็กๆ กับ ทรานซิสเตอร์ และ ต่อท้ายด้วย RC-Servo ด้วยครับ สำหรับ tutorial ของนาย JeremyBlum ผมว่าเขาทำดีมาก ให้ดูวิธีการอธิบายวงจร กับวิธีการสอนการเขียนโปรแกรมครับ ผมว่าน่าจะเข้าใจได้ไม่ยากครับ</p>
<blockquote><p>First up, we’ll control a standard DC motor running off a 9V battery  with the help of an NPN transistor and a PWM (Pulse Width Modulated)  signal from the Arduino.  Next, it’s onto 180 degree servo motors, which  we will power on an independent 5V rail using a voltage regulator.   We’ll control the servo using a PWM signal from the arduino, and I’ll  show you how you can turn analog distance readings into precise angles  for the servo to turn to!</p></blockquote>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/5bHPKU4ybHY?fs=1&amp;hl=en_US&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/5bHPKU4ybHY?fs=1&amp;hl=en_US&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p style="text-align: left;">สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/02/arduino-tutorial-5-motors-and-transistors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arduino Tutorial 2 :  สื่อสารด้วย Serial</title>
		<link>http://www.ayarafun.com/2011/02/arduino-tutorial-2-how-to-use-serial/</link>
		<comments>http://www.ayarafun.com/2011/02/arduino-tutorial-2-how-to-use-serial/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 20:00:59 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[serial]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=2547</guid>
		<description><![CDATA[มาศึกษาการสื่อสารระหว่างคอมพิวเตอร์กับ arduino กันบ้าง เพราะว่าการข้อความ จะทำให้ไมโครส่งค่าภายในออกมาแสดงผลได้ เข้าใจง่าย
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">มารอบนี้ ขอเริ่มด้วยพูดเรื่องการสื่อสารระหว่างคอมพิวเตอร์กับ arduino กันบ้าง ที่ทางผมเอาเรื่องนี้มาพูดก่อน เพราะว่าการข้อความ หรือ Text message มันนำจะทำให้ไมโครส่งค่าภายในออกมาแสดงผลได้ เข้าใจง่าย และการสื่อสารจะเป็นการสื่อสารสองทาง จะส่งจาก arduino ไปยังคอมพิวเตอร์ หรือ จะส่งจากคอมพิวตอร์ เข้าไปสั่งงาน arduino ก้อทำได้ครับ</p>
<p style="text-align: justify;">สำหรับในไมโครคอนโทรลเลอร์ เรายังใช้ Serial ในการเชื่อมต่ออุปกรณ์อื่นอีกด้วย ไม่ว่าจะเป็นข้อมูลพิกัดอย่าง GPS ,อ่านบัตร RFID และอุปกรณ์ออกแบบเฉพาะงานอย่างเช่น Serial LCD character สำหรับแสดงผล และ Serial Servo Control สำหรับงานควบคุมมอเตอร์ servo จำนวนมากๆ</p>
<p style="text-align: justify;"><img class="aligncenter size-full wp-image-3026" title="serial" src="http://www.ayarafun.com/wp-content/uploads/2011/01/serial.jpg" alt="" width="400" height="313" /></p>
<p style="text-align: justify;">ดังนั้นในตอนที่สอง เราจึงมาให้เริ่มใช้งานพื้นฐานของการสื่อสารกันครับ  สำหรับการสื่อสารภายนอก arduino จะใช้พอร์ตที่เรียกว่า Serial หรือ  ภาษาไทยเรียกพอร์ตอนุกรม แต่ผมขอเรียกทับศัพท์แล้วกันนะครับ มันไม่คุ้น  พอร์ต Serial มันเป็นพอร์ตการสื่อสารที่เป็นมาตราฐาน  ที่ใช้กันมาตั้งแต่เมาส์ และ คียบอร์ด<span id="more-2547"></span></p>
<p style="text-align: justify;">การสื่อสารแบบอนุกรม หรือ Serial เป็นส่งข้อมูล โดยใช้เทคนิคการเลื่อนข้อมูล (Shift Bit) ส่งไปที่ล่ะบิต บนสายสัญญาณเส้นเดียว โดยการส่งข้อมูลแบบ Serial จะไม่มีการ sync สัญญาณนาฬิการะหว่างตัวรับและตัวส่ง แต่จะอาศัยวิธีตั้งค่าความเร็วในการรับส่งสัญญาณให้เท่ากั น หรือ เรียกว่าตั้งค่า<strong> baud rate</strong> และส่งสัญญาณ start และ stop เพื่อบอกว่า เป็นส่วนต้นของข้อมูล (start bit) หรือ ส่วนท้ายของข้อมูล (stop bit) ดังรูป</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-3030" title="image018" src="http://www.ayarafun.com/wp-content/uploads/2011/02/image018.gif" alt="" width="420" height="130" /></p>
<p>รูปแบบของ ข้อมูลจากที่ส่งผ่าน Serial จะมีการเพิ่ม Start bit และ Stop bit เข้าไปเพิ่มจากข้อมูลเดิม</p>
<ol>
<li>บิตเริ่มต้น      (Start bit) จะมีขนาด 1 บิต      จะเป็นลอจิก LOW</li>
<li>บิตข้อมูล      (Data bit) 8 บิต ข้อมูลที่จะส่ง</li>
<li>บิตภาวะคู่หรือคี่      (Parity bit) มีขนาด 1 บิต      ใช้ตรวจสอบข้อมูล ถ้าข้อมูลที่ได้รับไม่สมบูรณ์ นำเข้าค่ามา check กับ      Parity bit จะได้ค่าไม่ตรงกัน</li>
<li>บิตหยุด      (Stop bit) เป็นการระบุถึงขอบเขตของการสิ้นสุดข้อมูล จะเป็นลอจิก      HIGH</li>
</ol>
<p>เอาล่ะครับ ผมเล่าเรื่องทฤษฏี ไม่รู้หลับไปหรือยังที่กล่าวมา ก้อเพื่อให้เข้าใจหลักการเบื้องต้นครับ สำหรับการสื่อสารกับคอมพิวเตอร์ Arduino จะมีโมดุล hardware และมี library ภายใน ที่จัดการเรื่อง Serial อยู่แล้ว เราไม่ต้องสนใจสิ่งที่ทำในส่วนระดับล่าง หรือ ระดับ Hardware  แต่เรามาสนใจสิ่งที่ทำบนระดับสูงเลยว่า เราจะต้องส่ง รับค่าอะไร และต้องตั้งค่าอัตราเร็วในการส่งให้เท่ากัน หรือ ตั้งค่า<strong> baud rate </strong>เท่าไร แค่นี้เราก้อรับส่งข้อมูลกับคอมพิวเตอร์ได้แล้ว</p>
<h3><strong>รู้จักคำสั่งของ </strong><strong>Arudino ที่ใช้ใน serial</strong></h3>
<ol>
<li>void <strong>serial.begin(rate)</strong> กำหนดอัตราบอดของการรับส่งข้อมูล หน่วยเป็นบิตต่อวินาที่ (bits per second :bps baud rate)</li>
<li>int <strong>serial.available()</strong> ใช้ตรวจสอบว่า buffer รับข้อมูลไว้หรือไม่ โดยจะคืนค่าจำนวนไบต์ที่อยู่ในบัฟเฟอร์</li>
<li>int <strong>serial.read()</strong> ส่งค่าที่รับจากพอร์ตอนุกรมออกมา (Serial)</li>
<li>void <strong>Serial.flush()</strong> เคลียร์บัฟเฟอร์ของพอร์ตอนุกรม(Serial) ให้ว่าง</li>
<li>void <strong>Setial.print()</strong> พิมพ์ข้อมูล ออกทางพอร์ตอนุกรม</li>
<li>void <strong>Setial.println()</strong> พิมพ์ข้อมูล ออกทางพอร์ตอนุกรม แต่ขั้นบรรทัดใหม่ด้วย</li>
</ol>
<h2><strong>&lt;Lab-4&gt; คุยกับคอมพิวเตอร์</strong></h2>
<p>เรามาลองเอาง่ายๆกัน ครับ โดยตัวอย่างแรกให้ทดลองส่งข้อมูลดูก่อนครับ ไม่กี่บรรทัด ก็เสร็จแล้วพิมพ์ตามกันไปเลย ตามตัวอย่างข้างล่างนี้</p>
<pre class="brush: java">int counter = 0;

void setup() {
 Serial.begin(9600);
 Serial.println("Hello Serial");
 delay(100);
}

void loop() {
 counter++;

 Serial.println(counter);

 if (counter == 25)  {
 counter = 0;
 }

 delay(100);
}</pre>
<p style="text-align: justify;">หลังจาก Upload sketch นี้ ให้เปิด <strong>Serial Monitor</strong> นะครับ  แล้วเลือกจะเห็นข้อความขึ้นมาเป็น คำทักทาย “Hello Serial “ และ  ตามด้วยตัวเลข 1 ถึง 25 แต่ถ้ามันขึ้นมาเป็นภาษาแปลกๆ ก้อแสดงว่า  ความเร็วในการรับส่งข้อมูลของ arduino กับ computer อาจจะไม่ตรงกัน  ควรจะตั้งค่าควรตั้งค่า baud rate กันใหม่ครับ</p>
<p><strong>ถ้าใครรู้สึกว่า ตั้งค่า </strong><strong>BaudRate  แก้ไขที่นี้ครับ ในช่อง Serial Monitor ทางขวาจะมีช่องค่า Baudrate  เลือกค่าที่ถูกต้องในตามโปรแกรม ค่าความเร็วกับรับส่งจะเป็นตัวเลขลงตัว  600, 1200, 4800, 9600, 19200, 38400, 57600, 1115200 ปกติเราจะใช้ค่า 9600</strong></p>
<p><strong><br />
</strong></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-3022" title="example" src="http://www.ayarafun.com/wp-content/uploads/2011/01/example.jpg" alt="" width="442" height="225" /></p>
<p style="text-align: center;">&nbsp;</p>
<p style="text-align: justify;">สิ่งที่อยากให้ลองคือ ภายในฟังก์ชั่น Serial.println( value ,format)  สามารถจะส่งค่า ออกมาในรูปแบบอื่นได้ ไม่ว่าจะเป็น แบบตัวอักษร(BYTE), เลขฐานสอง(BIN), เลขฐานแปด(OCT), เลขฐานสิบ(DEC), เลขฐานสิบหก(HEX), ข้อความ ลองเปลี่ยน Serial.println(counter); เป็น Serial.println(counter, BYTE) และอื่นๆ ดูตามตัวอย่างข้างล่าง</p>
<blockquote><p>Serial.println (78, BYTE)                                  &#8220;N&#8221;</p>
<p>Serial.println (78, BIN)                                    &#8220;1001110&#8243;</p>
<p>Serial.println (78, OCT)                                   “116&#8243;</p>
<p>Serial.println (78, DEC)                                   &#8220;78&#8243;</p>
<p>Serial.println (78, HEX)                                   &#8220;4E&#8221;</p>
<p>Serial.println(1.23456, 0)                                 &#8220;1&#8243;</p>
<p>Serial.println(1.23456, 2)                                 &#8220;1.23&#8243;</p>
<p>Serial.println(1.23456, 4)                                 &#8220;1.2346&#8243;</p></blockquote>
<h2><strong>&lt;Lab-5&gt; ส่งกลับจากคอมพิวเตอร์ไป Arduino บ้าง</strong></h2>
<p style="text-align: justify;">หลังจากที่เราส่งออกไปได้แล้ว มาทดลองรับค่าจากคอมพิวเตอร์บ้าง วิธีการรับข้อมูลเข้าจากคอมพิวเตอร์ arduino จะใช้ฟังก์ชั่น Serial.Read() ซึ่งในตัวอย่างนี้ช่วยให้เข้าใจมากขึ้น หลังจาก upload อย่าลืม เปิด Serial Monitor แล้วทดลองตามล่ะ</p>
<pre class="brush: java">int incomingByte = 0;
void setup() {
    Serial.begin(9600);
    Serial.println("Hello ,type something");
}

void loop() {
     if (Serial.available() &gt; 0) {
         incomingByte = Serial.read();
         Serial.print ("&gt; ");
         Serial.println (incomingByte,BYTE);
     }
}</pre>
<p style="text-align: justify;">serial.read() จะเป็นฟังก์ชั่น ที่คืนค่ารับมาจาก serial ซึ่งในที่นี้เป็นตัวอักษร จะนำมาใช้งานเราจึงต้องประกาศตัวแปร มารับค่า และนำค่าไปประมวลผลต่อไป สำหรับในโปรแกรมนี้ เราได้รับค่า และพิมพ์ค่าออก Serial Monitor โดยเพิ่ม “&gt;” เข้าไปข้างหน้าด้วย</p>
<p style="text-align: center;"><a href="http://www.ayarafun.com/wp-content/uploads/2011/01/example2.jpg"><img class="aligncenter size-full wp-image-3023" title="example2" src="http://www.ayarafun.com/wp-content/uploads/2011/01/example2.jpg" alt="" width="387" height="280" /></a></p>
<h2><strong><strong>&lt;Lab-6&gt; ควบคุมไฟกระพริบด้วย Serial<br />
</strong></strong></h2>
<p>หลังจากที่เราควบคุมไฟกระพริบเบื้องต้นไปได้แล้ว ในตอนที่ 1 มาตอนนี้เราจะให้ลองทำ ไฟกระพริบควบคุมจากคอมพิวเตอร์ได้อย่างง่ายครับ โดยการส่งตัวอักษร &#8220;a&#8221; เพื่อทำให้ LED มันกระพริบ</p>
<p><strong> </strong></p>
<pre class="brush: java">int incomingByte = 0;
int ledPin = 13;

void setup() {
   Serial.begin(9600);
   Serial.println ("Type 'a' for blinky");
}

void loop() {
    if (Serial.available() &gt; 0)     {
          incomingByte = Serial.read();
          Serial.print ("&gt; ");
          Serial.println (incomingByte,BYTE);

          if (incomingByte == 'a') {
              Blinky();
          }
    }
} 

void Blinky(void) {
     digitalWrite(ledPin, HIGH);  // set the LED on
     delay(250);                  // wait for a second
     digitalWrite(ledPin, LOW);   // set the LED off
     delay(250);                  // wait for a second
}</pre>
<p style="text-align: justify;">วิธีการใช้งาน หลังจากเขียนโปรแกรมเสร็จ เปิด serial monitor แล้วใส่ค่าตัวอักษร ‘a’ ลงไป จะเห็นว่าไฟกระพริบทำงานได้ และพิมพ์ข้อความกลับมาว่า “test blinky” ดูภาพประกอบไปด้วย</p>
<p style="text-align: justify;">น่าจะเป็นตัวอย่างให้กับเพื่อนๆที่กำลังอยากทำอุปกรณ์ ควบคุมผ่านคอมได้นะครับ ถ้าเปลี่ยนจาก LED เป็น Relay สามารถเปิดปิดอุปกรณ์ที่ใหญ่กว่านี้ได้แล้ว สำหรับการใช้งานจริงเราอาจจะออกแบบการรับคำสั่งที่เป็นรูปแบบ protocol ซึ่งจะเอาไว้กล่าวต่อไปครับ</p>
<p><a href="http://www.ayarafun.com/wp-content/uploads/2011/01/example3.jpg"><img class="aligncenter size-full wp-image-3024" title="example3" src="http://www.ayarafun.com/wp-content/uploads/2011/01/example3.jpg" alt="" width="385" height="369" /></a></p>
<p style="text-align: justify;">จบไปอีก หนึ่งตอน จะเห็นว่าการเขียนโปรแกรมไม่ได้ยากเลยครับ ค่อยๆเพิ่มเติม และออกแบบวิธีคิดที่ดีครับ วิธีการฝึกเขียนโปรแกรมที่ดีที่สุด คือลงมือทำบ่อยๆครับ แล้วลองแก้ไข โปรแกรมตัวอย่าง ของเราให้ไปด้วย จะช่วยให้เป็นได้เร็วขึ้นมากครับ แล้วเจอกันใหม่ตอนหน้าครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/02/arduino-tutorial-2-how-to-use-serial/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>[Sciguy14] Arduino Tutorial 4: Analog Inputs</title>
		<link>http://www.ayarafun.com/2011/01/arduino-tutorial-4-analog-inputs/</link>
		<comments>http://www.ayarafun.com/2011/01/arduino-tutorial-4-analog-inputs/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 18:44:53 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[SCIGUY14]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=2989</guid>
		<description><![CDATA[ศึกษาเกี่ยวกับสัญญาณ analog มาทดลองต่อเกี่ยวกับ เขียนโปรแกรมรับค่า analog กันต่อบ้าง 
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">แล้วครับ Tutorial ไม่รู้จะเรียนรู้กันทันไหม สำหรับตอน 4 จะเกี่ยวโยงกับตอนที่  3 นะครับ หลังจากที่ได้ศึกษาเกี่ยวกับสัญญาณ analog แล้วสำหรับในตอนนี้ เรามาทดลองต่อเกี่ยวกับ เขียนโปรแกรมรับค่า analog กันต่อบ้าง ดูเพลินๆ สนุกๆ นะครับ</p>
<blockquote><p>This week is all about analog inputs for the arduino.  I’ll show you how you can use a voltage divider circuit (<a title="Arduino Tutorial 3: EE Basics" href="http://jeremyblum.com/2011/01/17/electrical-engineering-basics-in-arduino-tutorial-3/" target="_blank">see episode 3</a>)  and a variable resistor to make an analog sensor.  We’ll also use a  Sharp IR distance sensor as an analog input to detect distance and  movement (with some clever programming).  By the end of this episode,  you will be able to create your own emergency lighting system!  Without  further adieu, check out this week’s tutorial on analog inputs for the  arduino.</p></blockquote>
<p style="text-align: justify;">โดยรวมวีดีโอนี้ จะประกอบไปด้วย analogRead() กับวิธีต่อวงจรไฟฟ้าเพื่อสร้างเป็นเซ็นเซอร์ส่งค่ามาให้ arduino ได้</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/js4TK0U848I?fs=1&amp;hl=en_US&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/js4TK0U848I?fs=1&amp;hl=en_US&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/01/arduino-tutorial-4-analog-inputs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Sciguy14]  Arduino Tutorial 03: ความรู้พื้นฐานวิศวกรรมไฟฟ้า</title>
		<link>http://www.ayarafun.com/2011/01/electrical-engineering-basics-in-arduino-tutorial-3/</link>
		<comments>http://www.ayarafun.com/2011/01/electrical-engineering-basics-in-arduino-tutorial-3/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 15:11:08 +0000</pubDate>
		<dc:creator>chang</dc:creator>
				<category><![CDATA[Arduino-Tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[SCIGUY14]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ayarafun.com/?p=2734</guid>
		<description><![CDATA[สำหรับตอนสาม จะเกี่ยวกับพื้นฐานทางไฟฟ้าครับ ทั้งเรื่อง "กฏของ Ohm" , "Pull-down" , "Pull-up" และอีกเรื่อง "Voltage-divider" ซึ่งเป็นพื้นฐานที่ต้องรู้นะครับ สำหรับงานทางไมโครคอนโทรลเลอร์
No related posts.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">มาแล้วตอนที่ 3 ของ Jeremy Blum ขยันออกมาก ผมยังทำไม่ทันเลย รู้สึกว่าจะแพ้เรื่องความหนุ่ม วีดีโอดี บทความเด่น ทำได้ไงนิ สำหรับตอนสาม จะเกี่ยวกับพื้นฐานทางไฟฟ้าครับ ทั้งเรื่อง &#8220;กฏของ Ohm&#8221; , &#8220;Pull-down&#8221; , &#8220;Pull-up&#8221; และอีกเรื่อง &#8220;Voltage-divider&#8221; ซึ่งเป็นพื้นฐานที่ต้องรู้นะครับ สำหรับงานทางไมโครคอนโทรลเลอร์</p>
<p>สำหรับตอนนี้ไม่มี arduino นะครับ ทดลองพื้นฐานล้วนๆ ใครยังไม่ทราบเลยดูตามกันไปครับ</p>
<blockquote><p>In this week’s <a title="Arduino Tutorial Series Posts" href="http://jeremyblum.com/category/arduino-tutorials/" target="_blank">arduino tutorial</a>,  we take a bit of a detour and focus on some key elements of electrical  engineering design that we’ll be using in future episodes.  Included  amongst these topics is <a title="Ohm's Law on Wikipedia" href="http://en.wikipedia.org/wiki/Ohms_law" target="_blank">Ohm’s Law</a>, <a title="Current-Limiting Reistors in an LED Circuit on Wikipedia" href="http://en.wikipedia.org/wiki/LED_circuit" target="_blank">current-limiting resistors</a>, <a title="&quot;Pull&quot; Resistors on Wikipedia" href="http://en.wikipedia.org/wiki/Pull-up_resistor" target="_blank">pull-down and pull-up resistors</a>, <a title="Voltage Divider on Wikipedia" href="http://en.wikipedia.org/wiki/Voltage_divider" target="_blank">voltage dividers</a>, <a title="Potentiometers on Wikipedia" href="http://en.wikipedia.org/wiki/Potentiometer" target="_blank">potentiometers</a>, <a title="Analog-to-Digital Converters on Wikipedia" href="http://en.wikipedia.org/wiki/Analog-to-digital_converter" target="_blank">analog inputs</a>, and <a title="Linear Voltage Regulators on Wikipedia" href="http://en.wikipedia.org/wiki/Linear_regulator" target="_blank">voltage regulators</a>.</p>
<p>If you’re yearning to learn more after watching this video, I’d suggest you check out <a title="Jeri Ellsworth on YouTube" href="http://www.youtube.com/user/jeriellsworth" target="_blank">Jeri Ellsworth</a> and <a title="DAve Jones (eevBlog) on YouTube" href="http://www.youtube.com/user/eevblog" target="_blank">Dave Jones</a> on youtube. They offer videos about everything from circuit design to pinball machines.</p>
<p>You can download the files associated with this episode here:</p>
<ul>
<li><a href="http://jeremyblum.com/wp-content/uploads/2011/01/Episode-03-Arduino-Programs.zip">Episode 03 Arduino Programs</a></li>
<li><a href="http://jeremyblum.com/wp-content/uploads/2011/01/Episode-03-Parts-List.pdf">Episode 03 Parts List</a></li>
<li><a href="http://jeremyblum.com/wp-content/uploads/2011/01/Episode-03-Schematics.zip">Episode 03 Schematics</a></li>
</ul>
</blockquote>
<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/abWCy_aOSwY?fs=1&amp;hl=en_US&amp;rel=0" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/abWCy_aOSwY?fs=1&amp;hl=en_US&amp;rel=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>สำหรับใครที่ติดตาม วีดีโอ Arduino Tutorial จากทางเวปไอยาราฟันส์นะครับ ผมได้รวบรวมโดยกด TAG “SCIGUY14″ หรือเข้าไปดูที่ <a href="http://www.youtube.com/user/sciguy14">Youtube Channel</a> ของทางเขาได้ครับ</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ayarafun.com/2011/01/electrical-engineering-basics-in-arduino-tutorial-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

