I have a BLE shield + Arduino UNO which is connected and working with my IOS device. All i want to do additionally was to show the connected status of the shield with a RGB LED.
I am using the following code, but somehow even when the functions are getting called i don't see the color changes.
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);
}
The LED code works fine when i execute it separately without BLE. Below code will work. Same thing put above won't work.
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);
}
What is the reason? Does it has to do anything with AnalogRead and AnalogWrite? TIA
delay(2000)
right? Isn't it too much? For this kind of delays I'd mimic the "blink without delay example" approach (usingmillis
)... And doesble_connected
return true? You can test this with someSerial.println
and check where it hangs – frarugi87