0
votes

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

1
Is the delay(2000) right? Isn't it too much? For this kind of delays I'd mimic the "blink without delay example" approach (using millis)... And does ble_connected return true? You can test this with some Serial.println and check where it hangsfrarugi87
The above code executes, even the function of setColor gets called. The analog inputs shows the data aswell. Everything works except the lights won't glow. The light would glow on the 2nd function. Makes me think in arduino can i do a analogRead and write together or is there a limitation?KD.
Ok, I tested it in a simulator and there is at least one problem: pins 12 and 13 are NOT PWM capable. As stated at this page "On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11". So try changing the pins and then see what happens. And.. No, analogRead has nothing to do with analogWrite, so they can't interferefrarugi87
Hi @frarugi87 thanks. That worked. Would you mind posting your reply as an answer so that I can mark this solved. Thanks again for sparing your time for helping me.KD.
Glad it worked :) I added the answer belowfrarugi87

1 Answers

0
votes

As the OP asked, here is the answer.

The problem was that the Arduino UNO doesn't have PWM on pins 12 and 13, so analogWrite on these pins does nothing.

According to Arduino documentation (here), "On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11". Consequently changing the R and G pins fixed the problem.