Arduino Uno R3 + Android multiple LED blink can turn on but wont go off

androidarduinoledusb-host

I'm new here and I have a question…I hope you could help me, any answer is more than welcome…so I am using an android app with switches to turn on and off 5 LEDs…I managed to turn on four of them, but none of them won't to turn off when I press the switch again in my app… a think that something is wrong with my arduino sketch…p.s. in my app in send command I can send also command from seekbar but I am not using it I mean I didn't put it in app.

This is Arduino code. 
#include <adk.h>
USB Usb;
ADK adk(&Usb, "test", // Manufacturer Name
              "test", // Model Name
              "Example sketch for the USB Host Shield", 
              "1.0", // Version
               "http://www.tkjelectronics.dk/uploads/ArduinoBlinkLED.apk", 
               "0000000012345678"); // Serial Number (optional)
const int LED1 =  9;
const int LED2 =  4;
const int LED3 =  7;
const int LED4 =  8;
const int LED5 =  3;
uint32_t timer;
boolean connected;
void setup() {

   Serial.begin(115200);
   while (!Serial); 
   if (Usb.Init() == -1) {
      Serial.print("\r\nOSCOKIRQ failed to assert");
      while (1); // halt
   }
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
Serial.print("\r\nArduino Blink LED Started");
}

void loop() {
Usb.Task();

if (adk.isReady()) {
  if (!connected) {
    connected = true;
    Serial.print(F("\r\nConnected to accessory"));
}


uint8_t msg[3]={0x0};
uint16_t len = sizeof(msg);
uint8_t rcode = adk.RcvData(&len, msg);
if( rcode ) {
 USBTRACE2("Data rcv. :", rcode );}
if (len > 0) {    
   USBTRACE("\r\nData Packet.");
   // assumes only one command per packet  
   if (msg[1] == 0x2)
       digitalWrite(LED1, msg[0]==0x1 ? HIGH : LOW);
   else if (msg[1] == 0x3)
   digitalWrite(LED2, msg[0]==0x1 ? HIGH : LOW);
   else if (msg[1] == 0x4)
   digitalWrite(LED3, msg[0]==0x1 ? HIGH : LOW);
   else if (msg[1] == 0x5)
   digitalWrite(LED4, msg[0]==0x1 ? HIGH : LOW);
    else if (msg[1] == 0x6)
   digitalWrite(LED5, msg[0]==0x1 ? HIGH : LOW);
}

msg[0] = 0x1;   


if (millis() - timer >= 1000) { // Send data every 1s
  timer = millis();
  rcode = adk.SndData(sizeof(timer), (uint8_t*)&timer);
  if (rcode && rcode != hrNAK) {
    Serial.print(F("\r\nData send: "));
    Serial.print(rcode, HEX);
  } else if (rcode != hrNAK) {
    Serial.print(F("\r\nTimer: "));
    Serial.print(timer);
  }
}
} else {
    if (connected) {
    connected = false;
    Serial.print(F("\r\nDisconnected from accessory"));
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    }
 }
}

and my android app main activity

private final byte digitalPort = 0x1;
private final byte outputHighLow = 0x0;
private final byte outputPWM = 0x1;
private final byte low = 0x0;
private final byte high = 0x1;

private Switch switch1,switch2,switch3,switch4,switch5;

private CompoundButton.OnCheckedChangeListener highLowChangeListener = new HighLowChangeListener();

  ....... some code for usb accessory


protected void showControls() {
    setContentView(R.layout.main);

    switch1 = (Switch) findViewById(R.id.switch1);
    switch1.setTag((byte) 0x2);
    switch1.setOnCheckedChangeListener(highLowChangeListener);
    switch2 = (Switch) findViewById(R.id.switch2);
    switch2.setTag((byte) 0x3);
    switch2.setOnCheckedChangeListener(highLowChangeListener);
    switch3 = (Switch) findViewById(R.id.switch3);
    switch3.setTag((byte) 0x4);
    switch3.setOnCheckedChangeListener(highLowChangeListener);
    switch4 = (Switch) findViewById(R.id.switch4);
    switch4.setTag((byte) 0x5);
    switch4.setOnCheckedChangeListener(highLowChangeListener);
    switch5 = (Switch) findViewById(R.id.switch5);
    switch5.setTag((byte) 0x6);
    switch5.setOnCheckedChangeListener(highLowChangeListener);
}


private class HighLowChangeListener implements CompoundButton.OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        byte portByte = (Byte) buttonView.getTag();
        if (isChecked) {
            sendCommand(digitalPort, portByte, outputHighLow, high);
            Log.i(TAG, "message send: digital pin " + portByte + " HIGH");
        } else {
            sendCommand(digitalPort, portByte, outputHighLow, low);
            Log.i(TAG, "message send: digital pin " + portByte + " LOW");
        }
    }
}


public void sendCommand(byte portType, byte portNumber, byte mode, byte value) {
    byte[] buffer = new byte[4];
    if (value > 255)
        value = (byte)255;
    buffer[0] = portType;
    buffer[1] = portNumber;
    buffer[2] = mode;
    buffer[3] = value;
    Log.i(TAG, "byte: " + buffer[0]);
    Log.i(TAG, "byte: " + buffer[1]);
    Log.i(TAG, "byte: " + buffer[2]);
    Log.i(TAG, "byte: " + buffer[3]);
    if (mOutputStream != null && buffer[1] != -1) {
        try {
            mOutputStream.write(buffer);
            mOutputStream.flush();
        } catch (IOException e) {
            Log.e(TAG, "write failed", e);
        }
    }
}


}

Best Answer

I haven't trawled through all your code, but one immediate mistake I see is this:

uint8_t msg[3]={0x0};
...
digitalWrite(LED4, msg[3]==0x1 ? HIGH : LOW);

You allocate an array of 3 bytes (slices 0..2) and then reference slice 3, which is the 4th byte of a 3-byte array.