Problems with code for hc-05 bluetooth, dht11 temp sensor and lcd keypad

Hello guys.

I am quite a noob in Arduino field, but I have decided to make my qualification project at school based on Arduino.

I have connected Arduino Mega 2560 with LCD Keypad shield, HC-05 bluetooth module, 2 relay module and DHT 11 temp and humidity sensor.

I have had lots of problems with this project but now there is one I can’t solve by myself.

I have programmed that d22 pin turns HIGH when room temperature is higher than I have set it . Using Bluetooth Spp pro app on my android tablet I can regulate the ambient temperature, see all the data from serial port and also trigger the value of D24 pin.

The problem is that d22 doesn’t switch it’s value automatically unless I am triggering d24. Then they both turn to desired values.

Here is my code. I have compiled it from different sketches

#include <DHT11.h>
int pin=2;
int ambtemp ;
DHT11 dht11(pin); 
#include <LiquidCrystal.h>
/*******************************************************

This program will test the LCD panel and the buttons
Mark Bramwell, July 2010

********************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//int ledPin_1 = 9;

//int ledPin_3 = 3;

int ledPin_22 = 22;

int ledPin_24 = 24;

//int ledPin_26 = 26;

int lcd_key     = 0;

int adc_key_in  = 0;
//#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
//#define btnLEFT   3
//#define btnSELECT 4
#define btnNONE   5

// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(2);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 300) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 //if (adc_key_in < 5)   return btnRIGHT;  
 if (adc_key_in < 135)  return btnUP; 
 if (280 < adc_key_in < 290)  return btnDOWN; 
 //if (adc_key_in < 360)  return btnLEFT; 
 //if (adc_key_in < 370)  return btnSELECT;  

  return btnNONE;  // when all others fail, return this...
}




void setup()
{
   Serial.begin(9600);
  lcd.begin(16, 2);              // start the library
 
 //pinMode(ledPin_1, OUTPUT);

//pinMode(ledPin_3, OUTPUT);

pinMode(ledPin_22, OUTPUT);

pinMode(ledPin_24, OUTPUT);

//pinMode(ledPin_26, OUTPUT);
}

void loop()
{
  
  
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print("temp.:");
    //Serial.print("t:");
 Serial.print(temp);
 Serial.print("(");
 Serial.print(ambtemp);
 Serial.print(")");
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
 }
  else
  //{
   //Serial.println();
   //Serial.print("Error No :");
   //Serial.print(err);
   //Serial.println();    
  //}
  //delay(DHT11_RETRY_DELAY); //delay for reread

 
 
 if (Serial.available() > 0) {

int i = Serial.read()-48;
/*
Serial.print(" Taustina vertiba");
Serial.print(i);
Serial.println();
*/
if ((temp) > (ambtemp))
{
  digitalWrite(ledPin_22, HIGH),Serial.println(" Dzesetajs ieslegts ");
}

else
{
 digitalWrite(ledPin_22, LOW),Serial.println(" Dzesetajs izslegts "); 
}


if(i == 6 ) 
{
  ambtemp = ambtemp + 1; 
}  
 
else if(i == 9)   
{
  ambtemp = ambtemp - 1;
}

//if (i == 1)digitalWrite(ledPin_13, !digitalRead(ledPin_13)),Serial.println(" Led ");

//if (i == 2)digitalWrite(ledPin_3, !digitalRead(ledPin_3)),Serial.println(" ledPin_3 !digitalRead ");

if (i == 3)digitalWrite(ledPin_24, !digitalRead(ledPin_24)),Serial.println(" Relejs Nr.2 ");

}

