HM-10 bluetooth module not receiving

For some Reason the bluetooth module keeps printing out -1 in the serial monitor when i send a number through from my phone.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
byte redPin = 9;
byte greenPin = 8;
byte bluePin = 7;
byte whitePin = 5;
String readString;
int readInt;

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(whitePin, OUTPUT);
}
void loop() {

  while (mySerial.available() == 0) {
  }

  readString = mySerial.readString();
  readInt = mySerial.parseInt();
  Serial.println(mySerial.read());
  Serial.println("DATA RECEIVED:");
  analogWrite(whitePin, readInt);

  if (readString == "red on" || readString == "Red on") {
    digitalWrite(redPin, HIGH);
    Serial.println("led on");
    mySerial.print("LED has turned on!");
  }

  if (readString == "red off" || readString == "Red off") {
    digitalWrite(redPin, LOW);
    Serial.println("led off");
    mySerial.print("LED has turned off!");
  }

  if (readString == "green on" || readString == "Green on") {
    digitalWrite(greenPin, HIGH);
    Serial.println("led on");
    mySerial.print("LED has turned on!");
  }

  if (readString == "green off" || readString == "Green off") {
    digitalWrite(greenPin, LOW);
    Serial.println("led off");
    mySerial.print("LED has turned off!");
  }
  if (readString == "blue on" || readString == "Blue on") {
    digitalWrite(bluePin, HIGH);
    Serial.println("led on");
    mySerial.print("LED has turned on!");
  }
  if (readString == "blue off" || readString == "Blue off") {
    digitalWrite(bluePin, LOW);
    Serial.println("led off");
    mySerial.print("LED has turned off!");
  }
  if (readString == "all on" || readString == "All on") {
    digitalWrite(bluePin, HIGH);
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, HIGH);
    Serial.println("All LED on");
    mySerial.print("All LED are on!");
  }

  if (readString == "all off" || readString == "All off") {
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    Serial.println("All LED off");
    mySerial.print("All LED are off!");
  }
}

You've just read everything there was to read, so this read returns -1

Hint: this has nothing at all to do with the installation of the IDE.

The same thing as mentioned by @anon56112670 .

Perhaps the second line should be:

readInt = readString.toInt(); ?

I have moved your topic to a more appropriate forum category @turbmoke .

In the future, please take some time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

Make sure that the baud rate of HM-10 is the same as the baud rate of software serial.

You can scope down the issue, firstly run a simple sketch to print out all what Arduino receive from Bluetooth module. If data received is correct, the problem is in how you handle data (readString, int .....), You can see the simple code to print out data received from HM-10 in this Arduino BLE with HM-10 tutorial

is there a way to read two different variable types from the arduino?

i tried it and when it read a string it would set the integer to 0, is there a way i can still keep the integer value the same even though you a reading a string?

Can you post the code, the results, and the serial monitor settings?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.