#include <SD.h>
#include <ezButton.h>
#include <LiquidCrystal_I2C.h>
#include "lcd_custom_character.h"
File file;
int book_num = 0;
int book_state = 0;
ezButton next(2);
ezButton prev(3);
ezButton select_book(4);
ezButton open_book(5);
LiquidCrystal_I2C lcd(0x27,20,4);
int prev_book_num=-1;
int page_num=0,prev_page_num=-1;
String display_text="";
void check_book(){
if(prev_book_num != book_num){
prev_book_num= book_num;
lcd.clear();
if(book_num){
lcd_print(0,0,"THINK AND GROW RICH");
Serial.println("THINK AND GROW RICH");
}
else {
lcd_print(0,0,"HARRY PORTER");
Serial.println("HARRY PORTER");
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if (!SD.begin())Serial.println("SD Card is not detected");
else Serial.println("SD card detected");
/*file = SD.open("harry.txt",FILE_READ);
Serial.print("Total chracters :");
Serial.println(file.size());
if (file){
Serial.println("Harry porter: ");
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);
}
void display_char(int x,int y,int id){
lcd.setCursor(x,y);
lcd.write(id);
}
void animate(){
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++){
display_char(11,3,1);
display_char(9,3,3);
display_char(7,3,0);
if (i%2==0){
display_char(8,3,2);
display_char(10,3,2);
delay(1100);
}
else{
display_char(8,3,4);
display_char(10,3,4);
delay(100);
}
}
lcd.clear();
}
void welcome(){
lcd_print(1,0,"WELCOME");
delay(1000);
lcd_print(8,1,"TO");
delay(1000);
lcd_print(10,2,"DIGI BOOK");
delay(1000);
}
void loop() {
book_num= constrain(book_num,0,1);
// put your main code here, to run repeatedly:
next.loop();
prev.loop();
select_book.loop();
open_book.loop();
if (book_state==0){
check_book();
}
if (next.isPressed()){
if(book_state)page_num++;
else book_num++;
}
else if(prev.isPressed()){
if(book_state)page_num--;
else book_num--;
}
else if (select_book.isPressed()){
book_state=0;
page_num=0;
prev_book_num=-1;
prev_page_num=-1;
check_book();
}
else if (open_book.isPressed()){
book_state=1;
read_book();
}
book_num=constrain(book_num,0,1);
if(page_num<0)page_num=0;
check_book();
read_book();
delay(10);
}
void read_book(){
if(book_state){
if(book_num){
file= SD.open("think.txt",FILE_READ);
Serial.println("total characters: ");
Serial.println(file.size());
}
else {
file= SD.open("harry.txt",FILE_READ);
Serial.println("total characters: ");
Serial.println(file.size());
}
int total_pages=file.size()/80;
page_num= constrain(page_num,0,total_pages);
int pointer= page_num*80;
display_text= "";
if (file){
if(prev_page_num !=page_num){
prev_page_num=page_num;
display_text="";
file.seek(pointer);
while (display_text.length()!= 80){
char data= file.read();
display_text.concat(data);
}
page_print();
}
}
}
file.close();
}
void page_print(){
for (int i =0;i<4;i++){
lcd_print(0,i,display_text.substring(i*20,(i+1)*20));
}
}