Due to high demand and inclement weather, expect some shipping delays at this time, orders may not ship for 4-5 business days.
0

Different delays for my serial monitor and LCD
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Different delays for my serial monitor and LCD

by mooredesign13 on Sun Dec 05, 2021 4:50 pm

I'm doing a data logging project. I have a few sensors wired up and I have an LCD display that cycles the temp, humidity and light values ever 3 seconds (3000 delay time). This code runs in the void loop. At the end of this, it runs the serial.print function to create a CSV of each of these values in the serial monitor. But I only want it to record a value here every 10 minutes or so. I do not need data points every 9 seconds. However, I also do not want a 10 minute delay before the LCD cycles again. How can I fix this? Seems like a simple fix but I am a beginner.

Right now, I only have the delay time at the end of the loop set to 15000ms because I don't want to wait ten minutes to watch the code cycle. I'll fix that later.

Code: Select all | TOGGLE FULL SIZE
#include "DHT.h"
#define Type DHT22
#include <LiquidCrystal.h>

int rs=7; //LCD Pins are 7 through 12
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int htPin=2;
int lightPin=A0;
int sensorValue;
DHT HT(htPin,Type);
float humidity;
float tempC;
float tempF;
int setTime=500;
int dt=15000;
int vPin=3;
int LCDdelay=3000;
 
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
HT.begin();
delay(setTime);
lcd.clear();
lcd.begin(16,2);
pinMode(vPin, OUTPUT);
}
 
void loop() {
  digitalWrite(vPin, HIGH);
humidity=HT.readHumidity();
tempC=HT.readTemperature();
tempF=HT.readTemperature(true);
sensorValue=analogRead(lightPin);

lcd.clear();
  lcd.setCursor(0,0);
lcd.print("Current Temp");
lcd.setCursor(0,1);
lcd.print(tempF);
lcd.print(" F");
delay(LCDdelay);

lcd.clear();
  lcd.setCursor(0,0);
lcd.print("Current Humidity");
lcd.setCursor(0,1);
lcd.print(humidity);
lcd.print(" %");
delay(LCDdelay);

lcd.clear();
  lcd.setCursor(0,0);
lcd.print("Light Level");
lcd.setCursor(0,1);
lcd.print(sensorValue);
lcd.print(" units");
delay(LCDdelay);
 
Serial.print(humidity);
Serial.print(",");
Serial.print(tempF);
Serial.print(",");
Serial.println(sensorValue);
delay(dt);
 
 
}
Last edited by adafruit_support_bill on Sun Dec 05, 2021 5:43 pm, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums.

mooredesign13
 
Posts: 30
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

by adafruit_support_bill on Sun Dec 05, 2021 5:53 pm

First add variables at global scope to store the serial display interval and the last serial display time:

Code: Select all | TOGGLE FULL SIZE
long SerialInterval = 600000; // 10 minutes
long lastSerialDisplayTime= 0;


Then in your loop() function, wrap your serial display code in this 'if' statement:

Code: Select all | TOGGLE FULL SIZE
   if (millis() - lastSerialDisplayTime >= SerialInterval) // has the display interval elapsed?
   {
      lastSerialDisplayTime = millis(); // update last display time

      Serial.print(humidity);
      Serial.print(",");
      Serial.print(tempF);
      Serial.print(",");
      Serial.println(sensorValue);
   }


In general, delays are not a great way to do timing. Especially if your code needs to do multiple things:
https://learn.adafruit.com/multi-taskin ... -the-delay

adafruit_support_bill
 
Posts: 82897
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

by mooredesign13 on Sun Dec 05, 2021 7:57 pm

That, totally worked! Thank you!

mooredesign13
 
Posts: 30
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

by adafruit_support_bill on Sun Dec 05, 2021 8:03 pm

Good to hear. Thanks for the follow-up.

adafruit_support_bill
 
Posts: 82897
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

by mooredesign13 on Mon Dec 06, 2021 1:28 am

Hi Bill, You're quickly becoming my favorite admin ;)

Quick question: Is there any reason why an LCD screen (standard 16x2) would not work simultaneously with the datalogger? I am able to run it easily with the code I had shown. then I added in all the code needed to run the adafruit data logging shield, and the characters on the display are not showing up correctly. I know it is not a hardware issue, If I stitch back to my first code, it works fine.

