Buttons to change threshold

Hello,

I'm making project of arduino 1602 keypad shield, relay module and dht11.

I have no idea,and I have not found anything familiar to what I need.

First, I want to change the limit from which relay module gets a signal,by buttons.

Ex. if (h > 50) , UP button would increase 50 by 1, DOWN button decrease it by 1.

Second, I want to do that, buttons LEFT/RIGHT would change the screens, first screen would show current temperature and humidity, second screen would show limits from which heater and fan will start working also, when changing that limit you could see it.

Any links or thoughts?

Any links or thoughts?

To what? Reading when a switch changes state is trivial. See the state change detection example. Incrementing value could hardly be easier (value++;).

Write a sketch that simply displays your first screen. Put this in your setup function so it just does it the once. Post your code once you've achieved this and I'll instruct you how to move forward.

Is it far from OK overall?

#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;
int h = dht.readHumidity();
int t = dht.readTemperature();

void setup() {

lcd.begin(16, 2);

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //Somehow I have inversed signals to control relays

pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH);


{
 lcd.setCursor(0,0);
 lcd.print("TEMP=");
 lcd.print(t);
 lcd.print(" *C");
 lcd.setCursor(0,1);
 lcd.print("DREGME=");
 lcd.print(h);
 lcd.print("% ");

}
}
void loop() 
 {
 if (h > count)
 {digitalWrite(RELAY, LOW);
 }
 else
 {digitalWrite(RELAY, HIGH);
 }
  if (t > count1)
 {digitalWrite(RELAY2, LOW);
 }
 else
 {digitalWrite(RELAY, HIGH);
 }
 }

But now it just doesn’t work.

Ok a few issues FIRSTLY When posting code you MUST enclose it within [code] and [/code] tags.

Secondly I notice you don’t have any code in there to look at those buttons. Do you have this sorted yet?

Thirdly, here’s your above effort with the syntax errors cleared up (but nothing more just yet).

#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;
int h = dht.readHumidity();
int t = dht.readTemperature();



void setup() {

lcd.begin(16, 2);

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //Somehow I have inversed signals to control relays

pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH);

h = dht.readHumidity();
t = dht.readTemperature();
  lcd.setCursor(0,0);
  lcd.print("TEMP=");
  lcd.print(t);
  lcd.print(" *C");
  lcd.setCursor(0,1);
  lcd.print("DREGME=");
  lcd.print(h);
  lcd.print("% ");

}

 void loop()
 {

h = dht.readHumidity();
t = dht.readTemperature();

 if (h > count)
  {digitalWrite(RELAY, LOW);
  }
  else
  {digitalWrite(RELAY, HIGH);
  }
   if (t > count1)
  {digitalWrite(RELAY2, LOW);
  }
  else
  {digitalWrite(RELAY, HIGH);
  }
 }

Sorry, thanks for that. But I’m facing more few problems when I moved lcdprint part to setup. Screen does not refresh, obviously because lcd print part is not in loop, so it should not refresh numbers on screen yes?
Also when limit is reached and relay gets a signal, it goes into strange loop on/off.
It works with this code properly:

#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;


void setup() {

lcd.begin(16, 2);

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //Somehow I have inversed signals to control relays

pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH);

int h = dht.readHumidity();
int t = dht.readTemperature();
 
  
}
 void loop() 
 { 
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  
  lcd.setCursor(0,0);
  lcd.print("TEMP=");
  lcd.print(t);
  lcd.print(" *C");
  lcd.setCursor(0,1);
  lcd.print("DREGME=");
  lcd.print(h);
  lcd.print("% ");
  
  if (h > count)
  {digitalWrite(RELAY, LOW);
  }
  else
  {digitalWrite(RELAY, HIGH);
  }
   if (t > count1)
  {digitalWrite(RELAY2, LOW);
  }
  else
  {digitalWrite(RELAY2, HIGH);
  }
  }

EdgarasA3: Sorry, thanks for that. But I'm facing more few problems when I moved lcdprint part to setup. Screen does not refresh, obviously because lcd print part is not in loop, so it should not refresh numbers on screen yes?

