Time displayed on LED incorrect but written to SD correct. Why the delay?

Hello to All!

HW - Arduino Uno, 0.56" 7-segment LED w/ backpack, SEEED SD Card Shield and MPL3115A2 sensor.

All data written to SD Shield is correct (time, date, temp *C & *F and hPa pressure). Per second. This part of the sketch is fine.

Data written to LED is not. Temp *C & *F and hPa pressure is perfect and is displayed for 4 seconds each. Time is completely off. It takes minutes or more to display the next minute. I think this has to do with packing everything in the same loop. I (obviously) don't get it.

Here is the part of the sketch that I think is problematic:

void loop () tempC = mySensor.getTemperature(); // Read Temperature from MPL3115A2 tempF = tempC*1.8 + 32.; // Convert degrees C to F pressure = mySensor.getPressure(); //Read Pressure //altitude = mySensor.getAltitude(); //Read Altitude hours = hour(t); minutes = minute(t); seconds = second(t);

mySensorData = SD.open("Weather.txt", FILE_WRITE);

matrix.print(tempC,1); matrix.writeDisplay(); delay(4000); //display *C for 4 seconds matrix.print(tempF,1); matrix.writeDisplay(); delay(4000); //display *F for 4 seconds matrix.print(pressure/100,0); matrix.writeDisplay(); delay(4000); //dispaly hPa for 4 seconds //matrix.print(altitude,0); //matrix.writeDisplay(); //delay(4000); matrix.writeDigitNum(0, (hours / 10) % 10, false); matrix.writeDigitNum(1, (hours % 10), false); matrix.drawColon(true); matrix.writeDigitNum(3, (minutes / 10) % 10, false); matrix.writeDigitNum(4, minutes % 10, false); matrix.writeDisplay(); ++t; delay(1000); //display time for 1 second

mySensorData.print(hours); mySensorData.print(":"); mySensorData.print(minutes); mySensorData.print(":"); mySensorData.print(seconds); mySensorData.print(","); mySensorData.print(tm.Day); mySensorData.print(" "); mySensorData.print(tm.Month); mySensorData.print(" "); mySensorData.print(tm.Year); mySensorData.print(","); mySensorData.print(tempC,1); //write temperature data to card mySensorData.print(","); //write a commma mySensorData.print(tempF,1); //write temperature data to card mySensorData.print(","); //write a commma mySensorData.println(pressure/100,0); //write pressure and end the line (println) mySensorData.close(); //close the file }

Thanks if advance.

Chris

The problem is always in the code that your DON'T post.

Please post the complete sketch and place it between [code] and [/code] tags

  hours = hour(t);
  minutes = minute(t);
  seconds = second(t);

What type is t? Where does it get a value? Where are these functions?

You f**k around with 13 seconds of delays, and then increment t by 1. What is the rationale behind that?

matrix.writeDigitNum(3, (minutes / 10) % 10, false);

In my world, minutes would contain a value between 0 and 59. Dividing that by 10 would give a value in the range 0 to 5. Applying the modulo operator, with a value of 10, to that makes no sense. What it the rationale behind doing that? What type is minutes? What values can it hold in your world?

First of all, thanks for the replies!

1 - Complete code follows

2 - I agree that 13 seconds of delay has A LOT to do with what’s happening when displaying time on the LED. The objective of the 4 seconds is to display the Temp and Pressure long enough for me to see it on the LED display. The one second, long enough to see the time. This definitely needs fixing. I can see the values on the LED, but the way I’m doing it is messing up the displayed time. Temp and pressure are fine both displayed and written to SD shield.

3 - I didn’t quite understand that either (this sketch is a patchwork of other sketches plus, but I think it’s this way because I’m displaying each digit (2 for hours and 2 for minutes plus the colon) in specific locations of the 7-segment LED. It works, I see the 2 digit hours, the colon and the the two digit minutes. Just not the correct time on the LED.

Once again. Thanks to All!

#include <Time.h>
#include <SD.h>      //Load SD card library
#include "Wire.h"    //Imports the wire library for talking over I2C
#include "Adafruit_MPL3115A2.h"
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_MPL3115A2 mySensor; //Create sensor object called mySensor
Adafruit_7segment matrix;   //Create LED object called matrix

float tempC;  // Variable for holding temp in C
float tempF;  // Variable for holding temp in F
float pressure; //Variable for holding pressure

int chipSelect = 10; //chipSelect pin for the SD card Reader

File mySensorData; //Data object you will write your sesnor data to

time_t t;
tmElements_t tm;

int seconds, minutes, hours;

void setup(){
  mySensor.begin();   //initialize pressure sensor mySensor

  matrix.begin(0x70);
  matrix.setBrightness(0);
  
  tm.Second = 0;
  tm.Minute = 43;
  tm.Hour = 21;
  tm.Day = 30;
  tm.Month = 11;
  tm.Year = 14;
  t = makeTime(tm);

pinMode(10, OUTPUT); //Must declare 10 an output and reserve it

SD.begin(10);} //Initialize the SD card reader
 
void loop(){
  tempC = mySensor.getTemperature(); //  Read Temperature from MPL3115A2
  tempF = tempC*1.8 + 32.; // Convert degrees C to F
  pressure = mySensor.getPressure(); //Read Pressure
  hours = hour(t);
  minutes = minute(t);
  seconds = second(t);
  
mySensorData = SD.open("Weather.txt", FILE_WRITE);

matrix.print(tempC,1);
matrix.writeDisplay();
delay(4000);
matrix.print(tempF,1);
matrix.writeDisplay();
delay(4000);
matrix.print(pressure/100,0);
matrix.writeDisplay();
delay(4000);
matrix.writeDigitNum(0, (hours / 10) % 10, false);
matrix.writeDigitNum(1, (hours % 10), false);
matrix.drawColon(true);
matrix.writeDigitNum(3, (minutes / 10) % 10, false);
matrix.writeDigitNum(4, minutes % 10, false);
matrix.writeDisplay();
++t;
delay(1000);

mySensorData.print(hours);
mySensorData.print(":");
mySensorData.print(minutes);
mySensorData.print(":");
mySensorData.print(seconds);
mySensorData.print(",");
mySensorData.print(tm.Day);
mySensorData.print(" ");
mySensorData.print(tm.Month);
mySensorData.print(" ");
mySensorData.print(tm.Year);
mySensorData.print(",");
mySensorData.print(tempC,1);          //write temperature data to card
mySensorData.print(",");              //write a comma
mySensorData.print(tempF,1);          //write temperature data to card
mySensorData.print(",");              //write a comma
mySensorData.println(pressure/100,0); //write pressure and end the line (println)
mySensorData.close();                 //close the file
}
matrix.writeDigitNum(0, (hours / 10) % 10, false);
matrix.writeDigitNum(1, (hours % 10), false);
matrix.drawColon(true);
matrix.writeDigitNum(3, (minutes / 10) % 10, false);
matrix.writeDigitNum(4, minutes % 10, false);

You haven’t changed this, so you must be in some universe where hours have more then 100 minutes and there are more than 100 hours in a day.

In that case, displaying time on 4 7 segment displays is not going to work.