วันจันทร์ที่ 26 ธันวาคม พ.ศ. 2559

ใบงานที่ 3 Switch Case with LDR

จัดทำโดย
นายนนทกร   คำลาย         เลขที่  9   กลุ่ม D5

วัสดุอุปกรณ์


1.บอร์ดทดลอง Arduino UNO R3     1   บอร์ด
2.สาย USB                                      1   เส้น
3.ตัวต้านทาน 220โอหม์                    8   ตัว
4.ตัวต้านทาน 1kโอหม์                      1 ตัว
5.สายไฟ                                         12   เส้น
6.หลอด LED                                      8 หลอด
7.LDR                                                1 ตัว
8.โฟโต้บอร์ด                                       1 แผง
  
รูปวงจร










code


// these constants won't change. They are the
// lowest and highest readings you get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment

const int sensorMax = 600;    // sensor maximum, discovered through experiment
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
}
void loop() {
// read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  // do something different depending on the
  // range value:
switch (range) {
    case 0:    // your hand is on the sensor
      Serial.println("มืด");
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delay(100);
      break;
     case 1:    // your hand is close to the sensor
      Serial.println("สลัว");
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
      delay(100);
      break;
     case 2:    // your hand is a few inches from the sensor
      Serial.println("สว่าง");
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      break;
     case 3:    // your hand is nowhere near the sensor
      Serial.println("สว่างมาก");
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delay(100);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      delay(100);
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      delay(100);
      break;
  }
  delay(1);        // delay in between reads for stability
}





วันพฤหัสบดีที่ 22 ธันวาคม พ.ศ. 2559

ใบงานที่ 2 เรื่อง Digital Output (LED 8 Digit)

จัดทำโดย

นายนนทกร คำลาย       เลขที่ 9    D5

วัสดุอุปกรณ์


1.บอร์ดทดลอง Arduino UNO R3    1   บอร์ด
2.สาย USB                                    1   เส้น
3.ตัวต้านทาน 220โอหม์                  8   ตัว
4.สายไฟ                                        9   เส้น
5. หลอด LED                                  8  หลอด
6.โฟโต้บอร์ด                                  1   แผง

รูปวงจร




code



void setup() {

  
pinMode(6,OUTPUT);
  
pinMode(7,OUTPUT);
  
pinMode(8,OUTPUT);
  
pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  
pinMode(11,OUTPUT);
  
pinMode(12,OUTPUT);
  
pinMode(13,OUTPUT);
}


void loop() {

  
digitalWrite(6, HIGH);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10,LOW);
  
digitalWrite(11,LOW);
  
digitalWrite(12,LOW);
  
digitalWrite(13,LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, HIGH);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, HIGH);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, HIGH);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, LOW);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  
digitalWrite(11, HIGH);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, LOW);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, HIGH);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, HIGH);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  
digitalWrite(11, LOW);
  
digitalWrite(12, LOW);
  
digitalWrite(13, LOW);
  
delay(300);

  digitalWrite(6, HIGH);
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10,LOW);
  
digitalWrite(11,LOW);
  
digitalWrite(12,LOW);
  
digitalWrite(13,LOW);
  
delay(300);

  digitalWrite(6, LOW);                      
  
digitalWrite(7, LOW);
  
digitalWrite(8, LOW);
  
digitalWrite(9, LOW);
  digitalWrite(10,LOW);
  
digitalWrite(11,LOW);
  
digitalWrite(12,LOW);
  
digitalWrite(13,LOW);
  
delay(300);

}