#include <SD.h>
#include <ezButton.h>
#include <LiquidCrystal_I2C.h>
#include "lcd_custom_character.h"
File file;
ezButton next(2);
ezButton prev(3);
ezButton select_book(4);
ezButton open_book(5);
// keeps track of which book is to be read
int book_num = 0;
//checks if the book should be opened or not.
int book_state = 0;
// flag to avoid reptation of the currently selected book
int prev_book = -1;
LiquidCrystal_I2C lcd(0x27 , 20 , 4);
void setup()
{
Serial.begin(9600);
if (!SD.begin())
Serial.println("SD card not detected");
else
Serial.println("SD card detected");
// file = SD.open("harry.txt" , FILE_READ);
// Serial.print("Total Characters: ");
// Serial.println(file.size());
// if(file)
// {
// Serial.println("Harry Potter");
// delay(2000);
// while (file.available())
// {
// char data = file.read();
// Serial.print(data);
// }
// Serial.println();
// file.close();
// }
// Serial.println("File read successfully");
next.setDebounceTime(50);
prev.setDebounceTime(50);
select_book.setDebounceTime(50);
open_book.setDebounceTime(50);
lcd.init();
lcd.backlight();
lcd.clear();
welcome();
animate();
delay(1000);
}
void lcd_print(int x , int y , String message)
{
lcd.setCursor(x,y);
lcd.print(message);
}
//created to draw customised charcter
void display_char(int x, int y, int id)
{
lcd.setCursor(x, y);
//draw customised charcter with it's id
lcd.write(id);
}
void animate()
{
// createChar() method to assign an id to the customized characters
lcd.createChar(0, left_stick);
lcd.createChar(1, right_stick);
lcd.createChar(2, open_eye);
lcd.createChar(3, middle_stick);
lcd.createChar(4, closed_eye);
for (int i = 0; i < 5; i++)
{
// col, row, id (left stick)
display_char(11, 3, 1); // right stick
display_char(9, 3, 3); // middle stick
display_char(7, 3, 0); // left stick
if (i % 2 == 0)
{
display_char(8, 3, 2); //open eye
display_char(10, 3, 2); //open eye
delay(1100);
}
else
{
display_char(8, 3, 4); //closed eye
display_char(10, 3, 4); //closed eye
delay(100);
}
}
lcd.clear();
}
void welcome()
{
lcd_print(2,0, "Welcome");
delay(1000);
lcd_print(8,1, "To");
delay(1000);
lcd_print(10,2, "Digi Book");
delay(1000);
}
void loop()
{
next.loop();
prev.loop();
select_book.loop();
open_book.loop();
if (next.isPressed())
book_num++;
else if (prev.isPressed())
book_num--;
else if (select_book.isPressed())
{
// changes values to restart after opening a book
book_state = 0;
prev_book = -1;
}
else if(open_book.isPressed())
{
// changes value of the variable to enable book opening in the read_book()
book_state = 1;
}
// makes sure doesn't go past 0 or 1 as only 2 books present.
book_num = constrain(book_num,0,1);
//checks which book is selected
check_book();
// reads book content
read_book();
}
void check_book()
{
// to avoid repeative printing of book name
if (prev_book != book_num)
{
lcd.clear();
prev_book = book_num;
if (book_num)
lcd_print(0,0,"Think and Grow Rich!");
else
lcd_print(0,0,"Harry Potter!");
}
}
void read_book()
{
if(book_state)
{
if(book_num)
{
file= SD.open("think.txt",FILE_READ);
}
else
{
file= SD.open("harry.txt",FILE_READ);
}
String display_text="";
if(file)
{
while(file.available())
{
char data=file.read();
Serial.print(data);
}
}
}
file.close();
}