Light Sensor * * Demonstrates the use a 16x2 LCD display. The

advertisement
/*
* LiquidCrystal Library - Light Sensor
*
* Demonstrates the use a 16x2 LCD display. The LiquidCrystal
* library works with all LCD displays that are compatible with the
* Hitachi HD44780 driver. There are many of them out there, and you
* can usually tell them by the 14-pin interface (16 w/ LED.)
*
*
* The circuit:
* LCD 1- Gnd
* LCD 2- +5v
* LCD 3- Gnd
* LCD 4- RS pin to digital pin 12
* LCD 5- Gnd
* LCD 6- Enable pin to digital pin 11
* LCD 11- D4 pin to digital pin 5
* LCD 12- D5 pin to digital pin 4
* LCD 13- D6 pin to digital pin 3
* LCD 14- D7 pin to digital pin 2
*
* This example code is in the public domain.
*
* http://www.arduino.cc/en/Tutorial/LiquidCrystal
****************************************************************/
// include the library code:
#include <Wire.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int a_in;
int sensorPin = 5;
int LEDpin = 7;
// create variable for storing analog readings
// variable for analog pin as A0 (IC pin 23)
// variable for LED pin
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
pinMode(LEDpin, OUTPUT);
// set digital pin as output
void loop() {
lcd.clear();
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
}
// read sensor, store in variable, print
lcd.setCursor(0, 1);
a_in = analogRead(sensorPin);
// get 10 bit analog value
a_in = map(a_in, 0, 1023, 0, 255); // scale it to 8 bits
lcd.print(a_in);
if(a_in < 80){
lcd.setCursor(0, 0);
lcd.print("Too high.");
digitalWrite(LEDpin, HIGH);
} else {
lcd.setCursor(0, 0);
lcd.print("Turn on!");
digitalWrite(LEDpin, LOW);
}
delay(20);
Download