Problems with displaying different sensor values on lcd with ir sensor.

I am trying to make a weather station with multiple sensors that displays the readings on an LCD display and sends them to the internet with a wifi module. The problem is that I can't fit all the data onto the lcd since it's a 16x4 lcd so I decided to use a ir sensor and remote to switch between them, but I cant get the reading onto the lcd. It shows only the last reading before i pressed the button. How do I make it display realtime readings since I also have a photocell vater level and pressure sensors?

#include <dht.h>
#include <LiquidCrystal.h>
#include <IRremote.h>
#include <IRremoteInt.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

dht DHT;
int RECV_PIN = 10;          //  The digital pin that the signal pin of the sensor is connected to
IRrecv receiver(RECV_PIN);  //  Create a new receiver object that would decode signals to key codes
decode_results results;
#define DHT11_PIN 9
int vodaPin = A5;
int voda;
int photocellPin = A0;
int svjetlost;
const int numReadings = 7;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;


void setup() {

  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.begin(16, 2);

  receiver.enableIRIn();
}

void loop()
{

  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(photocellPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits

  voda = analogRead(vodaPin);
  Serial.println(voda);
  Serial.println(average);
  int chk = DHT.read11(DHT11_PIN);
  Serial.print(DHT.temperature); Serial.print("C");
  Serial.print(" ");
  Serial.print(DHT.humidity); Serial.print("%");

  if (receiver.decode(&results)) {            //  Decode the button code and put it in "results" variable

    if (results.value ==  0xFF30CF ) {          //  Key '1' is pressed which belongs to the blue LED
      lcd.setCursor(0, 0);
      lcd.print("Temp: ");
      lcd.print(DHT.temperature);
      lcd.print((char)223);
      lcd.print("C");
      lcd.setCursor(0, 1);
      lcd.print("Humidity: ");
      lcd.print(DHT.humidity);
      lcd.print("%");
      lcd.noDisplay();
      delay(500);
      // Turn on the display:
      lcd.display();
      delay(500);

    }
    if (results.value == 0xFF18E7) {
      lcd.clear();
      delay(10);
      lcd.setCursor(0, 0);
      lcd.print("Svjetlost:");
      lcd.print(average);
      lcd.noDisplay();
      delay(500);
      // Turn on the display:
      lcd.display();
      delay(500);
    }

    if (results.value == 0xFF7A85) {
      lcd.clear();
      delay(10);
      lcd.setCursor(0, 0);
      lcd.print("Nivo vode:");
      lcd.print(voda);
      lcd.noDisplay();
      delay(500);
      // Turn on the display:
      lcd.display();
      delay(500);
    }

    receiver.resume();
  }
}

I want it to display real time values on the lcd and also want to change the sensor that's displayed on the lcd with the ir sensor and remote.

I forgot to mention it, but I also get an error when I put this int chk = DHT.read11(DHT11_PIN); into the "case". It says they are interrupting each other.

Hi,

Isolate the different parts of your project and get them working one by one (i.e.: by simulating the sensor readout in a separate routine get sure that the display work; then, by using the serial monitor, set up the sensors functioning; ...)

Regards

vffgaston:
Hi,

Isolate the different parts of your project and get them working one by one (i.e.: by simulating the sensor readout in a separate routine get sure that the display work; then, by using the serial monitor, set up the sensors functioning; ...)

Regards

Thanks for the answer. The codes and sensors are working properly when I tested them alone. My biggest problem is the dht11 sensor since I cant use analogRead for that( i think I can't). And they do display the correct values on the serial monitor even now but they don't display them on the lcd.

You need to separate the "what to display" part of your code from the "display what needs to be displayed now" part of the code.

The code to read the IR sensor should ONLY change the "what to display" setting. The rest of loop() should collect data, regardless of whether it will be displayed, or not. You loop() functions should look like:

void loop()
{
   determineWhatToDisplay();
   getDataToDisplay();
   displayWhatNeedsToBeDisplayed();
}

Given these function names, it should be easy to determine what parts of the (excessive) code in loop() now goes into each function.

NikolaJE:
Thanks for the answer. The codes and sensors are working properly when I tested them alone. My biggest problem is the dht11 sensor since I cant use analogRead for that( i think I can't). And they do display the correct values on the serial monitor even now but they don't display them on the lcd.

Yes, you can't: the DHT11 outputs a digital sequence and the analogRead is to reading analog values.

If you can see the correct values through the serial monitor, you have to be able to show them in the LCD.

Have you managed to show anything else in the LCD ("23" for instance)?

Regards

Do as Paul suggests.

I'd even supress the IR function for the moment (whatever it does) and focus in the "getDataToDisplay()" and "displayWhatNeedsToBeDisplayed()" ones.

Do you know how to split the code in functions?

Regards

vffgaston:
Do as Paul suggests.

I'd even supress the IR function for the moment (whatever it does) and focus in the "getDataToDisplay()" and "displayWhatNeedsToBeDisplayed()" ones.

Do you know how to split the code in functions?

Regards

No,I don't this is my first bigger projects, Im building something like a botanical garden( idk how to translate it) in which I can monitor and control most conditions.
Is there a tutorial on how to do what you and the first person said?

NikolaJE:
No,I don't this is my first bigger projects, Im building something like a botanical garden( idk how to translate it) in which I can monitor and control most conditions.
Is there a tutorial on how to do what you and the first person said?

https://www.arduino.cc/en/Reference/FunctionDeclaration

.

ieee488:
https://www.arduino.cc/en/Reference/FunctionDeclaration

.

Thank you

NikolaJE:
No,I don't this is my first bigger projects, Im building something like a botanical garden( idk how to translate it) in which I can monitor and control most conditions.
Is there a tutorial on how to do what you and the first person said?

Arduino is not a Plug and Play bussiness; nevertheless is good having a goal (your garden, whatever it translates -I'm not english native neither).

Try solving your three modules one by one (DTH11 one seems to be solved). Once you have the three pieces of code working it will be easier (not easy, but easier) to make the whole working. Use dummy variables to test things (the "23" I told you last post).

Regards

(Your code looks nice: Have you some background on programming?)

vffgaston:
Arduino is not a Plug and Play bussiness; nevertheless is good having a goal (your garden, whatever it translates -I'm not english native neither).

Try solving your three modules one by one (DTH11 one seems to be solved). Once you have the three pieces of code working it will be easier (not easy, but easier) to make the whole working. Use dummy variables to test things (the "23" I told you last post).

Regards

(Your code looks nice: Have you some background on programming?)

Thanks. I do know a bit of html and css and I just started learning java.
I'm just not sure how to use this

void loop()
{
   determineWhatToDisplay();
   getDataToDisplay();
   displayWhatNeedsToBeDisplayed();
}

Paul suggests you to divide the whole problem into three functions, that would be called sequentally (inside the main "loop", that repeats continuosly; is the orthodox procedure)

Each one are the three parts I told you to develop separately.

Regards.

vffgaston:
Paul suggests you to divide the whole problem into three functions, that would be called sequentally (inside the main "loop", that repeats continuosly; is the orthodox procedure)

Each one are the three parts I told you to develop separately.

Regards.

So I first make tose functions in loop or setup? and then call them in the if case for the ir sensor? Right?
And thanks for the help.

My advice is to make three different pieces of code (.ino files). Once they work one by one, to merge them is not difficult (as long as they are quite small ones).

Is the LCD working? (Can you display what you want?)

Regards

vffgaston:
My advice is to make three different pieces of code (.ino files). Once they work one by one, to merge them is not difficult (as long as they are quite small ones).

Is the LCD working? (Can you display what you want?)

Regards

The LCD works and it shows the value the sensors had when I pressed the button, but it doesn't change until I press the button on the remote again. So i should just make separate codes and then when I want to use them in the If function for the Ir sensor and lcd I should just write something like

lcd.print(DHT.temperature) ...?

NikolaJE:
The LCD works and it shows the value the sensors had when I pressed the button, but it doesn't change until I press the button on the remote again. So i should just make separate codes and then when I want to use them in the If function for the Ir sensor and lcd I should just write something like

lcd.print(DHT.temperature) ...?

OK. Things are better than I thought. Let me have a look on the code ...
Regards

vffgaston:
OK. Things are better than I thought. Let me have a look on the code ...
Regards

i am really thankful for your help. Since it looks like you really know what you are doing can you pls just add the function for at least the dht sensor into my code and just paste it here? I must finish the project tomorrow and I still need to add some leds and a stepper motor.
Thanks again

NikolaJE:
i am really thankful for your help. Since it looks like you really know what you are doing can you pls just add the function for at least the dht sensor into my code and just paste it here? I must finish the project tomorrow and I still need to add some leds and a stepper motor.
Thanks again

I am afraid things are not so simple ...

Are you seeing temperature and humidity values in the serial monitor?

Regards

vffgaston:
I am afraid things are not so simple ...

Are you seeing temperature and humidity values in the serial monitor?

Regards

Yes they are. Everything is displayed on the serial monitor, although the water level isn't showing good results.
The only problem is that they aren't updated real time on the LCD. I also tried using lcd.display and noDisplay but it didn't work and only showed the data for a bit and then just cleared the screen.