Code: Select all | TOGGLE FULL SIZE
#include "DHT.h"
#define Type DHT22
#include <LiquidCrystal.h>
#include "SD.h"
#include <Wire.h>
#include "RTClib.h"
//Datalogging
#define LOG_INTERVAL  1000 // mills between entries
#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()
RTC_PCF8523 RTC;
const int chipSelect = 10;
#define greenLEDpin 4
File logfile;
//LCD
int rs=6; //LCD Pins are 6 through 12
int en=7;
int d4=8;
int d5=9;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int htPin=2;
int lightPin=A0;
int sensorValue;
DHT HT(htPin,Type);
float humidity;
float tempC;
float tempF;
int setupTime=500;
int dt=15000;
//int vPin=3;
int LCDdelay=3000;

long SerialInterval = 1000; // 10 minutes
long lastSerialDisplayTime= 0;
 
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.print("Initializing SD card...");
HT.begin();
delay(setupTime);

lcd.begin(16,2);
lcd.clear();
//pinMode(vPin, OUTPUT);
pinMode(chipSelect, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
pinMode(A0, INPUT);
pinMode(2, INPUT);
 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
 
 /* if (! logfile) {
    error("couldnt create file");
  }*/
 
  Serial.print("Logging to: ");
  Serial.println(filename);
//Starting Wire
  Wire.begin(); 
  if (!RTC.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
 
 logfile.println("temperature,Humidity,Light");   

  }
 
 // pinMode(redLEDpin, OUTPUT);
  //pinMode(greenLEDpin, OUTPUT);
 
   // If you want to set the aref to something other than 5v
  //analogReference(EXTERNAL);


 
void loop() {
 // digitalWrite(vPin, HIGH);
humidity=HT.readHumidity();
tempC=HT.readTemperature();
tempF=HT.readTemperature(true);
sensorValue=analogRead(lightPin);

lcd.clear();
  lcd.setCursor(0,0);
lcd.print("Current Temp");
lcd.setCursor(0,1);
lcd.print(tempF);
lcd.print(" F");
delay(LCDdelay);

lcd.clear();
  lcd.setCursor(0,0);
lcd.print("Current Humidity");
lcd.setCursor(0,1);
lcd.print(humidity);
lcd.print(" %");
delay(LCDdelay);

lcd.clear();
  lcd.setCursor(0,0);
lcd.print("Light Level");
lcd.setCursor(0,1);
lcd.print(sensorValue);
lcd.print(" units");
delay(LCDdelay);

if (millis() - lastSerialDisplayTime >= SerialInterval)
   {lastSerialDisplayTime = millis();
Serial.print(tempF);
Serial.print(",");
Serial.print(humidity);
Serial.print(",");
Serial.println(sensorValue);

logfile.print(tempF);
logfile.print(",");
logfile.print(humidity);
logfile.print(",");
logfile.print(sensorValue);
logfile.print(",");
   }
 //digitalWrite(greenLEDpin, LOW);
}


As you can see, I've started to try debugging by commenting out lines of code, but no luck.

mooredesign13
 
Posts: 30
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

by adafruit_support_bill on Mon Dec 06, 2021 6:24 am

What processor are you using? On an Arduino UNO, pins 11-13 are part of the SPI bus that is used to communicate with the SD card. So there would be a conflict here with the LCD as wired:

Code: Select all | TOGGLE FULL SIZE
//LCD
int rs=6; //LCD Pins are 6 through 12
int en=7;
int d4=8;
int d5=9;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);


adafruit_support_bill
 
Posts: 82897
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

by mooredesign13 on Mon Dec 06, 2021 2:55 pm

Okay, I have pins 0, 1, 3,4,5 available, so I'll try switching things around. I assume with this shield, I cant use pins 3, 4 or 10 since those go to the LED's and to the logger. so Pins 0 and 1 it is! Oh wait, Research shows me these are for serial communications So I think I can use the A2 and A3 pins. They should be able to do a digital output...

Now it works!

But the datalogger file on the SD card is empty. After running the code for a few minutes, I see the CSV data being generated in the serial monitor, but when I open the csv in Excel, it has no data.

mooredesign13
 
Posts: 30
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

by adafruit_support_bill on Mon Dec 06, 2021 3:44 pm

You probably need to periodically flush or close and re-open the file to force the data to be written to the card.

adafruit_support_bill
 
Posts: 82897
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

by mooredesign13 on Mon Dec 06, 2021 5:17 pm

How do I do that?

mooredesign13
 
Posts: 30
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

by adafruit_support_bill on Mon Dec 06, 2021 5:36 pm


adafruit_support_bill
 
Posts: 82897
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

by mooredesign13 on Wed Dec 08, 2021 1:05 pm

Thank you! that worked. I've finished the project and I appreciate your help on this.

mooredesign13
 
Posts: 30
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

by adafruit_support_bill on Wed Dec 08, 2021 1:09 pm

Good to hear. Thanks for the follow-up!

adafruit_support_bill
 
Posts: 82897
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.