-1

I have an arduino board connected to 2 temperature sensors, 2 switch buttons and 1 lcd display.

When the switch button changes to off, "off" is printed on the lcd display. When the button state is on, the temperature is printed on the lcd display.

The problem is when it shows "off" its still possible to see the temperature value for "1 millisecond" each second. I don't know what is causing the "off" message to change for a millisecond.

Source code:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>

#define DS18B20_SENSOR1 2               // pino de dados do DS18D20
#define DS18B20_SENSOR2 4               // pino de dados do DS18D20

int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;

float Temp1, Temp2;

OneWire oneWire1(DS18B20_SENSOR1);       // Objeto do baramento One-wire
DallasTemperature sensor1(&oneWire1);     // Objeto do sensor DS18D20

OneWire oneWire2(DS18B20_SENSOR2);       // Objeto do baramento One-wire
DallasTemperature sensor2(&oneWire2);     // Objeto do sensor DS18D20

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup(void)
{
  Serial.begin(3000);
  Serial << F("Teste do Sensor 1") << endl << endl;
  Serial << F("Teste do Sensor 2") << endl << endl;
  sensor1.begin();
  sensor2.begin();
  lcd.begin();
  lcd.setBacklight(HIGH);


  /*
   * Control Variables
   */

  pinMode(5, INPUT);
  pinMode(7, INPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void turnAllOff1(){
  digitalWrite(11, LOW); 
  digitalWrite(12, LOW); 
  digitalWrite(13, LOW);
}

void turnAllOff2(){
  digitalWrite(8, LOW); 
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
}

void printTemp1(){
  lcd.setCursor (0,0);
  lcd.print("Sensor1: ");
  lcd.print(Temp1);
  lcd.print("C");
}

void printOff1(){
  lcd.setCursor (0,0);
  lcd.print("Sensor1: ");
  lcd.print(" off   ");  
}

void printTemp2(){
  lcd.setCursor (0,1);
  lcd.print("Sensor2: ");
  lcd.print(Temp2);
  lcd.print("C"); 
}

void printOff2(){
  lcd.clear();
  lcd.setCursor (0,1);
  lcd.print("Sensor2: ");
  lcd.print(" off   "); 
}

void loop(){
  buttonState1 = digitalRead(5);
  buttonState2 = digitalRead(7); 

  if(buttonState1 != lastButtonState1){
    if(buttonState1 == HIGH){
      sensor1.requestTemperatures();
      Temp1 = sensor1.getTempCByIndex(0);
      turnAllOff1();
      if(Temp1 < 36){
        digitalWrite(13, HIGH);
      }
      else if(Temp1 > 38){
        digitalWrite(11, HIGH); 
      }
      else{
        digitalWrite(12, HIGH);
      }
      printTemp1();
    }
    else{
      printOff1();
    }
  }
  lastButtonState1 = buttonState1;

  if(buttonState2 != lastButtonState2){  
    if(buttonState2 == HIGH){
      sensor2.requestTemperatures();
      Temp2 = sensor2.getTempCByIndex(0);
      turnAllOff2();
      if(Temp2 < 0){
        digitalWrite(10, HIGH);
      }
      else if(Temp2 > 2){
        digitalWrite(8, HIGH); 
      }
      else{
        digitalWrite(9, HIGH); 
      }
      printTemp2();
    }
    else{
      printOff2(); 
    }
  }
  lastButtonState2 = buttonState2;
}
0

You need to use INPUT_PULLUP pin mode and connect the buttons between the pins and ground.

pinMode(5, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);

Later on you will also have to debounce the button but that is another question (where you can find plenty of answers already).

Cheers!

  • No sir, that doesn't solve the problem. I even looked up on the debounce problem. If I use "INPUT_PULLUP" it won't recognize the state of the button hence not showing the "off" message. – rubito Mar 8 '16 at 21:47
  • You also need to change the button logic (buttonState2 == LOW) as pushing the button will given a LOW signal with PULLUP. – Mikael Patel Mar 8 '16 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.