Different delays for my serial monitor and LCD

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mooredesign13
 
Posts: 37
Joined: Fri Apr 17, 2020 12:18 pm

Different delays for my serial monitor and LCD

Post by mooredesign13 »

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

#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.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

Post by adafruit_support_bill »

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

Code: Select all

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

	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

User avatar
mooredesign13
 
Posts: 37
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

Post by mooredesign13 »

That, totally worked! Thank you!

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

Post by adafruit_support_bill »

Good to hear. Thanks for the follow-up.

User avatar
mooredesign13
 
Posts: 37
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

Post by mooredesign13 »

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

#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.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

Post by adafruit_support_bill »

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

//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);


User avatar
mooredesign13
 
Posts: 37
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

Post by mooredesign13 »

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.

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

Post by adafruit_support_bill »

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

User avatar
mooredesign13
 
Posts: 37
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

Post by mooredesign13 »

How do I do that?


User avatar
mooredesign13
 
Posts: 37
Joined: Fri Apr 17, 2020 12:18 pm

Re: Different delays for my serial monitor and LCD

Post by mooredesign13 »

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

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: Different delays for my serial monitor and LCD

Post by adafruit_support_bill »

Good to hear. Thanks for the follow-up!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”