//Robert Fisher
//Design Lab 1 Boat Project

//LCD Screen with Light Sensor


//You may have to replace this library call. To do that
//go to sketch>>import library>>Liquid Crystal. The 
//"#include seen below will be placed at the top of your sketch.
//Erase the one below and the sketch should be okay to be loaded.
#include <LiquidCrystal.h>

const int contrastSensor = 0; // Light Sensor pin

const int contrastPin = 6; // PWM Output to LCD screen [j28] 
//see SIK Guide>>circuit 15, page 78 in the component section

int contrast, high = 0, low = 1023; // set int contrast to read 
// multiple levels

LiquidCrystal lcd(12,11,5,4,3,2); // Pins for LCD

void setup()
{
  
  lcd.begin(16, 2); // Sets LCD rows and columns

  lcd.clear();

  lcd.print("hello, world!"); // This will eventually be 
  //turning status.
  
  Serial.begin(9600);// so we can see it on th ecomputer as well. 
  
}

void loop()
{
  contrast = analogRead(contrastSensor); // read sensor
  
  
  if (contrast < low)// Compound reading
  {
    low = contrast;
  }
  
  if (contrast > high)
  {
    high = contrast;
  }
  
  contrast = map(contrast, low+100, high-100, 0, 250);
  //Maps values of sensor and also makes the ranges smaller.
  //I did this because the sensor is not like the potentiometer.
  
  contrast = constrain(contrast, 100, 150);
  //Constrains the values between 100 and 150
  
  analogWrite(contrastPin, contrast); // sets LCD contrast.
  
  Serial.println(contrast); // just to print the output for testing

  lcd.setCursor(0,1);//set cursor position, column 0 and bottom row

  lcd.print(millis()/1000); // print second count

  lcd.setCursor(13,1); // sets cursor position to 13th column
  // and the bottom row.
  
  lcd.print(contrast); // print contrast reading at set position.
  
}