使用 BLE shield 读取时 RGB LED 不工作
RGB LED won't work when reading with BLE shield
我有一个 BLE shield + Arduino UNO,它已连接并与我的 IOS 设备一起工作。我还想做的就是用 RGB LED 显示屏蔽的连接状态。
我正在使用以下代码,但不知何故,即使在调用函数时我也看不到颜色变化。
int redPin = 13;
int greenPin = 12;
int bluePin = 11;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Init. and start BLE library.
ble_begin();
// Enable serial debug
Serial.begin(57600);
}
void loop()
{
Serial.println("Inside loop");
if ( ble_connected() )
{
setColor(200, 200, 200);
int sensorValue = analogRead(A0);
//Some code to write stuffs
}
ble_do_events();
delay(2000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
Serial.println("Inside setcolor");
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
当我在没有 BLE 的情况下单独执行时,LED 代码工作正常。下面的代码将起作用。同样的东西放在上面是行不通的。
int redPin = 13;
int greenPin = 12;
int bluePin = 11;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(20, 20, 20);
delay(2000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
这是什么原因?它与 AnalogRead 和 AnalogWrite 有什么关系吗?
TIA
正如 OP 所问,这里是答案。
问题是 Arduino UNO 在引脚 12 和 13 上没有 PWM,所以这些引脚上的 analogWrite
什么都不做。
根据 Arduino 文档 (here),"On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11"。因此,更改 R 和 G 引脚解决了问题。
我有一个 BLE shield + Arduino UNO,它已连接并与我的 IOS 设备一起工作。我还想做的就是用 RGB LED 显示屏蔽的连接状态。
我正在使用以下代码,但不知何故,即使在调用函数时我也看不到颜色变化。
int redPin = 13;
int greenPin = 12;
int bluePin = 11;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Init. and start BLE library.
ble_begin();
// Enable serial debug
Serial.begin(57600);
}
void loop()
{
Serial.println("Inside loop");
if ( ble_connected() )
{
setColor(200, 200, 200);
int sensorValue = analogRead(A0);
//Some code to write stuffs
}
ble_do_events();
delay(2000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
Serial.println("Inside setcolor");
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
当我在没有 BLE 的情况下单独执行时,LED 代码工作正常。下面的代码将起作用。同样的东西放在上面是行不通的。
int redPin = 13;
int greenPin = 12;
int bluePin = 11;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(20, 20, 20);
delay(2000);
}
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
这是什么原因?它与 AnalogRead 和 AnalogWrite 有什么关系吗? TIA
正如 OP 所问,这里是答案。
问题是 Arduino UNO 在引脚 12 和 13 上没有 PWM,所以这些引脚上的 analogWrite
什么都不做。
根据 Arduino 文档 (here),"On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11"。因此,更改 R 和 G 引脚解决了问题。