Yes that's correct, but you want your display to be dependent on which part of the menu system you're in, yes? So although you've already managed to switch your relays and successfully write to the screen, your next issue should be getting your button inputs recognised.

Have you got any code that allows you read your buttons succesfully?

Not sure if it’s total nonsense, or it’s ok.
But I guess I can’t increase count if it’s not in setup. But this is the only way how I think I could read buttons.

#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;
int h = dht.readHumidity();
int t = dht.readTemperature();
int BUT0 = A0;
int BUT1 = A1;
int BUT2 = A2;
int BUT3 = A3;
int BUT4 = A4;
int BUT5 = A5;

int STATEBUT0 = 0;
int STATEBUT1 = 0;
int STATEBUT2 = 0;
int STATEBUT3 = 0;
int STATEBUT4 = 0;
int STATEBUT5 = 0;



void setup() {

lcd.begin(16, 2);

pinMode(BUT0, INPUT);
pinMode(BUT1, INPUT);
pinMode(BUT2, INPUT);
pinMode(BUT3, INPUT);
pinMode(BUT4, INPUT);
pinMode(BUT5, INPUT);

digitalWrite(BUT0, HIGH);
digitalWrite(BUT1, HIGH);
digitalWrite(BUT2, HIGH);
digitalWrite(BUT3, HIGH);
digitalWrite(BUT4, HIGH);
digitalWrite(BUT5, HIGH);

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //Somehow I have inversed signals to control relays

pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH);

h = dht.readHumidity();
t = dht.readTemperature();
  lcd.setCursor(0,0);
  lcd.print("TEMP=");
  lcd.print(t);
  lcd.print(" *C");
  lcd.setCursor(0,1);
  lcd.print("DREGME=");
  lcd.print(h);
  lcd.print("% ");

}

 void loop()
 
 {
STATEBUT0 = digitalRead(BUT0);
STATEBUT1 = digitalRead(BUT1);
STATEBUT2 = digitalRead(BUT2);
STATEBUT3 = digitalRead(BUT3);
STATEBUT4 = digitalRead(BUT4);   
STATEBUT5 = digitalRead(BUT5);

h = dht.readHumidity();
t = dht.readTemperature();
 if (STATEBUT0 = HIGH)
 {
   count++;
 }
 if (h > count)
  {digitalWrite(RELAY, LOW);
  }
  else
  {digitalWrite(RELAY, HIGH);
  }
   if (t > count1)
  {digitalWrite(RELAY2, LOW);
  }
  else
  {digitalWrite(RELAY, HIGH);
  }
 }

Yes I think you’re right. It’s nonsense :slight_smile:

I think the keypads on those LCD shields generally use a resistance ladder on A0. Try this and see if you get the expected results when you press your buttons.

#include "DHT.h"
#include <LiquidCrystal.h>
#define KEYPAD_NONE -1
#define KEYPAD_RIGHT 0
#define KEYPAD_UP 1
#define KEYPAD_DOWN 2
#define KEYPAD_LEFT 3
#define KEYPAD_SELECT 4

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;
int h = dht.readHumidity();
int t = dht.readTemperature();


void setup()
{
lcd.begin(16, 2);
}

int lastKey=KEYPAD_NONE;

void loop()
{
int currentKey=buttonDown();

if (lastKey!=currentKey)
  {
   lcd.clear();
   switch(currentKey)
    { 
    case KEYPAD_NONE:
         lcd.print("NOTHING DOWN");
         break;
    case KEYPAD_RIGHT:
         lcd.print ("RIGHT");
         break;   
    case KEYPAD_UP:
         lcd.print ("UP");
         break;   
    case KEYPAD_DOWN:
         lcd.print ("DOWN");
         break;   
  case KEYPAD_LEFT:
         lcd.print ("LEFT");
         break;   
  case KEYPAD_SELECT:
         lcd.print ("SELECT");
         break;   
    } 
  }
lastKey=currentKey;
}


int buttonDown()
{
int value;
 
  static int NUM_KEYS=5;
  static int adc_key_val[5] ={  
    30, 150, 360, 535, 760     };
  int k, input;
  input=analogRead(0);
  for (k = 0; k < NUM_KEYS; k++)
  {
    if (input < adc_key_val[k])
    {
      return k;
    }
  }
  if (k >= NUM_KEYS)
    k = -1;     // No valid key pressed
  return k;
}

