Code Improvement

Hi all, I was looking for some help to improve my code and make it more streamlined. My code is for a basic clock with thermometer I am making with a parallel lcd and adafruits RTC breakout. I would also like to display the temperature to three significant figures e.g. 23.5 degrees instead of e.g. 23.51 degrees.

I would appreciate any help I can get as I have just started using an Arduino. Thanks

#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int temperaturePin = 0;



void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  Wire.begin();
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop() {
 float temperature = analogRead(temperaturePin);  //getting the voltage reading from the temperature sensor
 temperature = (temperature - .5) * 100;
 temperature = (temperature)  * .004882814;
 temperature = temperature - 32;
 temperature = temperature / 1.8;  
  DateTime now = RTC.now();
    lcd.setCursor(3, 1);
    lcd.print(now.day(), DEC);
    lcd.print('/');
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.year(), DEC);
    lcd.setCursor(0, 0);
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);
    lcd.setCursor(10, 0);
 lcd.print(temperature);
  lcd.setCursor(15, 0);
 lcd.print("C");
    delay(1000);
 lcd.clear();

 
}

Is there something you want it to do that it isn't doing? Is it not doing something you thought it would?

Perhaps you could clarify what you mean by "streamlined"?

If you mean optimizing it to make it run faster, there is nothing you could do to that block of code to make it execute more quickly in any meaningful way. The various I/O operations (reading the temperature pin and the RTC, writing to the LCD) will take up 99%+ of the time it takes to execute that loop, and there isn't much to be done about it. The temperature calculation could be substantially simplified with a pencil, paper, and a little algebra, and the construction of the string to be printed to the LCD could probably be sped up by several orders of magnitude by using a char array and some clever use of sprintf(), but those optimizations would have no real impact on the overall speed of the loop.

If by streamlined you mean less code (a la perl golf), I'm not going to help you there. With the exception of the erratic indentation, that is a refreshingly readable chunk of C++ code.

Whether or not you chose to simplify it, I strongly recommend commenting up that temperature arithmetic - someday you'll wonder what it was you were doing there.

I assume RTC.adjust sets the time and date?

If so

RTC.adjust(DateTime(DATE, TIME));

sets it to the time the file was compiled, it that what you had in mind?


Rob

my main questions are: How well is my code well put together? And how can i change the amount of significant figures of temperature is displayed on my lcd.

thanks

For such short programs it is very hard to argue in ANY direction. Unless the exect requirements / environmental constraints are known it is completely unclear how and why to optimize.

Possible directions: - Code does not match the requirements - Requirements unknown or not fully understood - To many comments in it - Not enough comments in it - Licensing not suitable for commercial application - Licensing not suitable for open source - License information missing - Violates naming and style guidelines - Project has no naming or style guidelines - Inadequate / missing handling of component failure - Inadequate / missing handling of power failure

Or it is: completely suitable small prototype

Choose whatever your want ;)

If you want to learn about creating good code "Code Complete" by "McConnel" is a good place to start.

Udo

my main questions are: How well is my code well put together?

With the exception of the lack of comments (already mentioned) that describe how the temperature is derived from the raw sensor reading, and the uneven indentation (already mentioned, too, but possibly a cut and paste issue), the code is put together fairly well.

What does seem out of order is writing a bunch of stuff to the display, then waiting a second, then erasing the screen. A more logical order of operations, to me, would be to clear the screen before writing new data to it.

And how can i change the amount of significant figures of temperature is displayed on my lcd.

 lcd.print(temperature);

You are printing a float using the default behavior of the print() method for floats. The LiquidCrystal class derives from the Print class. It does not override any of the print() methods, so all the actual display of data on the screen is handle by the Print class.

The Print::print() method that handles floats has an optional second argument that controls the number of digits to display after the decimal point. It defaults to 2.

Now, it isn't clear whether you want to display the temperature to decimal places or always using 4 characters, including the decimal point.

If you want one digit after the decimal place, add a second argument with a value of 1. 23.5167 will be printed as 23.5. 9.2124 will be printed as 9.2.

If you want to use 4 characters always, you will need to determine the correct number of decimal places based on the magnitude of the temperature. Greater than 100 ==> 0 decimal places. Greater than or equal 10 and less than 100 ==> 1 decimal place. Less then 10 ==> 2 decimal places. What to do with negative values is up to you.