Topic 2 - Giải pháp Điều khiển và đo độ sáng đèn LED qua Virtual PIN
Phát triển Giải pháp Điều khiển Đèn & Đo Độ sáng đèn LED trên Nền tảng E-Ra
E-ra điều khiển độ sáng Led
E-ra đọc giá trị ánh sáng
Source code
int led =2;int freq =5000;int ledChannel =0;int resolution =8;ERaTimer timer;/* This function print uptime every second */voidtimerEvent() {ERA_LOG("Timer","Uptime: %d",ERaMillis() /1000L);int value =analogRead(34);ERa.virtualWrite(V1,4095- value); // Reverse}ERA_WRITE(V0) { /* Get value from Virtual Pin 0 and write Pin 2. */uint8_t value =param.getInt();#if (ESP_IDF_VERSION_MAJOR>4)ledcWrite(led, value *255/100); // Range 0 - 255#elseledcWrite(ledChannel, value *255/100); // Range 0 - 255#endif}voidsetup() { /* Setup debug console */Serial.begin(115200);ERa.begin(ssid, pass); /* Setup timer called function every second */timer.setInterval(1000L, timerEvent);#if (ESP_IDF_VERSION_MAJOR>4)ledcAttach(led, freq, resolution);#elseledcSetup(ledChannel, freq, resolution);ledcAttachPin(led, ledChannel);#endif}voidloop() {ERa.run();timer.run(); }