Your example doesn’t boot up. ^

I’ve tried to write code above, with this tutorial http://www.raywenderlich.com/32392/arduino-tutorial-for-complete-beginners-using-a-button.

EdgarasA3: Your example doesn't boot up. ^

I've tried to write code above, with this tutorial http://www.raywenderlich.com/32392/arduino-tutorial-for-complete-beginners-using-a-button.

Well my example won't show anything unless it finds a key input. How are your buttons connected to the arduino?

It’s from A0 to A5. I’ve just put the shield on to arduino board. But i guess it’s the only pin that matters is A0. I guess different button gives different value to A0 pin, so I need to read those values to know which button was pressed? :neutral_face:

#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;

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

int read_LCD_buttons()

{
  
 adc_key_in = analogRead(0);      
 if (adc_key_in > 1000) return btnNONE; 

 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT; 
 return btnNONE;
 
}
void setup() {

I’ve found this part on the internet example, but i don’t know if it’s complete code, and how to use described buttons. This is a beginning of my current code.

Hope not to get banned for double post,but i need quick advice, but is this correct way to read buttons and make them work?

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:
     {
     lcd.print("UP    ");
     break;
     }
   case btnDOWN:
     {
     lcd.print("DOWN  ");
     break;
     }
   case btnSELECT:
     {
     lcd.print("SELECT");
     break;
     }
     case btnNONE:
     {
     lcd.print("NONE  ");
     break;
     }
 }

}

Code is without errors,but when pressing buttons nothing happens, maybe values are wrong, or wrong code to read them.

#include "DHT.h"
#include <LiquidCrystal.h>

#define DHTPIN 22   
#define DHTTYPE DHT11

#define RELAY 24
#define RELAY2 26

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int count = 50;
int count1 = 25;

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

int read_LCD_buttons()

{
  
 adc_key_in = analogRead(0);      
 if (adc_key_in > 1000) return btnNONE; 

 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT; 
 return btnNONE;
 
}
void setup() {

lcd.begin(16, 2);

pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //Somehow I have inversed signals to control relays

pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH);

int h = dht.readHumidity();
int t = dht.readTemperature();
 
  
}
 void loop() 
 { 
  int h = dht.readHumidity();
  int t = dht.readTemperature();
  
   lcd_key = read_LCD_buttons();  

 switch (lcd_key)               
 {
   case btnRIGHT:
     {
     lcd.print("RIGHT ");
     break;
     }
   case btnLEFT:
     {
     lcd.print("LEFT   ");
     break;
     }
   case btnUP:
     {
     lcd.print("UP    ");
     break;
     }
   case btnDOWN:
     {
     lcd.print("DOWN  ");
     break;
     }
   case btnSELECT:
     {
     lcd.print("SELECT");
     break;
     }
     case btnNONE:
     {
     lcd.print("NONE  ");
     break;
     }
 }
 
  delay(10000);
  lcd.setCursor(0,0);
  lcd.print("TEMP=");
  lcd.print(t);
  lcd.print(" *C");
  lcd.setCursor(0,1);
  lcd.print("DREGME=");
  lcd.print(h);
  lcd.print("% ");
  
  if (h > count)
  {digitalWrite(RELAY, LOW);
  }
  else
  {digitalWrite(RELAY, HIGH);
  }
   if (t > count1)
  {digitalWrite(RELAY2, LOW);
  }
  else
  {digitalWrite(RELAY2, HIGH);
  }
  }

Changed
from

if (adc_key_in > 1000) return btnNONE;

to

if (adc_key_in > 1500) return btnNONE;

now it shows pressed button

But still a problem how to switch screens using buttons and increase value in one of the sreens

Anyone?

if (adc_key_in > 1500) return btnNONE;

The maximum value returned by analogRead() is 1023. This test will NEVER be true.

Why not print the value of adc_key_in so you KNOW what the code is doing, instead of guessing?

Finished using easiest way, using case and delaying for couple of secs.

Thanks everyone for helping.