วันพุธที่ 14 มีนาคม พ.ศ. 2561

ส่งงานครั้งที่ 4

Arduino - LED Bar Graph
const int analogPin = A0;
const int ledCount = 8;
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
void setup() {
   for (int thisLed = 0; thisLed < ledCount; thisLed++) { //กำหนด thisLed=0 ถ้า thisLed น้อยกว่า ledCount ให้เพิ่ม thisLed ทีละ 1
      pinMode(ledPins[thisLed], OUTPUT);
   }
}
void loop() {
   int sensorReading = analogRead(analogPin);
   int ledLevel = map(sensorReading, 0, 1023, 0, ledCount); //กำหนดค่า sensorReading จาก 0,1023 เป็น 0,ledCount
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      if (thisLed < ledLevel) {
         digitalWrite(ledPins[thisLed], HIGH);
      }else {
         digitalWrite(ledPins[thisLed], LOW);
      }
   }
Bar Breadboard

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

Mini Project

 Control Lamp 220v  With LDR const int lamp = 12; const int ldrPin=A0; #include <Wire.h> #include <LiquidCrystal_I2C.h&g...