In: Electrical Engineering
Write a Micro C program to:
-Measure temperature using LM35
-Show the temperature on LCD
You can use headers. if you use header, send headers with main code as homework.
/* LCD + LM35 Temperature sensor arduino sketch
includes fahrenheit and kelvin modes
push button to chnage modes
default 10 minutes inactivity power off time
*/
#include <avr/sleep.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd (10, 9, 8, 7, 6, 5);
byte degree[8] =
{
0b00000,
0b11100,
0b10100,
0b11100,
0b00000,
0b00000,
0b00000,
};
const int buttonPin = 1; // the pin that the pushbutton is
attached to
const int tempPin = 0;
const long interval = 500; // the delay in milliseconds for the
temperature reading to update
unsigned long previousMillis = 0;
int buttonState; // current state of the button
int lastButtonState; // previous state of the button
float tempC = 0;
float tempF = 0;
float tempK = 0;
int reading = 0;
unsigned long pwrofftime = 600000; //initial time before powering
down in milliseconds
int tempmode = 0; //0 = celcius, 1 = fahrenheit, 2 = kelvin
void setup()
{
analogReference(INTERNAL); //sets the reference voltage for
Arduino's ADC to 1.1 volts
pinMode(buttonPin, INPUT);
lcd.begin(8, 2);
lcd.createChar(8, degree);
lcd.setCursor(0, 0);
lcd.print("Hello");
lcd.setCursor(0, 1);
lcd.print("World!");
delay(1000);
}
void loop()
{
buttonState = digitalRead(buttonPin);
unsigned long currentMillis = millis();
if(buttonState != lastButtonState)
{
if (buttonState == LOW)
{
lcd.clear();
tempmode = tempmode + 1;
if(tempmode > 2)
{
tempmode = 0;
}
if(tempmode == 0)
{
lcd.setCursor(0, 0);
lcd.print("Celsius");
lcd.setCursor(0, 1);
lcd.print("Mode");
}
else if(tempmode == 1)
{
lcd.setCursor(0, 0);
lcd.print("Fahrenhe");
lcd.setCursor(0, 1);
lcd.print("it Mode");
}
else if (tempmode == 2)
{
lcd.setCursor(0, 0);
lcd.print("Kelvin");
lcd.setCursor(0, 1);
lcd.print("Mode");
}
}
pwrofftime = pwrofftime + currentMillis;
delay(500);
lastButtonState = buttonState;
}
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
int reading = analogRead(tempPin);
tempC = reading / 9.31;
tempF = (9/5)*tempC + 32;
tempK = tempC + 273.15;
lcd.setCursor(0, 0);
if(tempmode == 0)
{
lcd.print("TempC: ");
lcd.setCursor(0, 1);
lcd.print(tempC);
lcd.write(8);
lcd.print("C ");
}
else if(tempmode == 1)
{
lcd.print("TempF: ");
lcd.setCursor(0, 1);
lcd.print(tempF);
lcd.write(8);
lcd.print("F ");
}
else if(tempmode == 2)
{
lcd.print("TempK: ");
lcd.setCursor(0, 1);
lcd.print(tempK);
lcd.write(8);
lcd.print("K ");
}
}
if(pwrofftime < currentMillis)
{
sleep();
}
}
void sleep()
{
lcd.setCursor(0, 0);
lcd.print("Powering");
lcd.setCursor(0, 1);
lcd.print(" Down...");
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Powered ");
lcd.setCursor(0, 1);
lcd.print(" Down.");
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
}