#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2, 3); // RX | TX

const int ledPin = 13; // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT); // sets the digital pin as output
  BTSerial.begin(9600); // sets the Bluetooth module's baud rate
  Serial.begin(9600); // sets the Arduino's baud rate
}

void loop()
{
  if (BTSerial.available()) // checks for incoming data
  {
    char data = BTSerial.read(); // reads the incoming data
    if (data == '1') // if the data is "1"
    {
      digitalWrite(ledPin, HIGH); // turn the LED on
      Serial.println("LED turned on"); // print a message
    }
    else if (data == '0') // if the data is "0"
    {
      digitalWrite(ledPin, LOW); // turn the LED off
      Serial.println("LED turned off"); // print a message
    }
  }
}
Author

Write A Comment