I have created this code for a digital thermometer that I am making. It works fine as is but I need to add a switch to change between F and C instead of just displaying F. Can i add a switch input and then have it skip the C steps if its F or vice versa? I'm very new to the arduino and programming as a whole so thank you for your help.
#include <DHT.h> //Including the temp sensor library
#include <DHT_U.h>
#include <Adafruit_Sensor.h>
#include "LiquidCrystal.h" // Including the LCD library
#define DHTPIN 6 // Declares pin 6 for communicating with the DHT22 sensor
#define DHTTYPE DHT22 // Declares the type of DHT sensor we are using (DHT22)
DHT dht(DHTPIN, DHTTYPE); // Declares the DHT connection and type
LiquidCrystal lcd(12,11,10,9,8,7); // Declares the LCD connections
int Fan_Pin1 = 4; // pin 2 on L293D
int Fan_Pin2 = 3; // pin 7 on L293D
int Enable = 5; // pin 1 on L293D
int LED = 2; //pin 1 on LED
int Alarm = 13; // pin 1 on alarm
void setup() {
pinMode(Fan_Pin1, OUTPUT); //setting up fan, led, and alarm as outputs
pinMode(Fan_Pin2, OUTPUT);
pinMode(Enable, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(Alarm, OUTPUT);
lcd.begin(16,2); // Initializes the LCD
dht.begin(); // Initializes DHT22 temp sensor
}
void loop() {
lcd.clear(); // Clear the LCD
float temp = dht.readTemperature(); // Reading the temperature in Celsius
float degF = temp * 9 / 5 + 32; //converting celsius reading to fahrenheit
if (isnan(degF)) { // Validating received data
lcd.print("Failed to read");
delay(1000); // Delays by 1 second
return;
}
lcd.setCursor(0,0);
lcd.print("Temp: "); //Writes "Temp: " in the first row
lcd.print(degF); // Writes the recieved temperature in the first row
lcd.print(" F"); //Writes " F" after the actual temperature
lcd.setCursor(0,1); // Sets the second row to display the fan speed
if(degF <75 ) { // If the temperature less than 75
analogWrite(Enable,0); // 0% PWM duty cycle
lcd.print("Fan OFF "); // Prints that the fan is off on LCD
delay(100);
}
else if(degF>=76) { // If the temperature is above 76
analogWrite(Enable, 255); // 100% duty cycle
lcd.print("Fan Speed: 100% ");
delay(100);
}
else if(degF>=90) { // If the temperature is above 90
analogWrite(Enable, 255); // 100% duty cycle
lcd.print(" SHUT DOWN NOW!! ");
digitalWrite(LED, HIGH); // Turns on warning LED
digitalWrite(Alarm, HIGH); //Sounds the warning alarm
delay(100);
}
digitalWrite(Fan_Pin1, LOW); // To drive the fan in a particular direction
digitalWrite(Fan_Pin2, HIGH); // To drive the fan in a particular direction
delay(2000); // 2 seconds delay
}