lcd_key = read_LCD_buttons();  // read the buttons

 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   //case btnRIGHT:
    // {
     //lcd.print("RIGHT ");
     //break;
     //}
   //case btnLEFT:
     //{
     //lcd.print("LEFT   ");
     //break;
     //}
   case btnUP:
     {
     ambtemp = ambtemp + 1;
       //digitalWrite(ledPin_13,HIGH);
     break;
     }
   case btnDOWN:
     {
     ambtemp = ambtemp - 1;
       //digitalWrite(ledPin_13,LOW);
     break;
     }
   //case btnSELECT:
     //{
     //lcd.print("Liene");
     //break;
     //}
     //case btnNONE:
     //{
     //lcd.print("NONE  ");
     //break;
     //}
 }
 
lcd.setCursor(0,0);
 lcd.print("temp.:");
 lcd.print(temp);
 lcd.print(" (");
 lcd.print(ambtemp);
 lcd.print(")");
 lcd.setCursor(0,1);
 lcd.print("humidity:");
 lcd.print(humi);

delay (300);
}

Hi maris

Does it help if you change the following?

            if (temp > ambtemp)
            {
                digitalWrite(ledPin_22, HIGH);
                Serial.println(" Dzesetajs ieslegts ");
            }
  else
            {
                digitalWrite(ledPin_22, LOW);
                  Serial.println(" Dzesetajs izslegts "); 
            }

Regards

Ray

Thank yo Hack, but it didn't help. Seems like d22 works only when bluetooth gives signal to d24. This is strange because I had a working system two days ago. I have tried to replace all components except the lcd shield. Could it make the failure here?

P.S. Now After I have once triggered the d24 pin d22 regulates automatically as it should do but after a while it goes back to the dark side ]:)

  digitalWrite(ledPin_22, HIGH),Serial.println(" Dzesetajs ieslegts ");

Unless you are on the C++ standards committee, inventing new syntax is not a good idea. The comma operator has a very specific purpose. This is NOT what it is designed for.

DO NOT PUT MULTIPLE STATEMENTS ON ONE LINE.

PaulS:
DO NOT PUT MULTIPLE STATEMENTS ON ONE LINE.

Thank you!
I changed that part, but problem still remains. It just went more specific. I have programmed that I can also trigger d24 with btnRight on keypad. When I press it only d24 switches its value. But when I do the same via bluetooth from my tablet then d22 changes its value accordingly to the statement.

Here is my current code

#include <DHT11.h>
int pin=2;
int ambtemp ;
DHT11 dht11(pin); 
#include <LiquidCrystal.h>
/*******************************************************

This program will test the LCD panel and the buttons
Mark Bramwell, July 2010

********************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//int ledPin_1 = 9;

//int ledPin_3 = 3;

int ledPin_22 = 22;

int ledPin_24 = 24;

//int ledPin_26 = 26;

int lcd_key     = 0;

int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
//#define btnLEFT   3
//#define btnSELECT 4
#define btnNONE   5

// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(2);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 300) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 5)   return btnRIGHT;  
 if (adc_key_in < 135)  return btnUP; 
 if (280 < adc_key_in < 290)  return btnDOWN; 
 //if (adc_key_in < 360)  return btnLEFT; 
 //if (adc_key_in < 370)  return btnSELECT;  

  return btnNONE;  // when all others fail, return this...
}




void setup()
{
   Serial.begin(9600);
  lcd.begin(16, 2);              // start the library
 
 //pinMode(ledPin_1, OUTPUT);

//pinMode(ledPin_3, OUTPUT);

pinMode(ledPin_22, OUTPUT);

pinMode(ledPin_24, OUTPUT);

//pinMode(ledPin_26, OUTPUT);
}

void loop()
{
  
  
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
    Serial.print("temp.:");
    //Serial.print("t:");
 Serial.print(temp);
 Serial.print("(");
 Serial.print(ambtemp);
 Serial.print(")");
    Serial.print(" humidity:");
    Serial.print(humi);
    Serial.println();
 }
  else
  //{
   //Serial.println();
   //Serial.print("Error No :");
   //Serial.print(err);
   //Serial.println();    
  //}
  //delay(DHT11_RETRY_DELAY); //delay for reread

 
 
 if (Serial.available() > 0) {

int i = Serial.read()-48;
/*
Serial.print(" Taustina vertiba");
Serial.print(i);
Serial.println();
*/
if (temp > ambtemp)
            {
                digitalWrite(ledPin_22, HIGH);
                Serial.println(" Dzesetajs ieslegts ");
            }
  else
            {
                digitalWrite(ledPin_22, LOW);
                  Serial.println(" Dzesetajs izslegts "); 
            }

