Skip to content

Instantly share code, notes, and snippets.

@ethnt
Last active April 27, 2017 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethnt/de384660da330450321e8fad81c5f729 to your computer and use it in GitHub Desktop.
Save ethnt/de384660da330450321e8fad81c5f729 to your computer and use it in GitHub Desktop.
Arduino project
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonPin = 7;
int buttonState;
int buttonPresses = 0;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(0, 1);
lcd.print("Light: ");
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
buttonPresses = buttonPresses + 1;
}
printTemperature(analogRead(A0));
printLight(analogRead(A1));
delay(1000);
}
void printTemperature(int sensorValue) {
lcd.setCursor(7, 0);
double temp = (double)sensorValue / 1024;
temp = temp * 5;
temp = temp - 0.5;
double tempC = temp * 100;
double tempF = tempC * (9.0 / 5.0) + 32;
String output;
if (buttonPresses % 2 == 0) {
output = String(tempF) + "F";
} else {
output = String(tempC) + "C";
}
lcd.print(output);
}
void printLight(int sensorValue) {
lcd.setCursor(7, 1);
sensorValue = sensorValue - 165;
String output = String(sensorValue);
lcd.print(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment