#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
#include <SD.h>
#define BUTTON_ADC_PIN A0 // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
// ADC readings expected for the 5 buttons on the ADC input
#define RIGHT_10BIT_ADC 0 // right
#define UP_10BIT_ADC 145 // up
#define DOWN_10BIT_ADC 329 // down
#define LEFT_10BIT_ADC 505 // left
#define SELECT_10BIT_ADC 741 // right
#define BUTTONHYSTERESIS 10 // hysteresis for valid button sensing window
//return values for ReadButtons()
#define BUTTON_NONE 0 //
#define BUTTON_RIGHT 1 //
#define BUTTON_UP 2 //
#define BUTTON_DOWN 3 //
#define BUTTON_LEFT 4 //
#define BUTTON_SELECT 5 //
const int chipSelect = 2;
int linecount = 0;
byte buttonJustPressed = false; //this will be true after a ReadButtons() call if triggered
byte buttonJustReleased = false; //this will be true after a ReadButtons() call if triggered
byte buttonWas = BUTTON_NONE; //used by ReadButtons() for detection of button events
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
int count = 99;
int randNumber = random(count);
char buffer[30];
unsigned int yorn = 0; // 0 no 1 yes
int user = 0; //0 select 1 nic 2 laura
int nic[99]; // 0 blank 1 yes 2 no
int laura[99]; //0 blank 1 yes 2 no
File dataFile = SD.open("names.txt");
File indexFile = SD.open("index.txt");
void setup() {
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
File dataFile = SD.open("names.txt");
File indexFile = SD.open("index.txt");
if (dataFile) {
while (dataFile.available()) {
char tmp = dataFile.read();
if (tmp == '\n') {
if (indexFile) {
linecount++;
indexFile.write(linecount);
indexFile.print(",");
indexFile.write(dataFile.position());
}
else
{
Serial.println("Could not open index.txt");
}
}
}
dataFile.close();
indexFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
randomSeed(analogRead(3));
randNumber = random(count);
pinMode( BUTTON_ADC_PIN, INPUT ); //ensure A0 is an input
digitalWrite( BUTTON_ADC_PIN, LOW ); //ensure pullup is off on A0
//lcd backlight control
digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
pinMode( LCD_BACKLIGHT_PIN, OUTPUT ); //D3 is an output
lcd.begin(16, 2);
lcd.print(user);
Serial.begin(9600);
}
void loop() {
if (!dataFile) File dataFile = SD.open("names.txt");
int i = 0;
randomSeed(analogRead(3));
int randNumber = random(linecount);
if (dataFile) {
while (dataFile.available()) {
char tmp = dataFile.read();
if (i == randNumber)
{
Serial.write(tmp);
}
if (tmp == '\n') i++;
}
//dataFile.close();
}
else {
Serial.println("WUT!");
}
byte button;
button = ReadButtons();
if( buttonJustPressed || buttonJustReleased )
{
//lcd.setCursor( 4, 1 );
//lcd.print( " " );
}
switch( button )
{
case BUTTON_NONE:
{
int length = strlen(buffer);
lcd.setCursor( 8-(length/2), 1 );
//lcd.print(" ");
lcd.print( buffer );
lcd.setCursor(0,0);
if (user == 0) lcd.print("Select User ");
else if (user == 1) {
lcd.print("Nic Says: ");
if (nic[randNumber] == 1) lcd.print("YES");
else if (nic[randNumber] == 2) lcd.print(" NO");
}
else if (user == 2) {
lcd.print("Laura Says: ");
if (laura[randNumber] == 1) lcd.print("YES");
else if (laura[randNumber] == 2) lcd.print(" NO");
}
break;
}
case BUTTON_RIGHT:
{
lcd.setCursor( 13, 0 );
if (user == 1) nic[randNumber] = 1;
else if (user == 2) laura[randNumber] = 1;
break;
}
case BUTTON_UP:
{
user = 1;
lcd.setCursor(0,0);
lcd.print(" ");
break;
}
case BUTTON_DOWN:
{
user = 2;
lcd.setCursor(0,0);
lcd.print(" ");
break;
}
case BUTTON_LEFT:
{
lcd.setCursor( 13, 0 );
if (user == 1) nic[randNumber] = 2;
else if (user == 2) laura[randNumber] = 2;
break;
}
case BUTTON_SELECT:
{
randomSeed(analogRead(3));
randNumber = random(count);
strcpy_P(buffer, (char*)pgm_read_word(&(string_table[randNumber])));
lcd.setCursor( 0, 1 );
lcd.print( " NEW NAME " );
lcd.setCursor( 13, 0 );
lcd.print(" ");
delay(500);
lcd.setCursor( 0, 1 );
lcd.print( " " );
break;
}
default:
{
break;
}
}
//clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
if( buttonJustPressed )
buttonJustPressed = false;
if( buttonJustReleased )
buttonJustReleased = false;
}
/*--------------------------------------------------------------------------------------
ReadButtons()
Detect the button pressed and return the value
Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
--------------------------------------------------------------------------------------*/
byte ReadButtons()
{
unsigned int buttonVoltage;
byte button = BUTTON_NONE; // return no button pressed if the below checks don't write to btn
//read the button ADC pin voltage
buttonVoltage = analogRead( BUTTON_ADC_PIN );
//sense if the voltage falls within valid voltage windows
if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_RIGHT;
}
else if( buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_UP;
}
else if( buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_DOWN;
}
else if( buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_LEFT;
}
else if( buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
&& buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
{
button = BUTTON_SELECT;
}
//handle button flags for just pressed and just released events
if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
{
//the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press eve
//it's the duty of the receiver to clear these flags if it wants to detect a new button change event
buttonJustPressed = true;
buttonJustReleased = false;
}
if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
{
buttonJustPressed = false;
buttonJustReleased = true;
}
//save the latest button value, for change event detection next time round
buttonWas = button;
return( button );
}