ใบงานที่ 3.5 การเขียนโปรแกรมใช้งานบอร์ด Arduino เพื่อการรับตัวแปรแบบอาเรย์ (Array) และมีการทำงานแบบวนซ้ำ (for loops)
ใบงานที่ 3.5 การเขียนโปรแกรมใช้งานบอร์ด Arduino เพื่อการรับตัวแปรแบบอาเรย์ (Array) และมีการทำงานแบบวนซ้ำ (for loops)
จุดประสงค์
เพื่อให้ผู้เรียนได้ศึกษาเกี่ยวกับการเขียนโปรแกรมการใช้งานบอร์ด Arduino สำหรับการรับตัวแปรแบบอาเรย์ (Array) และมีการทำงานแบบวนซ้ำ (for loops)
for (i = 0; i < 5; i++)
วิธีการทดลอง
1. ทำการศึกษาสัญลักษณ์ในวงจรอิเล็กทรอนิกส์ตามแผนผังการต่อวงจรเพื่อใช้ตัวแปร แบบอาเรย์ (Array)และมีการทำงานวนซ้ำ (for loop) ดังรูป
3. ทำการเขียนโปรแกรมภาษาซีตามโปรแกรม แล้วอัพโหลดเข้าสู่บอร์ดอาดุยโน่ สังเกตการณ์เปลี่ยนแปลงที่เกิดขึ้น
//โปรแกรมArray & for loop
// Array & for loop
int timer = 1000; /* The higher the number,the slower the timing.*/
// an array of pin numbers to which LEDs are attached
int ledPins[] = {9, 10, 11};
// the number of pins (i.e. the length of the array)
int pinCount = 3;
void setup() { // the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() { // loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--)
{
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
งานวันนี้
1. แสดงผลหมายเลขพอร์ตดิจิทัลของบอร์ด Arduino ที่ทำให้หลอดไดโอดเปล่งแสงสว่าง ผ่านทาง Serial Monitor
2. ต่อวงจรเพื่มให้มี LED 6 ดวง