if(i == 6 ) 
{
  ambtemp = ambtemp + 1; 
}  
 
else if(i == 9)   
{
  ambtemp = ambtemp - 1;
}

//if (i == 1)digitalWrite(ledPin_13, !digitalRead(ledPin_13)),Serial.println(" Led ");

//if (i == 2)digitalWrite(ledPin_3, !digitalRead(ledPin_3)),Serial.println(" ledPin_3 !digitalRead ");

if (i == 3)
{
 digitalWrite(ledPin_24, !digitalRead(ledPin_24));
 Serial.println(" Relejs Nr.2 ");
}
}

lcd_key = read_LCD_buttons();  // read the buttons

 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
     digitalWrite(ledPin_24, !digitalRead(ledPin_24));
     break;
     }
   //case btnLEFT:
     //{
     //lcd.print("LEFT   ");
     //break;
     //}
   case btnUP:
     {
     ambtemp = ambtemp + 1;
       //digitalWrite(ledPin_13,HIGH);
     break;
     }
   case btnDOWN:
     {
     ambtemp = ambtemp - 1;
       //digitalWrite(ledPin_13,LOW);
     break;
     }
   //case btnSELECT:
     //{
     //lcd.print("Liene");
     //break;
     //}
     //case btnNONE:
     //{
     //lcd.print("NONE  ");
     //break;
     //}
 }
 
lcd.setCursor(0,0);
 lcd.print("temp.:");
 lcd.print(temp);
 lcd.print(" (");
 lcd.print(ambtemp);
 lcd.print(")");
 lcd.setCursor(0,1);
 lcd.print("humidity:");
 lcd.print(humi);

delay (300);
}
 if (adc_key_in < 5)   return btnRIGHT;  
 if (adc_key_in < 135)  return btnUP;

If the first comparison is true, so must the second one. In which case, both the functions will be called.

if (280 < adc_key_in < 290)  return btnDOWN;

The C language needs this to be written out as follows:

if ( 280 < adc_key_in && adc_key_in < 290 ) etc ...

If the first comparison is true, so must the second one. In which case, both the functions will be called.

Excuse me, but how is the second statement going to be reached after the first one returns?

Excuse me, but how is the second statement going to be reached after the first one returns?

I was absolutely wrong.

Sorry for the very red herring!

Hi Maris

I have programmed that I can also trigger d24 with btnRight on keypad. When I press it only d24 switches its value.

When you press btnRight, are you expecting both d24 and d22 to change value?

when I do the same via bluetooth from my tablet then d22 changes its value accordingly to the statement.

What character do you send from your tablet to change d22? And are you expecting that d24 also changes value, or just d22?

Thanks

Ray

No. I am not sending anything to change d22. It changes itself (if conditions are requiring changes) when I change d24. And now d22 works only when there is some bluetooth action. If I press btnRight only d24 switches not d22.

But I start to think I have fried some electronics here. Now d22 changes its value if I am reconnecting 3,3V power for bluetooth module. Android app probably does something for it and that's why changes appear only when there is some action via bluetooth. I had an accident with wrongly connected power supply and now I am quite sure relays are messing things up, but looks like Uno board itself makes some strange actions even when relays are disconnected

