[HM-10] Need help in simple coding!

Hello,

I have connected BT module to my Arduino Uno, everything work, but I want to change the code:

#include <SoftwareSerial.h>;

int TX_PIN = 10;
int RX_PIN = 11;

SoftwareSerial bluetooth(TX_PIN, RX_PIN);
String receivedBluetoothString = "";

int led = 12;

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

void loop() {
  while (bluetooth.available() > 0) {

    char receivedBluetoothChar = bluetooth.read();
    receivedBluetoothString += receivedBluetoothChar;

    if (receivedBluetoothChar == '\n') {

      if (receivedBluetoothString.toInt() == 1111) {
      }
    }
    receivedBluetoothString = "";
  }

}

And after Arduino (receivedBluetoothString.toInt() == 1111) should set pin X to HIGH, but after i send the same “1111” code the pin should go LOW. I tried coding with boolean ang long function but still it doesn’t work.

avik111:
Hello,

I have connected BT module to my Arduino Uno, everything work, but I want to change the code:

#include <SoftwareSerial.h>;

int TX_PIN = 10;
int RX_PIN = 11;

SoftwareSerial bluetooth(TX_PIN, RX_PIN);
String receivedBluetoothString = “”;

int led = 12;

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

void loop() {
 while (bluetooth.available() > 0) {

char receivedBluetoothChar = bluetooth.read();
   receivedBluetoothString += receivedBluetoothChar;

if (receivedBluetoothChar == ‘\n’) {

if (receivedBluetoothString.toInt() == 1111) {
     }
   }
   receivedBluetoothString = “”;
 }

}




And after Arduino (receivedBluetoothString.toInt() == 1111) should set pin X to HIGH, but after i send the same "1111" code the pin should go LOW. I tried coding with boolean ang long function but still it doesn't work.

Try this:

bool ledStage=false; // led initially is off
#define led 13  //  pin number that a LED is connected to

void setup(){
pinMode(led,OUTPUT);

...
}

void loop(){
...
  if (receivedBluetoothString.toInt() == 1111) {
    ledState = !ledState; // ledState will alternate between HIGH and LOW, LOW=!HIGH, HIGH=!LOW
    digitalWrite(led,ledState);
   }
 }

Chuck.

@chucktodd

Thank you very much, works flawlessly!

Regards, Lucas