Điều khiển đèn Led built_in của ESP8266 thông qua dòng lệnh nhập từ của sổ Serial Monitor
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(16, OUTPUT);
}
char endChar = '\n';
char chrTemp;
String strTemp="";
bool endChar_ok = false;
String strLenhDaNhan ="";
bool ledState = 0;
unsigned long currentTime, previousTime=0;
void loop() {
while(Serial.available()>0 && endChar_ok == false){
chrTemp = Serial.read();
if(chrTemp == endChar){
endChar_ok = true;
} else{
strTemp = strTemp+String(chrTemp);
endChar_ok = false;
}
}
if(endChar_ok == true){
strLenhDaNhan = strTemp;
Serial.println("#"+strTemp+"@");
strTemp="";
endChar_ok = false;
}
if(strLenhDaNhan == "Mở đèn led 2") digitalWrite(2, LOW);
if(strLenhDaNhan == "Tắt đèn led 2") digitalWrite(2, HIGH);
if(strLenhDaNhan == "Mở đèn led 16") digitalWrite(16, LOW);
if(strLenhDaNhan == "Tắt đèn led 16") digitalWrite(16, HIGH);
if(strLenhDaNhan == "Mở cả hai đèn led"){
digitalWrite(2, LOW);
digitalWrite(16, LOW);
}
if(strLenhDaNhan == "Tắt cả hai đèn led"){
digitalWrite(2, HIGH);
digitalWrite(16, HIGH);
}
if(strLenhDaNhan == "Nháy đa hài"){
currentTime = millis();
digitalWrite(2, ! ledState);
digitalWrite(16, ledState);
if((currentTime - previousTime) > 500){
ledState = ! ledState;
previousTime = currentTime;
}
}
}
Không có nhận xét nào:
Đăng nhận xét