/*
* Spuzzum's ADC buttons
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/
//#include "Time.h"
#include "Wire.h"
#include "RTClib.h" // #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include "LiquidCrystal.h"
RTC_DS1307 RTC;
LiquidCrystal lcd(0);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 200) return btnUP;
if (adc_key_in < 400) return btnDOWN;
if (adc_key_in < 600) return btnLEFT;
if (adc_key_in < 800) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup() {
Serial.begin(57600);
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__));
/* if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
*/
// set up the LCD's number of rows and columns:
lcd.begin(20, 4);
lcd.clear(); // Clear Display
lcd.setBacklight(HIGH);
lcd.setCursor(5,0); // Place cursor row 6, 1st line (counting from 0)
lcd.print("Setup");
lcd.setCursor(7,1); // Place cursor row 8, 2nd line (counting from 0)
lcd.print("ok");
delay(2000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("ADC Key Test");
delay(100);
lcd.clear();
}
void loop()
{
digitalClockDisplay();
delay(1000);
//lcd.clear(); // Clear Display
lcd.setCursor(4,0);
lcd.print("ADC Key Test");
{
//lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
//lcd.print(millis()/1000); // display seconds elapsed since power-up
lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.setCursor(5,2);
lcd.print("RIGHT ");
delay(1000);
break;
}
case btnLEFT:
{
lcd.setCursor(5,2);
lcd.print("LEFT ");
delay(1000);
break;
}
case btnUP:
{
lcd.setCursor(5,2);
lcd.print("UP ");
delay(1000);
break;
}
case btnDOWN:
{
lcd.setCursor(5,2);
lcd.print("DOWN ");
delay(1000);
break;
}
case btnSELECT:
{
lcd.setCursor(5,2);
lcd.print("SELECT");
delay(1000);
break;
}
case btnNONE:
{
lcd.setCursor(5,2);
lcd.print("NONE ");
break;
}
}
}
}
void digitalClockDisplay(){
DateTime now = RTC.now();
// digital clock display of the time
lcd.setCursor(0,3);
//lcd.setCursor(2,0);
lcd.print(now.month());
lcd.print("/");
lcd.print(now.day());
lcd.print("/");
lcd.print(now.year());
lcd.print(" ");
lcd.print(now.hour());
printDigits(now.minute());
printDigits(now.second());
//lcd.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
lcd.print(":");
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}