Digital Input
- ทำอะไร
- เมื่อกด IN1 ค้างให้ LED ติด 1 ดวงวิ่งจาก OUT7 ไป OUT0
- เมื่อกด IN2 ค้างให้ LED ติด 1 ดวงวิ่งจาก OUT0 ไป OUT7
- ทำ ทำไม
- เพื่อสังเกตุการรับค่า Digital จากอุปกรณ์สวิทช์
- อุปกรณ์การใช้งาน
Arduino Duemilanove Extended |
ET-TEST 10PIN/INP (สวิทช์) |
ET-TEST 10PIN/OUT (LED) |
Bread Board |
- ขั้นตอนการทำ
- เขียนโปรแกรม Arduino โดยเมื่อกดสวิทช์จะได้รับค่า 1 ให้กระแสไฟสามารถผ่านไปได้ เพื่อให้ LED สว่าง และรับค่า 0 เมื่อไม่ได้กดสวิทช์
- ต่อ Cable M-F ที่ Arduino ขา 2, 3, 4 กับสวิทช์ ให้เป็น Input
- ต่อ Cable M-F ที่ Arduino ขา 6-13 กับ LED
- จ่ายไฟ 5V จาก Arduino ให้กับสวิทช์ กับ LED
- ต่อ GROUND ให้กับสวิทช์
- สังเกตการรับค่าเมื่อกดสวิทช์
การรับค่า 1 และ 0 เมื่อกดสวิทช์ |
- ผลการทำงาน
- แบบทดสอบ Digital Input
- เมื่อกด IN1 ให้แสดง LED จากตรงกลางแล้ววิ่งออกจากกัน
- เมื่อกด IN2 ให้แสดง LED 8 ตัวแล้วดับจากตรงกลางออกจากกัน
- Source Code
void setup() {
Serial.begin(9600);
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
}
void loop() {
int count = digitalRead(2); Serial.begin(count);
int count1 = digitalRead(3); Serial.begin(count1);
int count2 = digitalRead(4); Serial.begin(count2);
if(count == LOW) {
digitalWrite(13, HIGH); digitalWrite(12, HIGH); digitalWrite(11, HIGH); digitalWrite(10, HIGH);
digitalWrite(9, HIGH); digitalWrite(8, HIGH); digitalWrite(7, HIGH); digitalWrite(6, HIGH);
delay(100);
digitalWrite(9, HIGH); digitalWrite(8, HIGH); digitalWrite(7, HIGH); digitalWrite(6, HIGH);
delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW); digitalWrite(10, LOW);
digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, LOW); digitalWrite(6, LOW);
digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, LOW); digitalWrite(6, LOW);
delay(100);
}
if(count1 == LOW) {
digitalWrite(13, HIGH); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, HIGH); digitalWrite(11, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, HIGH); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, HIGH); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, HIGH); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, HIGH);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, HIGH); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, HIGH); delay(100);
}
if(count2 == LOW) {
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, HIGH); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, HIGH); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, HIGH);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, HIGH); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, HIGH); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH);
digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, HIGH);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, LOW); digitalWrite(12, HIGH); digitalWrite(11, LOW);
digitalWrite(13, LOW); digitalWrite(12, HIGH); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
digitalWrite(13, HIGH); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(13, HIGH); digitalWrite(12, LOW); digitalWrite(11, LOW);
digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW);
digitalWrite(7, LOW); digitalWrite(6, LOW); delay(100);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น