#include <EEPROM.h>
// define the pin of LED Matrix
#define ROW_1 2
#define ROW_2 3
#define ROW_3 4
#define ROW_4 5
#define ROW_5 6
#define ROW_6 7
#define ROW_7 8
#define ROW_8 9
#define COL_1 10
#define COL_2 11
#define COL_3 12
#define COL_4 13
#define COL_5 A0
#define COL_6 A1
#define COL_7 A2
#define COL_8 A3
// initialize for input string and index char
int x_index = 0;
char data_read;
String text_0;
int timeCount = 0;
int n;
// get all the pin to array
const byte rows[] = {
ROW_1, ROW_2, ROW_3, ROW_4, ROW_5, ROW_6, ROW_7, ROW_8
};
const byte cols[] = {
COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8
};
char value ;
int done = 0;
// intialize variable use for button
int reading;
int lastReading = LOW;
int bounceTime = 50;
int holdTime = 250;
int hold = 0;
long onTime = 0;
long oldTime = 0;
long doubleTime = 500;
long lastSwitchTime = 0;
int memorise_button = 0 ;
// byte array contain all the byte
byte A[] = {B00011000,B00100100,B01000010,B01000010,B01111110,B01000010,B01000010,B01000010};
byte B[] = {B01111100,B01000010,B01000100,B01111000,B01000100,B01000010,B01000010,B01111110};
byte C[] = {B01111110,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01111110};
byte D[] = {B01111000,B01000100,B01000010,B01000010,B01000010,B01000010,B01000100,B01111000};
byte E[] = {B01111110,B01000000,B01000000,B01111000,B01000000,B01000000,B01000000,B01111110};
byte F[] = {B01111110,B01000000,B01000000,B01111100,B01000000,B01000000,B01000000,B01000000};
byte G[] = {B01111110,B01000000,B01000000,B01000000,B01001110,B01000010,B01000010,B01111110};
byte H[] = {B01000010,B01000010,B01000010,B01111110,B01000010,B01000010,B01000010,B01000010};
byte I[] = {B00001000,B00001000,B00001000,B00001000,B00001000,B00001000,B00001000,B00001000};
byte J[] = {B01111110,B00001000,B00001000,B00001000,B00001000,B00001000,B01001000,B00110000};
byte K[] = {B01000100,B01001000,B01010000,B01100000,B01010000,B01001000,B01000100,B01000010};
byte L[] = {B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01000000,B01111110};
byte M[] = {B01000001,B01100011,B01010101,B01001001,B01000001,B01000001,B01000001,B01000001};
byte N[] = {B01000001,B01100001,B01010001,B01001001,B01000101,B01000011,B01000001,B01000001};
byte O[] = {B01111110,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01111110};
byte P[] = {B01111110,B01000010,B01000010,B01111110,B01000000,B01000000,B01000000,B01000000};
byte Q[] = {B01111110,B01000010,B01000010,B01000010,B01000010,B01001010,B01000110,B01111110};
byte R[] = {B01111110,B01000010,B01000010,B01111110,B01010000,B01001000,B01000100,B01000010};
byte S[] = {B01111110,B01000000,B01000000,B01111110,B00000010,B00000010,B00000010,B01111110};
byte T[] = {B01111111,B00001000,B00001000,B00001000,B00001000,B00001000,B00001000,B00001000};
byte U[] = {B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B01111110};
byte V[] = {B01000001,B01000001,B01000001,B01000001,B01000001,B00100010,B00010100,B00001000};
byte W[] = {B01000001,B01000001,B01000001,B01000001,B01001001,B01010101,B01100011,B01000001};
byte X[] = {B00000000,B01000001,B00100010,B00010100,B00001000,B00010100,B00100010,B01000001};
byte Y[] = {B01000001,B01000001,B00100010,B00010100,B00001000,B00001000,B00001000,B00001000};
byte Z[] = {B01111111,B00000010,B00000100,B00001000,B00010000,B00100000,B01000000,B01111111};
byte XX[] = {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};
// contain all shift byte of char
byte word_set[8];
byte Right_1[8];
byte Right_2[8];
byte Right_3[8];
byte Right_4[8];
byte Right_5[8];
byte LEFT_1[8];
byte LEFT_2[8];
byte LEFT_3[8];
byte LEFT_4[8];
byte LEFT_5[8];
void setup() {
// Set up the serial monitor
Serial.begin(9600);
Serial.println("HEllo World ");
//Set up the button
pinMode(21, INPUT);
//set up all the pin for LED MATRIX
for (byte i = 2; i <= 13; i++)
pinMode(i, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
}
void loop() {
// button function check for any click
button_check();
// check if there input , get the input of serial monitor as string
while (Serial.available()) {
delay(10);
if (Serial.available() > 0) {
text_0 = Serial.readString();
}
}
if (text_0.length() > 0) {
n = text_0.length();
// declaring character array
// save the string as char array
//add the turn off byte (XX contain all zero byte) at the last place
char char_array[n];
char_array [n+1] = "XX";
text_0.toCharArray(char_array, n + 1);
// set the eeprom
//however as print the eeprom on serial use void loop make system slow
// turn off the eeprom to improve the speed of system
// for (int i = 0 ; i < n ; i++) {
// EEPROM.write(i + 1 , char_array[i]);
// value = EEPROM.read(i + 1);
// Serial.print(i + 1);
// Serial.print("\t");
// Serial.print(value);
// Serial.println();
// }
// if hold =2 read the text set index beginning at postion 0 (RIGHT TO LEFT SCROLLING )
if (hold == 2 && done == 0){
x_index = 0 ;
}
// if hold =1 read the text set index beginning at postion n -2 (last postion -2 )(LEFT TO RIGHT WHEN SCROLLING)
else if (hold == 1 && done == 0 ){
x_index = n - 2;
}
// index the element from char array
data_read = char_array[x_index];
done = 1; // turn of the x_index in else if statement
}
// set the input letter to byte letter
set_letter ();
shift_text (word_set);// run the shift letter to achive the shifting
// if hold = 2 run the text from Right to LEFT
if (hold == 2) {
timeCount = timeCount + 1;
if(timeCount < 50) {
draw_Letter(Right_5);
}
else if (timeCount < 60 ) {
draw_Letter(Right_4);
}
else if (timeCount < 110) {
draw_Letter(Right_3);
}
else if (timeCount < 120) {
draw_Letter(Right_2);
}
else if(timeCount < 170) {
draw_Letter(Right_1);
}
else if (timeCount < 180) {
draw_Letter(word_set);
}
else if (timeCount < 230) {
draw_Letter(LEFT_1);
}
else if (timeCount < 240) {
draw_Letter(LEFT_2);
}
else if (timeCount < 260) {
draw_Letter(LEFT_3);
}
else if (timeCount < 290) {
draw_Letter(LEFT_4);
}
else if (timeCount < 300) {
draw_Letter(LEFT_5);
}
else {
timeCount = 0;
}
// IF TIME IN THIS THE RANGE OF 280 TO 282 , INCREASE THE INDEX BY 1
if (timeCount > 280 && timeCount < 282 )
{
x_index = x_index + 1 ;
}
// IF INDEX GET TO THE END OF ARRAY SET IT BACK TO BEGINNING
if (x_index > (n -2) )
{
x_index = 0 ;
}
}
// if hold = 1 run the text from LEFT to RIGHT
if (hold == 1) {
timeCount = timeCount + 1;
if(timeCount < 50) {
draw_Letter(LEFT_5);
}
else if (timeCount < 60 ) {
draw_Letter(LEFT_4);
}
else if (timeCount < 110) {
draw_Letter(LEFT_3);
}
else if (timeCount < 120) {
draw_Letter(LEFT_2);
}
else if(timeCount < 170) {
draw_Letter(LEFT_1);
}
else if (timeCount < 180) {
draw_Letter(word_set);
}
else if (timeCount < 230) {
draw_Letter(Right_1);
}
else if (timeCount < 240) {
draw_Letter(Right_2);
}
else if (timeCount < 260) {
draw_Letter(Right_3);
}
else if (timeCount < 290) {
draw_Letter(Right_4);
}
else if (timeCount < 300) {
draw_Letter(Right_5);
}
else {
timeCount = 0;
}
// IF TIME IN THIS THE RANGE OF 280 TO 282 , INCREASE THE INDEX BY 1
if (timeCount > 280 && timeCount < 282 )
{
x_index = x_index - 1 ;
}
// IF INDEX GET TO THE END OF ARRAY SET IT BACK TO BEGINNING
if (x_index < 0 )
{
x_index = n -2 ;
}
}
}
// DRAW LETTER LED MATRIX
void draw_Letter(byte word_1[]){
// Turn on each row in series
for (byte i = 0; i < 8; i++) {
setColumns(word_1[i]); // Set columns for this specific row
digitalWrite(rows[i], HIGH);
delay(2);
digitalWrite(rows[i], LOW);
}
}
// READ the byte in column
void setColumns(byte column) {
for (byte j = 0; j < 8; j++) {
digitalWrite( cols[j], !bitRead(column,j));
}
}
// shift the byte of word byte
// shift in 2 ,4 , 6,8,10
void shift_text (byte buffer3[]){
for ( byte i = 0 ; i < 8 ; i++)
{
Right_1[i] = buffer3[i] >> 2 ;
Right_2[i] = buffer3[i] >> 4 ;
Right_3[i] = buffer3[i] >> 6 ;
Right_4[i] = buffer3[i] >> 8 ;
Right_5[i] = buffer3[i] >> 10 ;
LEFT_1[i] = buffer3[i] << 2 ;
LEFT_2[i] = buffer3[i] << 4 ;
LEFT_3[i] = buffer3[i] << 6 ;
LEFT_4[i] = buffer3[i] << 8 ;
LEFT_5[i] = buffer3[i] << 10 ;
}
}
// define what letter input mean
//what word byte correspond to the input letter
void set_letter (){
if (data_read == 'A' or data_read == 'a' ){
memcpy(word_set, A, sizeof( A));
}
else if (data_read == 'B' or data_read == 'b' ){
memcpy(word_set, B , sizeof( B));
}
else if (data_read == 'C' or data_read == 'c' ){
memcpy(word_set, C, sizeof( C));
}
else if (data_read == 'D' or data_read == 'd' ){
memcpy(word_set, D , sizeof( D));
}
else if (data_read == 'E' or data_read == 'e' ){
memcpy(word_set, E, sizeof( E));
}
else if (data_read == 'F' or data_read == 'f' ){
memcpy(word_set, F, sizeof( F));
}
else if (data_read == 'G' or data_read == 'g' ){
memcpy(word_set, G , sizeof( G));
}
else if (data_read == 'H' or data_read == 'h' ){
memcpy(word_set, H , sizeof( H ));
}
else if (data_read == 'I' or data_read == 'i' ){
memcpy(word_set, I , sizeof( I ));
}
else if (data_read == 'J' or data_read == 'j' ){
memcpy(word_set, J , sizeof( J ));
}
else if (data_read == 'K' or data_read == 'k' ){
memcpy(word_set, K , sizeof( K ));
}
else if (data_read == 'L' or data_read == 'l' ){
memcpy(word_set, L , sizeof( L ));
}
else if (data_read == 'M' or data_read == 'm' ){
memcpy(word_set, M , sizeof( M ));
}
else if (data_read == 'N' or data_read == 'n' ){
memcpy(word_set, N , sizeof( N ));
}
else if (data_read == 'O' or data_read == 'o' ){
memcpy(word_set, O , sizeof( O ));
}
else if (data_read == 'P' or data_read == 'p' ){
memcpy(word_set, P , sizeof( P ));
}
else if (data_read == 'Q' or data_read == 'q' ){
memcpy(word_set, Q , sizeof( Q ));
}
else if (data_read == 'R' or data_read == 'r' ){
memcpy(word_set, R , sizeof( R ));
}
else if (data_read == 'S' or data_read == 's' ){
memcpy(word_set, S , sizeof( S ));
}
else if (data_read == 'T' or data_read == 't' ){
memcpy(word_set, T , sizeof( T ));
}
else if (data_read == 'U' or data_read == 'u' ){
memcpy(word_set, U , sizeof( U ));
}
else if (data_read == 'V' or data_read == 'v' ){
memcpy(word_set, V , sizeof( V ));
}
else if (data_read == 'W' or data_read == 'w' ){
memcpy(word_set, W , sizeof( W ));
}
else if (data_read == 'X' or data_read == 'x' ){
memcpy(word_set, X , sizeof( X ));
}
else if (data_read == 'Y' or data_read == 'y' ){
memcpy(word_set, Y , sizeof( Y ));
}
else if (data_read == 'Z' or data_read == 'z' ){
memcpy(word_set, Z , sizeof( Z ));
}
else if (data_read == "XX" or data_read == "xx" ){
memcpy(word_set, XX , sizeof( XX ));
}
}
void button_check() {
// read the click from button
reading = digitalRead(21);
//when there is a click begin time the time of button was press
if (reading == HIGH && lastReading == LOW) {
onTime = millis();
}
// if the button is double click and the interval is over 2000
// set hold to 1
if (reading == HIGH && lastReading == HIGH) {
if ( (onTime - oldTime) > 2000) {
Serial.println("Hiu");
hold = 1;
oldTime = onTime; //reset button time
done = 0; //done =0
}}
// if it only asingle click and the time is in range of 2 to 1000
// set hold to 2
//set done to 0
if (reading == LOW && lastReading == HIGH) {
if ((( onTime - oldTime) > 2) && (( onTime - oldTime) < 1000) ) {
Serial.println("H");
hold = 2;
done = 0;
oldTime = onTime; //reset the button time
}
// else just reset the button time
else {
oldTime = onTime;
}
}
lastReading = reading; //set last reading to reading
}