NodeMCU MQTT LED亮度控制

时间:2019-07-05 11:06:18

标签: lua nodemcu

嗨,我想通过mqtt控制led,但是我找不到将led设置为发送给nodemcu的值的方法。这就是我现在尝试执行的操作,但是它只能执行0(然后关闭)或更高(然后打开)的值,并且不会变暗。

if(topic=="ledstrip/red") {
  Serial.print("Changing RED Lights to ");
  if(messageTemp == "on"){
    for (int r = 0; r < 1023; r++) { 
      analogWrite(ledstrip_red, r);
      delay(1);
    }
    digitalWrite(ledstrip_red, HIGH);
    Serial.print("On");
  } else if(messageTemp == "off"){
    digitalWrite(ledstrip_red, HIGH);
    for (int r = 1023; r > 0; r--) { 
      analogWrite(ledstrip_red, r);
      delay(1);
    } 
    digitalWrite(ledstrip_red, LOW);
    Serial.print("Off");
  } else {
    Serial.print("Changing RED Brightness to ");
    digitalWrite(ledstrip_red, messageTemp.toInt());
    Serial.print(messageTemp);       
}

1 个答案:

答案 0 :(得分:0)

digitalWrite()只能将值设置为0或1