Question

I've discovered some pretty interesting things on the connection of my Mega ADK to the Galaxy Nexus 4.0.4.

If I have a loop like this, it works just fine

void loop() {

    if(acc.isConnected()){   
        Serial.println("Accessory Connected");
        delay(1000);
    }
    else
        Serial.println("Accessory Not Connected");

}

But if I make a small change to it like that, it doesn't connect to the phone.

void loop() {

    if(acc.isConnected())
        Serial.println("Accessory Connected");
    else
        Serial.println("Accessory Not Connected");

    delay(1000);

}

My setup looks like this:

void setup() {

    Serial.begin(115200);

    pinMode(led, OUTPUT);

    pinMode(HabilitaMotores, OUTPUT);
    pinMode(Motor0FW, OUTPUT);
    pinMode(Motor0RW, OUTPUT);
    pinMode(Motor1FW, OUTPUT);
    pinMode(Motor1RW, OUTPUT);

    acc.powerOn();
}

Have you ever seen this before? Am I making a terrible mistake that I can't see?

Was it helpful?

Solution

When the ADK board, tries to connect to Android, it will constantly poll. The Android waits only for a few seconds and after which it doesn't allow the connection to be made.

In the first version of the code, the delay() call happens only after it is connected. So till the board is connected, the board will keep on polling and will not miss the Android time window.

But in the second version, the delay() is called every time the loop() function is executed. So the poll happens only ones every second.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top