Trouble with reading a sensor and DS1307 RTC chip

Hello fellow Arduino fans.

I'm kinda new to the whole thing but have managed to get up and running quite quickly thanks to the awesome support.
I've come across a little niggle though and it's probably due to me being new to C (I'm a VB.NET programmer).

My question is this:

I'm trying to write Temperature (DS18B20) AND Time (DS1307) at the same time to the LCD but when I execute the command to get the temp, my time seems to stop running smoothly for just a second or 2 whilst the temp sensor is sending data.

I'm assuming it's a programming thing so I'd appreciate some pointers if you have any.
P.S. The switch is being de-bounced using a Schmitt Trigger so don't worry if "void swap()" looks a little too simple :slight_smile:

The Sketch is as follows so far:

//LCD
//          RS        -> Arduino Digital 10
//          E         -> Arduino Digital 11
//          DB4       -> Arduino Digital 6
//          DB5       -> Arduino Digital 5
//          DB6       -> Arduino Digital 4
//          DB7       -> Arduino Digital 3

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Time.h>

#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
//char* weekdayname[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char* myDate;
char date[10];
char time[8];

#define ONE_WIRE_BUS 7                      // Data wire is plugged into pin 7 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);              // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
LiquidCrystal lcd(10, 11, 6, 5, 4, 3);      // initialize the library with the numbers of the interface pins
DallasTemperature sensors(&oneWire);        // Pass our oneWire reference to Dallas Temperature

const int ledPin = 13;
const int switchPin = 2;
volatile int lastButton = LOW;

unsigned long myTimer = millis();

void setup(void)
{
  // Query the sensor
  sensors.begin();
  Wire.begin();
  Serial.begin(9600);
  
  lcd.begin(16, 2);
  lcd.setCursor(3,0);
  lcd.print("Warming up");
  delay(1000);
  
  lcd.clear();
  
  setTime(22,35,00,21,7,13); // (hr,min,sec,day,month,yr)

  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  lcd.print(getTemperature());
  lcd.print((char)223);

  //Attatch the interrupt
  attachInterrupt(0, swap, RISING);
  
}

void loop(void)
{ 
  if (millis() - myTimer >= 30000){
  // call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus
  //sensors.requestTemperatures();
  lcd.setCursor(0,0);
  lcd.print(getTemperature());
  lcd.print((char)223);
  myTimer = millis();
  }
  
  lcd.setCursor(0,1);
  time_t t = now();
  String stringTime = String(hour(t)) + ":" + String(minute(t)) + ":" + String(second(t));
  lcd.print(stringTime);
}

//Function to toggle the LED/Relay from On to Off and vice versa
  void swap()
  {
    if (digitalRead(switchPin) == HIGH && lastButton == LOW)
    {
      lastButton = HIGH;
    }
    else
    {
      lastButton = LOW;
    }
    digitalWrite(ledPin, lastButton);
  }

//Function to return the Temperature from sensor
  float getTemperature()
  {
    sensors.requestTemperatures();
    return sensors.getTempCByIndex(0);
  }

//Function to convert Binary Coded Decimal to Decimal
  byte decToBcd(byte val)
  {
    return ((val/10*16) + (val%10));
  }

//Function to convert Decimal to Binary Coded Decimal
  byte bcdToDec(byte val)
  {
    return ((val/16*10) + (val%16));
  }

myTimer is a lousy name for the last time you got the temperature. Something like lastTempReading would be better.

  String stringTime = String(hour(t)) + ":" + String(minute(t)) + ":" + String(second(t));
  lcd.print(stringTime);

Quit pissing resources away like this. 5 calls to lcd.print() will not take any longer.

but when I execute the command to get the temp, my time seems to stop running smoothly for just a second or 2 whilst the temp sensor is sending data.

The function to get the temperature is a blocking function. Time keeps running smoothly, but the time variables are not updated.

The sensor can be read differently - start a reading and come back later to see if it's done. Whether the effort required to do this is worth the gain, or not, only you can decide.

PaulS:
Quit pissing resources away like this. 5 calls to lcd.print() will not take any longer.

Hi Paul
I've only had my Arduino for a week now so I'm really not aware of all the intricacies of what's going on yet.

Are you saying that something like
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);

would use less resources than throwing it all together into one string up front?

I've looked everywhere but just can't seem to find a library that's capable of returning either the date or time portion of a date...

Thanks for the help though.
More reading is obviously required :slight_smile:

This library has a getFormatted & getFormatted2K command that returns the date & time formatted into a single char array.

Are you saying that something like
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);

would use less resources than throwing it all together into one string up front?

Yes. And, it's String, not string, that pisses away resources like there's no tomorrow.

I've looked everywhere but just can't seem to find a library that's capable of returning either the date or time portion of a date...

Why do you need a library? You have direct access to hour, minute, second and/or month, day, year.

Riva:
This library has a getFormatted & getFormatted2K command that returns the date & time formatted into a single char array.

Thank you kindly!
I will take a look at this right away.