Sunday, October 14, 2018

day 14 10/11/18

Arduino LCD with I^2C Temp Sensor
#1. Interface your I2C temperature to the Arduino and have the values of the temperature shown on the screen. Allow the user to press one of the buttons and have it display the temperature in o F and the other button to display it in oC.

//LCD text with incrementing number
//Include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
const int temp_address = 73; //1001100 written as decimal number
int c = 0, f = 0;
//Start the time at 0
int time = 0;
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
  lcd.begin(16,2);
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 Wire.begin();
}
void loop() {
 lcd.setCursor(0,1);
 // read the input on analog pin 0:
 int sensorValue = analogRead(A0);
 // print out the value you read:
 Serial.println(sensorValue);
  //Send a request
 //Start talking to the device at the specified address
 Wire.beginTransmission(temp_address);
 //Send a bit asking for register zero, the data register
 Wire.write(0);
 //Complete Transmission
 Wire.endTransmission();
 //Read the temperature from the device
 //Request 1 Byte from the specified address
 Wire.requestFrom(temp_address, 1);
 //Wait for response
 while(Wire.available() == 0);
 //Get the temp and read it into a variable
 c = Wire.read();
 //Do some math to convert the Celsius to Fahrenheit
 f = round(c*9.0/5.0 +32.0);
 //Send the temperature in degrees C and F to the serial monitor
 Serial.print(c);
 Serial.print("C ");
 Serial.print(f);
 Serial.print("F ");
 delay(500);
  if(sensorValue >= 477 && sensorValue < 719){
    lcd.print(f);
    lcd.print("f         ");
    int val = 1;
    while (val == 1){
  sensorValue = analogRead(A0);
  delay(100);
  if(sensorValue >= 719 && sensorValue < 1023){
    val --;
  }
  }
 }
  else if(sensorValue >= 719 && sensorValue <= 1023){
  lcd.print(c);
  lcd.print("c         ");
 }
 delay(1); // delay in between reads
}

No comments:

Post a Comment