0

I am making an Android-controlled LED system over Bluetooth but I need to use 4 LEDs. I have done Android app but I am not able to figure out this Arduino code. Can some please help me get this done?

char command;
String string;
boolean ledon = false;
#define led 5

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
    string = "";
  }

  while(Serial.available() > 0) {
    command = ((byte)Serial.read());
    if(command == ':') {
      break;
    } else {
      string += command;
    }
    delay(1);
  }

  if(string == "TO") {
    ledOn();
    ledon = true;
  }

  if(string =="TF") {
    ledOff();
    ledon = false;
    Serial.println(string);
  }

  if ((string.toInt()>=0)&&(string.toInt()<=255)) {
    if (ledon==true) {
      analogWrite(led, string.toInt());
      Serial.println(string);
      delay(10);
    }
  }
}

void ledOn() {
  analogWrite(led, 255);
  delay(10);
}

void ledOff() {
  analogWrite(led, 0);
  delay(10);
}
  • what is the data format sent for android? what is the results expected? – dhimaspw Dec 7 '16 at 4:30
  • you only define 1 led but want to control 4? – dhimaspw Dec 7 '16 at 4:50
  • android application is sends code like TO & TF for on&off respectively the result i want is to switch 4 leds on and off by those commands which are sent by android app over bluetooth – Kartik Rana Dec 7 '16 at 4:52
  • yup i actualy got this code from a website em a total newbie to this so i have no idea – Kartik Rana Dec 7 '16 at 4:53
1

Its better if you use SoftwareSerial for Bluetooth, so the native serial port can be used to debugging. But in case you doesn't need them:

uint8_t led[4] = {5,6,7,8}; // pin for LED 1,2,3,4
char command1,command2;
String string;
  void setup()
  {
    Serial.begin(9600);
    for (int i=0; i<4; i++)
        pinMode(led[i], OUTPUT); //set all LED pin as OUTPUT
  }

  void loop()
  {
    if (Serial.available() > 1) //run if 1 bit or more data received
    {
    string = "";
        command1 = Serial.read();
        command2 = Serial.read();
        switchled(command1-'0',command2-'0');
    string = command1+command2;
    }
    delay(1);
  }
void switchled(char pinLED,char statusLED)
{
    if (statusLED == 0 || statusLED == 1)
    digitalWrite(led[pinLED-1],statusLED);
    else
    digitalWrite(led[pinLED-1],!digitalRead(led[pinLED-1]));

    Serial.print("LED "); 
    Serial.print(pinLED); 
    Serial.print(" = "); 
    Serial.print(digitalRead(pinLED)); 
}

The code reads 2 byte of data, so if your phone sent "11" the 1st led will turned on, and "10" will turn the 1st led off. The first digit is the LED index (1/2/3/4) and the second digit is the LED state (0/1), other than that, the LED on selected index will be toggled;

  • the following error comes up when trying to compile ---string was not declared in this scope --- if (Serial.available() > 1) //run if 1 bit or more data received { string = ""; // ---string was not declared in this scope --- command1 = Serial.read(); – Kartik Rana Dec 7 '16 at 10:40
  • still unable to compile shows ";" expected before string but adding that gived another error please verify the code – Kartik Rana Dec 7 '16 at 16:15
  • updated answers – dhimaspw Dec 8 '16 at 1:15
  • still compilation error please check it on the software then upload – Kartik Rana Dec 8 '16 at 13:10
  • There are a million reason of compilation error. I compiles successfully. Please keep in mind that I have no obligation to do this "app" of yours, I am just trying to help. Do some effort, this is not "please-make-me-a-code.com". I'm out of here. – dhimaspw Dec 9 '16 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.