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);
}
dda
1,5951 gold badge12 silver badges17 bronze badges
asked Dec 7, 2016 at 4:01
4
  • what is the data format sent for android? what is the results expected? Commented Dec 7, 2016 at 4:30
  • you only define 1 led but want to control 4? Commented Dec 7, 2016 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 Commented Dec 7, 2016 at 4:52
  • yup i actualy got this code from a website em a total newbie to this so i have no idea Commented Dec 7, 2016 at 4:53

1 Answer 1

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;

answered Dec 7, 2016 at 6:45
5
  • 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(); Commented Dec 7, 2016 at 10:40
  • still unable to compile shows ";" expected before string but adding that gived another error please verify the code Commented Dec 7, 2016 at 16:15
  • updated answers Commented Dec 8, 2016 at 1:15
  • still compilation error please check it on the software then upload Commented Dec 8, 2016 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. Commented Dec 9, 2016 at 1:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.