if (Serial.available() > 0) 
        {
            int i = Serial.read()-48;
            if (temp > ambtemp)
            {
                digitalWrite(ledPin_22, HIGH);
                Serial.println(" Dzesetajs ieslegts ");
            }
            else
            {
                digitalWrite(ledPin_22, LOW);
                Serial.println(" Dzesetajs izslegts "); 
            }

Pin 22 will only change state if a character has been received on serial. Is that what you want to happen?

Hackscribble:

if (Serial.available() > 0) 

{
            int i = Serial.read()-48;
            if (temp > ambtemp)
            {
                digitalWrite(ledPin_22, HIGH);
                Serial.println(" Dzesetajs ieslegts “);
            }
            else
            {
                digitalWrite(ledPin_22, LOW);
                Serial.println(” Dzesetajs izslegts ");
            }




Pin 22 will only change state if a character has been received on serial. Is that what you want to happen?

This is exactly what I didn’t want. Thank you Hack. You all guys are amazing. I moved this part of code out of brackets and all the system works. I am very grateful to ya all. :sweat_smile:

Only my relay module is damaged because of wrong power supply, but I am planning to hack it and control directly without module. This is not programming question anymore but I don’t wan’t to start new topic. Do you think it is possible to damage Arduino board by connecting it without additional power supply as it supposed to be? I measured and it takes 70mAmps to hold the coil. Arduino specification says 40mA max.

Generally it's Very Poor practice... to use the maximum limits for anything except a defined "Drop Dead" point. I tend (through 45 years experience) to be very conservative... If my design 'requires' more than 25%of the Maximum available pin current then it's time to use a buffer... Conditions are always variable.. to a degree but your example of 70 mA for a relay coil violates two important ones... 1. Never... Never, Ever exceed the Max rating of ANY parameter... even when you are certain that it Isn't An Issue Whatsoever. 2. NEVER, NEVER, EVER USE A CONTROLLER PIN TO DIRCTLY SWITCH AN INDUCIVE LOAD OF ANY TYPE, even with a back emf diode for protection. The diode will, if it is fast enough and big enough (to safely dissipate the energy stored in the magnetic field) deal ONLY with the back end... but the surge current from making the afor mentioned magnetic field will exceed the MAX pin current and can very easily be an order of magnitude greater than the "ABSOLUTE MAXIMUM RATING"... While it 'might' work... there are always unintended consequences... Remember that there are "Conductors" or "Wires" inside the IC... and quite small ones at that. High current pulses drop voltage across those "Wires" and those "voltage pulses" created from the current pulses will typically be the source of many types of "undefined behavior" in your code. Noisy power supplies are the most frequent design mistakes... Small fet's are ideal as buffers... Use them... And use bypass caps , 100 nF as close to the chip as physically possible! and a 1 uF (minimum) bypass for every 2 IC'S ON A PCB or more for breadboard development environment. Don't forget the diode... while there is a body diode in the fet it generally Isn't a good part for controlling the charge stored in an inductor..

Doc

Thank you Doc! I even can't get such a quick advice from my professor. He hasn't answered my email for two days when I was asking about programming.

I have to finish writing my paper now because deadline is on friday, but for the project presentation I have one more week. I hope I will manage to make those relays work properly, because I can't order them from China so quickly.

I have one more programming question according to my system. How can I include hysteresis in code to prevent relay bouncing at the moment when room temperature floats around the set ambient temperature. The goal I want to reach is that d22 goes to the LOW state after room temperature falls one degree under the ambient temperature but next time it goes to HIGH level only after room temperature exceeds the set ambtemp.

How can I include hysteresis in code to prevent relay bouncing at the moment when room temperature floats around the set ambient temperature. The goal I want to reach is that d22 goes to the LOW state after room temperature falls one degree under the ambient temperature but next time it goes to HIGH level only after room temperature exceeds the set ambtemp.

What have you tried? This is pretty simple.

if(roomTemp > setPoint)
    // It's time to turn on the AC
else if(roomTemp < setPoint - fudgeFactor)
    // It's time to turn off the AC

Yes that was exactly this simple. Thank you Paul. I did something similar before, but I forgot to remove existing else statement. Now it works. This forum is even better than college. I can learn things very quickly without sitting in classes.