Digital Switch on LED with a single input via Bluetooth

Hello everyone! I have an Arduino UNO 3.

I am new to Arduino and I am trying to make a digital switch on a LED with a single input via Bluetooth. In other words, I have an LED (with a resistor) connected to pin 3. I want to turn on and off the led with a single press of a button, exactly the number "0". By that I mean: when the LED is off and i enter the number "0", the LED should turn on. When it is on and i enter the number "0", the LED should turn off.

Here is my code:

#define ledd 3

int ok = 0;

char state = 0;

void setup()
{
  pinMode(ledd, OUTPUT);
  Serial.begin(9600); // Default communication rate of the Bluetooth module
}

//main loop
void loop()
{

   if(Serial.available() > 0)
     { // Checks whether data is comming from the serial port
         state = Serial.read(); // Reads the data from the serial port

     }
     if(ledd == HIGH)
     {
        ok = 0;
     }
     else ok = 1;
     
     if (state == '0') 
     {
        if(ok == 1)
        {
          digitalWrite(ledd,HIGH);         
        }
        else
        {
          digitalWrite(ledd,LOW);
        }
     }
}

What can be the problem?

 if(ledd == HIGH)

ledd is a pin number (3), not the state of ledd, and it will never change. 3 can never be LOW (0).

Does this work for you?

const byte ledPin = 13;

void setup()
{
   Serial.begin(115200);  // make sure serial monitor baud rate matches
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   if(Serial.available())
   {
      char state = Serial.read();
      if (state == '0')
      {
         digitalWrite(ledPin, !digitalRead(ledPin)); // toggle led state
      }
   }
}

Firstly, thanks for replying.

It works… in a way.

I don’t know what’s the cause, but it’s working like this: I enter “0”, the led blinks for a fraction of a second, than it remains off. I enter “0” again, repeatedly, it blinks like that for the first 2, 3 attempts, then in the fourth, fiveth or more attempt, it remains on as it should. Then, when i enter “0” again, it shut down for a fraction of a second, then it keeps beeing on, and so on untill eventually it is off, and so on.

I don’t know what’s up… it seems very strange.
I made a little modification to the code just to give you an ideea of what is going on:

void loop()
{
  if(Serial.available())
  {
     char state = Serial.read();
     if (state == '0')
     {
        digitalWrite(ledPin, !digitalRead(ledPin)); // toggle led state
        Serial.println("State of LED: ");
        Serial.println(digitalRead(ledPin));
     }
  }
}

And the input / output from the Bluetooth terminal:

0 ← first input

0 ← first output (that blinks the led)
State of LED:
1
State of LED:
0

0 ← second input
State of LED: ← second ouput (the led is now on)
1

0 ← first input (to turn the led off)

0 ← first output (blinks)
State of LED:
0
State of LED:
1

0 ← second input

0 ← second output (blinks)
State of LED:
0
State of LED:
1

0 ← third input

0 ← third output (blinks)
State of LED:
0
State of LED:
1

0 ← fourth input

0 ← fourth output (blinks)
State of LED:
0
State of LED:
1

0 ← fiveth input

0 ← fiveth output (blinks)
State of LED:
0
State of LED:
1

0 ← sixth input
State of LED: ← finally, it turns it off
0

Also, with your Serial.begin(115200) the bluetooth doesn’t work, it works only with 9600. I don’t really know the difference, but this is how it is.

If you have any other ideas, please let me know.

Thanks in advance!

Change:

Serial.begin(115200);

To:

Serial.begin(9600);

Sorry, I forgot that Bluetooth works at 9600 baud. And did you change the const byte ledPin to 3.

EDITED: I changed the ledPin to 3 in the first place, please look at my edited post above to see the output from the bluetooth monitor.

This code I get this output:

State of LED:
1
State of LED:
0
State of LED:
1
State of LED:
0
State of LED:
1
State of LED:
0

I am testing by sending “0”'s at 9600 baud to an Uno from serial monitor, which is as close as I can do with my hardware, with this code (your additions included).

const byte ledPin = 13;

void setup()
{
   Serial.begin(9600);  // make sure serial monitor baud rate matches
   pinMode(ledPin, OUTPUT);
}
void loop()
{
  if(Serial.available())
  {
     char state = Serial.read();
     if (state == '0')
     {
        digitalWrite(ledPin, !digitalRead(ledPin)); // toggle led state
        Serial.println("State of LED: ");
        Serial.println(digitalRead(ledPin));
     }
  }
}

Are you sure that you are sending only one ‘0’ at a time?

Ooof, you are right!

I don't have an Android device, and my iPhone can't connect to this bluetooth HC-05, so i was trying to send the data from an Windows Application called Bluetooth Serial Terminal, and I think this app doesn't send the data correctly. I made a simple .apk for an android phone with a button that sends "0" and it's working correctly. I don't have access to an Android phone everytime, so do you have a suggestion of a Windows App that can communicate with bluetooth?

Thanks a lot!

Sorry, I've only used Bluetooth with my Android tablet.

andreitc: Ooof, you are right!

I don't have an Android device, and my iPhone can't connect to this bluetooth HC-05, so i was trying to send the data from an Windows Application called Bluetooth Serial Terminal, and I think this app doesn't send the data correctly. I made a simple .apk for an android phone with a button that sends "0" and it's working correctly. I don't have access to an Android phone everytime, so do you have a suggestion of a Windows App that can communicate with bluetooth?

Thanks a lot!

u can use HM-10 module as it has BLE 4 so u can connect with ur iphone

for an app u can use any serial terminal app and select the proper com port and its settings