AS A.H.指出,Pin 12不是PWM pin。 嘗試更改您的代碼以使用引腳9,10,11而不是10,11,12,並再次使用它。
這裏的文件樣本的修改版本來說明這個想法:
int rPin = 9;
int gPin = 10;
int bPin = 11;
int analogPin = 0; // potentiometer connected to analog pin 0
int val = 0; // variable to store the read value
void setup()
{
pinMode(rPin, OUTPUT); // sets the pin as output
pinMode(gPin, OUTPUT); // sets the pin as output
pinMode(bPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
analogWrite(rPin, val/4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
analogWrite(gPin, val/4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
analogWrite(bPin, val/4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
請注意,你需要掛接到模擬引腳0改變導致值的傳感器。
嗯...... 12腳是不是對我一個Arduino的PWM引腳。所以'analogWrite'將不會像該引腳所期望的那樣工作。 –