Bluetooth not working with Temperature Sensors

Hello,

Firstly, thanks for taking the time to read this. I'm having an issue with a project for college.

I can get my arduino to read in temperature from two DS18B20 temperature sensors.
Separately I can get the arduino to turn on/off an led using my phone and a bluetooth module.

But when I combine the two programs, the arduino will read in the commands coming from my phone, but will no longer turn on and off the LED.

any ideas what the problem could be?

Here is the code:

//Libaries to include
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

//Set up for Bluetooth Communication
SoftwareSerial HM10(2, 3); // RX = 2, TX = 3
char appData;  
String inData = "";

// Data wire is plugged into port 7 on the Arduino
#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Addresses of 2 DS18B20s
uint8_t sensor1[8] = { 0x28, 0xFF, 0x64, 0x0E, 0x74, 0x58, 0x4B, 0x45 };
uint8_t sensor2[8] = { 0x28, 0xFF, 0x64, 0x0E, 0x74, 0x4E, 0x3F, 0x9A };


void setup()
{
  Serial.begin(9600);
  Serial.println(" ");
  Serial.println("HM10 serial started at 9600");

  HM10.begin(9600); // set HM10 serial at 9600 baud rate

  pinMode(13, OUTPUT); // onboard LED
  digitalWrite(13, LOW); // switch OFF LED

  HM10.print("Input Desired Temperature");
  //HM10.print(" ");
  HM10.print(' ');
  delay(500);

  sensors.begin();
}

void loop()
{

//HERE
  sensors.requestTemperatures();

  Serial.print("Sensor 1: ");
  printTemperature(sensor1);
  
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
  
  Serial.println();
  delay(1000);
  //TO HERE

  HM10.listen();  // listen the HM10 port

  while (HM10.available() > 0) {   // if HM10 sends something then read

    appData = HM10.read();
    inData = String(appData);  // save the data in string format
    Serial.write(appData);

  }

 
  // Read user input if available.
  if (Serial.available()) {

    delay(10);

    HM10.write(Serial.read());
  }

  if ( inData == "F") {

    Serial.print(" ");
    Serial.println("LED OFF");

    digitalWrite(13, LOW); // switch OFF LED

    delay(500);
  }


  if ( inData == "N") {

    Serial.print(" ");
    Serial.println("LED ON");

    digitalWrite(13, HIGH); // switch OFF LED

    delay(500);
  }

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print(tempC);
  Serial.print("C ");
}

If I comment out the temperature reading part of the code between "//HERE" and "//TO HERE" Then the led will turn on and off no problem. But even just having the line "sensors.requestTemperatures();" without the instructions to print the temperatures will cause the program to not turn on/off the LED. However the Serial Monitor will still display the instructions I am sending from my phone, so I know that the arduino is receiving the instructions, but it is not using them to enter the IF loops.

Thank you in advance for any help you can offer.

Ok, after looking at the code again I tried the following and it worked:

I changed: while (HM10.available() > 0)
to if (HM10.available() > 0)

So I don't need a response, but maybe someone will benefit from this being here.

Do you understand WHY the above doesn't work the way you intended?

I don't 100% know why it didn't function as intended.

However, while troubleshooting I made the program print to the serial monitor every time the program entered that loop.
Every time I sent a message to the arduino, the program would enter the loop 3 times.
Now with the if statement, the program only enters once.

I'm assuming there will be issues with the robustness of the program, but I haven't investigated further.

Have you any insights you'd like the share? I'm always happy to learn.

The only parameter in the "while" loop is never changed by the code in the loop, therefor the loop goes on forever. There is NO magic that makes that change for you.

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