/*==============================================================\\
|| Date : November 27th 2016 ||
|| Author : Unknown ||
|| Description: This project is aimed to help familarize myself ||
|| with different sensors and efficiency for programming. ||
\\==============================================================*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,10,11,12,13);
int page = 2;
const int temp = A0; //Temperature Sensor
const int phot = A1; //Photoresistor
const int lbtn = 2; //Left Button
const int rbtn = 3; //Right Button
int trig = 4; //sonar trigger
int echo = 5; //sonar echo
void setup(){
lcd.begin(16,2);
lcd.setCursor(0,0);
pinMode(lbtn,INPUT);
pinMode(rbtn,INPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
float temperature(int pin){
float t = (((analogRead(pin)/1024.0) * 5) - 0.5) * 100; //Conversion to celsius Degrees
return t;
}
int light(int pin){
//anything below value of 100 is considered dark/night time
return analogRead(pin);
}
int distance(int t, int e){
float cm;
digitalWrite(t, LOW); //Low high and low level take a short time to TrigPin pulse
delayMicroseconds(2);
digitalWrite(t, HIGH);
delayMicroseconds(10);
digitalWrite(t, LOW);
cm = pulseIn(e, HIGH) / 58.0; //Echo time conversion into cm
cm = (int(cm * 100.0)) / 100.0; //Keep two decimal places
return cm;
}
void loop(){
switch(page){
case 1:
lcd.clear();
lcd.print("Degree:");
lcd.setCursor(8,0);
lcd.print(temperature(temp));
lcd.setCursor(8,1);
lcd.print("Dstnce=>");
break;
case 2:
lcd.clear();
lcd.print("Dstnce:");
lcd.setCursor(8,0);
lcd.print(distance(trig,echo));
lcd.print(" cm");
lcd.setCursor(0,1);
lcd.print("<=Temp");
lcd.setCursor(8,1);
lcd.print("Light=>");
break;
case 3:
lcd.clear();
lcd.print("Light:");
lcd.setCursor(8,0);
lcd.print(light(phot));
lcd.setCursor(0,1);
lcd.print("<=Dstnce");
break;
}
//left button
if(digitalRead(2) == HIGH && page > 1){
page--;
}
//right button
if(digitalRead(3) == HIGH && page < 3){
page++;
}
delay(250);
}
pin 2 is left button pin 3 is right button.
youy press left and you go to the first item and such.
my question is can i revise this anymore? im hoping to clean this up or use more correct syntax. can someone proofread this code and critic it for efficiency.