0

I wrote a simple program for controlling an LED using mobile. So, I bought the HC-05 bluetooth module configured it and then connected it to the arduino.

This is the code that I wrote for it.

int ledPin1=13;
int state;
int flag=0;

void setup()
{
    pinMode(ledPin1, OUTPUT);
    digitalWrite(0,HIGH);

    second:
    Serial.begin(9600); 
}

void loop()
{
    if(Serial.available() >0)
    {
        state = Serial.read();
        flag=0;
    }  
    if (state == '0') 
    {
        digitalWrite(ledPin1, LOW);
        if(flag == 0){ 
            Serial.println("led off");
            flag=1;
        }
    }   
    if (state == '1')
    {         
        digitalWrite(ledPin1, HIGH);
        if(flag == 0){ 
            Serial.println("led on");
            flag=1;
        }
    }
}

Everything is working cool with the serial monitor. But, I'm not able to control it using my mobile. I gave an input 1 in serial monitor and if showed "led on". the same thing appears on the commanding interface of the app used in the mobile. So, the bluetooth is transmitting information from the arduino to mobile but I'm not able to send commands from the mobile and control the led. I tried using various apps like Blueterm, Ardudroid etc but, the situation remains the same.

What am I doing wrong? Is there something wrong with the code?

I'm using Arduino Uno.

2

Your android app might be sending extra characters like "end of line", which will cause the return value of Serial.read() to be other than 1 or 0 after the first byte. Nevertheless, it looks like it should still turn on or off the LED as soon as it reads the first byte, and do nothing when it reads the following bytes. I have used the "hyperterminal rs232" app successfully to send single characters, maybe give that I try to see if it helps and ensure it is configured at the correct baud rate.

I would recommend to send back to the serial stream the characters read, to ensure they have been received correctly, so I would change this:

if(Serial.available() >0)
{
    state = Serial.read();
    flag=0;
} 

to:

if(Serial.available() >0)
{
    state = Serial.read();
    Serial.print(state);
    flag=0;
}

Edit:

Just realised this in setup():

digitalWrite(0,HIGH);

You are setting the receive pin of the UART peripheral to hight, so that is probably why you are not reading data from your phone.

0

Why don't you try making another code. If you want to use library, use #include <SoftwareSerial.h>

Do not use digitalWrite(0, HIGH) in void setup(){...}

Declare ledPin = 13;

Use only pinMode and Serial.begin(9600) in void setup()

In loop() do ask if (Serial.available()){...}

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.