简体   繁体   English

Arduino“RGB”灯不闪烁

[英]Arduino "RGB" Light not Blinking

I just want to know that for RGB light to work on Arduino, is there any special code other than this我只想知道RGB灯在Arduino上工作,除此之外还有什么特殊代码吗

int rPin = 13;
int gPin = 12;
int bPin = 11;
String msg1 = "Enter the color : ";
String inpt;
void setup() {
  pinMode(rPin,OUTPUT);
  pinMode(gPin,OUTPUT);
  pinMode(bPin,HIGH);
  Serial.begin(9600);
}
void loop() {
  digitalWrite(rPin,HIGH);
  digitalWrite(gPin,HIGH);
  digitalWrite(bPin,HIGH);
}

RGB is not doing anything right now.. RGB现在没有做任何事情..

i guess there are some things missing out.我想有一些东西遗漏了。 First, you don't change the states of the leds to OFF in the loop.首先,您不要在循环中将 LED 的状态更改为 OFF。 And second there's no delays.其次,没有延迟。 Even if you chnage the states, you must have some delays to see the blinking.即使你改变了状态,你也必须有一些延迟才能看到闪烁。 here's a code I suggest, try it and see if the result is what you want:这是我建议的代码,尝试一下,看看结果是否是你想要的:

int rPin = 13;
int gPin = 12;
int bPin = 11;
int period = 500; //blinking period in ms
//String msg1 = "Enter the color : "; not used in this example
//String inpt;
void setup() {
  pinMode(rPin,OUTPUT);
  pinMode(gPin,OUTPUT);
  pinMode(bPin,HIGH);
  Serial.begin(9600);
}
void loop() {
  digitalWrite(rPin,HIGH);
  delay(period);
  digitalWrite(rPin,LOW);

  digitalWrite(gPin,HIGH);
  delay(period);
  digitalWrite(gPin,LOW);

  digitalWrite(bPin,HIGH);
  delay(period);
  digitalWrite(bPin,LOW);

}

and of course you might put different periods for each led, depending on your application.当然,根据您的应用程序,您可能会为每个 LED 设置不同的时间段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM