# My first Arduino project - LCD Thermometer

#### Prototype

##### New Member
This project was my first as is about as simple as they get, but I thought I would share anyway. The code is made up of bits I stitched together, borrowed from other projects.

#include <LiquidCrystal.h> //import LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //define connections for lcd
const int sense = A0; //define analog input
void setup(){
Serial.begin(9600); //open a serial port
lcd.begin(16, 2); //define lcd size
lcd.setCursor(2,0);
lcd.print("Temperature:");
lcd.setCursor(6,1);
lcd.print("C");
lcd.setCursor(12,1);
lcd.print("F");
}

void loop(){
int senseVal = analogRead(sense); //read sensor value
float volt = (senseVal/1024.0)*5.0; //convert ADC reading to voltage
Serial.print("Temperature: ");
int tempC = (volt - .5)*100; //convert the voltage to temperature in degrees
int tempF = (tempC*9/5+32); //convert centigrade to farenheit
Serial.print(tempC);
Serial.print(" C, ");
Serial.print(tempF);
Serial.println(" F");
lcd.setCursor(3,1);
lcd.print(" "); //clears display for next reading
lcd.setCursor(3,1);
lcd.print(tempC);
lcd.setCursor(9,1);
lcd.print(" ");
lcd.setCursor(9,1);
lcd.print(tempF);
delay(1000); //wait 1 second between readings
}

#### Pommie

##### Well-Known Member
Most Helpful Member
Congratulations. Which temperature sensor are you using?

The only suggestion I would make is how you define the input pin,
const int sense = A0; //define analog input
This stores the value of A0 in program space. It is usually done with
#define sense A0
Note the lack of semi colon.

Mike.

#### Prototype

##### New Member
Congratulations. Which temperature sensor are you using?

The only suggestion I would make is how you define the input pin,
const int sense = A0; //define analog input
This stores the value of A0 in program space. It is usually done with
#define sense A0
Note the lack of semi colon.

Mike.
I'm using the TMP36, and thanks for the tip, I will give it a try when I can get Linux to play nicely with my Arduino.

I have another project in the pipeline at the moment, flashing 4 LED's in 4 different predetermined patterns, independently of each other. I think there are two ways to code it, one is simple, but messy and long winded having a single loop with lots of delays and a line of code for every time an led needs to turn on or off, the hardest part of that is figuring out which leds need to be turned on and which leds need to be turned off at each stage, I did start making a timing diagram/ truth table to help with that.

The other too complicated for me to figure out involving 4 loops running at the same time.

Last edited:

#### Pommie

##### Well-Known Member
Most Helpful Member
You can use millis() in your main code to execute as many "loops" as you want.
Code:
unsigned long time1=0,time2=0;   //add as many as you want

void setup() {
}

void loop() {
if((millis()-time1)>=20){
time1=millis();
//code to be executed every 20mS
}
if((millis()-time2)>=750){
time2=millis();
//code to be executed every 3/4 of a second - 750mS
}
}
There are many good explanations of this method on the web.

Mike.
Edit, changed if statements to prevent wrap around bug.

Last edited:

#### Wp100

##### Well-Known Member
You might find the lcd uses up too many pins as your projects exapnd, if so you can use either an I2C port expander chip or a cheap I2C adapter for the lcd, so only 2 lines needed, plenty of free libraries for it; also makes mounting the lcd in a project box a lot easier with just 4 wires.

On your programming problems, its often good to doodle a flow chart before you start the actual coding, helps spot any logic errors.

#### Dr_Doggy

##### Well-Known Member
i found the best results for my LEDS is that first i would set the led up to run on the TIMER0 interrupt so i can control brightness and color when they are on....(RGB's) ... plus then the leds dont lock up during other code delays

or put the leds on a pwm pin:
void SetColor(unsigned char R, unsigned char G, unsigned char B)
{
analogWrite(RedPin, R);
analogWrite(GreenPin, G);
analogWrite(BluePin, B);
}

then i use the millis loop(usually at 3 or 5 hundred ms) to switch the flashing pattern

.... one way i did it was that i would set up my millis for every 250 ms ... then i would run it through a buffer (depends on how complex you want the pattern to be)

char fastblink = 0b10101010 500ms duty
char medblink = 0b11001100 1s duty
char slowblink = 0b11110000 2s duty
char oneblink = 0b0000001 250ms on 1.75s off

if(blink &=1){ledson();blink=(blink>>1)+128}else{ledsoff();blink>=1} // rotates the buffer, and switches the leds

#### Prototype

##### New Member
i found the best results for my LEDS is that first i would set the led up to run on the TIMER0 interrupt so i can control brightness and color when they are on....(RGB's) ... plus then the leds dont lock up during other code delays

or put the leds on a pwm pin:
void SetColor(unsigned char R, unsigned char G, unsigned char B)
{
analogWrite(RedPin, R);
analogWrite(GreenPin, G);
analogWrite(BluePin, B);
}

then i use the millis loop(usually at 3 or 5 hundred ms) to switch the flashing pattern

.... one way i did it was that i would set up my millis for every 250 ms ... then i would run it through a buffer (depends on how complex you want the pattern to be)

char fastblink = 0b10101010 500ms duty
char medblink = 0b11001100 1s duty
char slowblink = 0b11110000 2s duty
char oneblink = 0b0000001 250ms on 1.75s off

if(blink &=1){ledson();blink=(blink>>1)+128}else{ledsoff();blink>=1} // rotates the buffer, and switches the leds
That looks like the ideal solution...I found a GIF which demonstrates the pattern, actually watching the pattern made me realise that it may not be as difficult as I first thought.
https://upload.wikimedia.org/wikipedia/commons/a/a7/Cardinal_Marks.gif

Loading