Last active
April 3, 2017 15:57
-
-
Save khatthaphone/b572f4bf643133c1e54de16cd9989ae0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ປະກາດຕົວປ່ຽນທີ່ຄົງຄ່າເປັນແບບ define ເພາະຈະເກັບຄ່າຄົງຄ່າບໍ່ສາມາດປ່ຽນແປງໄດ້ (constant) | |
// ແລະ ປະຢັດຄວາມຈຳ ເພາະໃຊ້ຄວາມຈຳຕາມຈຳນວນທີ່ໃຊ້ແທ້ | |
// (int ຈະຈອງຄວາມຈຳໄວ້ 4 bytes ບໍ່ວ່າເຮົາຈະເກັບຄ່າໜ້ອຍດຽວກໍຕາມແຕ່) | |
#define GAS_SENSOR A0 // ຂາທີ່ເຊື່ອມເຊັນເຊີແກສເອົາ input | |
#define LED 13 // ຂາທີ່ເຊື່ອມ LED ລາຍງານສະຖານະ | |
#define DANGER_GAS_LEVEL 250 // ຄ່າທີ່ຈະປຽບທຽບເມື່ອລະດັບແກສເທົ່າກັບ ຫຼື ຫຼາຍກວ່າຄ່ານີ້ ໃຫ້ເຮັດໄປກຳນົດ LED ລາຍງານສະຖານະ | |
int gas_level; // ຕົວປ່ຽນສຳລັບເກັບລະດັບແກສ | |
void setup() { | |
pinMode(GAS_SENSOR, INPUT); // ປະກາດໃຫ້ຮັບຄ່າແກສຈາກຂາ GAS_SENSOR | |
pinMode(LED, OUTPUT); // ປະກາດໃຫ້ສົ່ງຄ່າອອກຂາ LED | |
Serial.begin(9600); // ປະກາດໃຫ້ເປີດໃຊ້ Serial ເພື່ອສະແດງຄ່າອອກຄອມຜ່ານຄວາມຖີ່ 9600 (buad) | |
} | |
// ຄຳສັ່ງໃນ loop ຈະແລ່ນຊ້ຳຄືນຕະຫຼອດ | |
void loop() { | |
gas_level = analogRead(GAS_SENSOR); // ອ່ານຄ່າຈາກຂາ GAS_SENSOR ເປັນຄ່າ analog | |
Serial.print("Gas Level: "); // | |
Serial.print(gas_level); // ສະແດງຜົນລະດັບຂອງແກສອອກ Serial | |
Serial.print("\n"); // | |
digitalWrite(LED, (gas_level >= DANGER_GAS_LEVEL ? HIGH:LOW)); | |
// ຂຽນຄ່າໃຫ້ LED ໂດຍມີເງື່ອນໄຂ ຖ້າ gas level ໃຫຍ່ກວ່າ ຫຼື ເທົ່າກັບ DANGER_GAS_LEVEL | |
// ຖ້າ 'ຖືກ' ໃຫ້ຂຽນເປັນຄ່າ HIGH (ເປີດ) | |
// ກົງກັນຂ້າມ ຖ້າ 'ຜິດ' ໃຫ້ຂຽນເປັນຄ່າ LOW (ປິດ) | |
delay(1000); // ຢຸດການເຮັດວຽກໄວ້ 1000 ມິລິວິນາທີ = 1 ວິນາທີ, ກ່ອນຈເລີ່ມຕົ້ນ loop ໃໝ່ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment