/*-------------------------------------------------------
DISTINCTION TASK QUESTION2
STUDENT ID:103495874
SARAH SALEEM
-----------------------------------------------------------
*/
//left to right 13 - 6 column connetion
//top to bottom 22 - 29 row connection
#include"Text.h"
#include <EEPROM.h>
int index_words[31];//stores the indexes to which the alphabets of the word corresponds to in the 3-d array defined in the library "Text.h"
int length_word;//will store the lenth of the string that needs to be printed
//int library_display
int scroll_speed = 1900; //controls the speed of scrolling
String Word;//word that needs to be displayed
int button = 18;
volatile unsigned int start, stop; //to store the millis() values for the instant when the button was pushed and when it was released
volatile float time; //to store the time for which the button was pressed
volatile int lastState = HIGH;
volatile bool time_flag;
bool rightToLeft, LeftToRight;
void setup() {
Serial.begin(9600);
//if anything has been previously written in the memory the 0 of eeprom will be 9
if (EEPROM.read(0) == 9) {
//will go to the function to read the data from the eeprom
read_fromEEPROM();
}
else { //if nothing has been written the code will be prompted to the read_and_store function
read_and_store();
}
for (int i = 6; i < 14; i++) { //setting pin modes for left to right connection
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
for (int i = 22; i < 30; i++) { //setting pin mode for top to bottom connection
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), timer, CHANGE);
//Serial.println("start");
Serial.println(">Push the button for more than 3 seconds to make the text scroll from Right to Left");
Serial.println(">Push the button for less than 3 seconds to make the text scroll from Left to Right");
//delay(1000);
}
void loop() {
//using time_flag to make sure that for one button press option_select function is triggered just once
if (time_flag == 1) {
option_select(time);
}
time_flag = 0;
if (rightToLeft == 1)
{
Scroll_right_to_left();
}
else if (LeftToRight == 1)
{
Scroll_left_to_right();
}
}
void timer() { //this function finds out the time for which the buton has been pressed
int value = digitalRead(button);
if (lastState != value) {
lastState = value;
if (value == LOW) {
start = millis();
Serial.println("pressed");
}
else if (value == HIGH) {
Serial.println("released");
stop = millis();
time = (stop - start) / 1000.f;
Serial.println("time");
Serial.println(time);
time_flag = 1;
}
}
}
void option_select(int time) {
if (time >= 3) {
//if the time for which the button was pressed is more than 3 seconds then right to left scrolling will be enabled
rightToLeft = 1;
LeftToRight = 0;
}
else if (time < 3) {
//if the time for which the button was pressed is less than 3 seconds then left to right scrolling will be enabled
LeftToRight = 1;
rightToLeft = 0;
}
}
//this function will read the text from the serial and store it in eeprom
void read_and_store() {
//Serial.begin(9600);
Serial.flush();
Serial.println("Enter the text that you want to display:");
while (Serial.available() == 0) {} //waits for data available
Word = Serial.readString();
Serial.print("the word"); Serial.println(Word);
length_word = Word.length() - 1; //length of the word that needs to be displayed
// one is being subtracted because .readString adds an extra space at the end which is identified as a character
Serial.println("length of word");
Serial.println(length_word);
index_words[0] = length_word; //storing the length in the zeroth index of this array
Serial.print("index zero: ");
Serial.println(index_words[0]);
for (int a = 1; a <= length_word; a++) { //storing the indexes to which the alphabets of the word corresponds to in the 3-d array defined in the library "Text.h"
index_words[a] = alpha_toindex(Word.charAt(a - 1));
Serial.println(index_words[a]);
}
//Now writing to eeprom
EEPROM.write(0, 9); //writing 9 to the zero of eeprom, so that later on we can verify that something was written to the memory
EEPROM.write(1, length_word);
for (int a = 1; a <= length_word; a++) { //writing to eeprom the elements of the array index words
EEPROM.write(a + 1, index_words[a]);
//Serial.println(EEPROM.read(a+1));
}
}
//This function will read from the eeprom
void read_fromEEPROM() {
length_word = EEPROM.read(1);
for (int a = 0; a <= length_word; a++) {
index_words[a] = EEPROM.read(a + 1);
Serial.println(index_words[a]);
}
}
void Scroll_left_to_right() {
int e;
int ii;
for (int i = length_word; i > 0; i--) {
ii = index_words[i];
if (i == length_word) { //loops inside this if will create an effect of scrolling in of the last alphabet from left to right
for (int f = 7; f >= 1; f--) {
//when f=7 led matrix displays the last column of the last alphabet of the string in the first column of the matrix
//when f=6 led matrix displays the last 2 columns of the last alphabet of the string in the first and second column of matrix and so on
//when f=5 led matrix displays the last 3 columns of the last alphabet of the string
//when f=4 led matrix displays the last 4 columns of the last alphabet of the string
//when f=3 led matrix displays the last 5 columns of the last alphabet of the string
//when f=2 led matrix displays the last 6 columns of the last alphabet of the string
//when f=1 led matrix displays the last 7 columns of the last alphabet of the string
for (int time = 0; time < scroll_speed; time++) {
for (int j = 7; j >= f; j--) {
digitalWrite(22, library_display[ii][0][j]) ;
digitalWrite(23, library_display[ii][1][j]);
digitalWrite(24, library_display[ii][2][j]);
digitalWrite(25, library_display[ii][3][j]);
digitalWrite(26, library_display[ii][4][j]);
digitalWrite(27, library_display[ii][5][j]);
digitalWrite(28, library_display[ii][6][j]);
/*
for example
for f=7 j=7 we want column number 1 of the led matrix to be On
which is pin number 6 so (6+j-f)=(6+7-7)=6
*/
digitalWrite((6 + j - f), LOW);
digitalWrite((6 + j - f), HIGH);
}
}
}
}
for (int time = 0; time < scroll_speed; time++) {
for (int j = 7; j >= 0; j--) { //displays the alphabet in full form
digitalWrite(22, library_display[ii][0][j]);
digitalWrite(23, library_display[ii][1][j]);
digitalWrite(24, library_display[ii][2][j]);
digitalWrite(25, library_display[ii][3][j]);
digitalWrite(26, library_display[ii][4][j]);
digitalWrite(27, library_display[ii][5][j]);
digitalWrite(28, library_display[ii][6][j]);
digitalWrite((6 + j), LOW);
digitalWrite((6 + j), HIGH);
}
}
if (i - 1 >= 1) { //this will check if there is an alphabet in the user entered string before the current one
for (int f = 1; f <= 7; f++) {
//the for loops below this will create a scrolling effect by changing the columns of the alphabets that are being displayed
for (int time = 0; time < scroll_speed; time++) { //displays first column of of current alphabet and last 7 column of next alphabet
for (int j = 7; j >= 0; j--) {
e = j - f;
//when f=1 then led matrix displays last column of the alphabet that needs to be printed next and first 7 column of current alphabet
//when f=2 then led matrix displays last 2 column of the alphabet that needs to be printed next and first 6 columns of current alphabet
//when f=3 then led matrix displays last 3 column of the alphabet that needs to be printed next and first 5 columns of current alphabet
//when f=4 then led matrix displays last 4 column of the alphabet that needs to be printed next and first 4 columns of current alphabet
//when f=5 then led matrix displays last 5 column of the alphabet that needs to be printed next and first 3 columns of current alphabet
//when f=6 then led matrix displays last 6 column of the alphabet that needs to be printed next and first 2 columns of current alphabet
//when f=7 then led matrix displays last 7 column of the alphabet that needs to be printed next and first 1 column of current alphabet
if (e == -1) {
e = 7;
ii = index_words[i - 1];
}
else if (e == -2) {
e = 6;
ii = index_words[i - 1];
}
else if (e == -3) {
e = 5;
ii = index_words[i - 1];
}
else if (e == -4) {
e = 4;
ii = index_words[i - 1];
}
else if (e == -5) {
e = 3;
ii = index_words[i - 1];
}
else if (e == -6) {
e = 2;
ii = index_words[i - 1];
}
else if (e == -7) {
e = 1;
ii = index_words[i - 1];
}
digitalWrite(22, library_display[ii][0][e]) ;
digitalWrite(23, library_display[ii][1][e]);
digitalWrite(24, library_display[ii][2][e]);
digitalWrite(25, library_display[ii][3][e]);
digitalWrite(26, library_display[ii][4][e]);
digitalWrite(27, library_display[ii][5][e]);
digitalWrite(28, library_display[ii][6][e]);
digitalWrite((6 + j), LOW);
digitalWrite((6 + j), HIGH);
}
ii = index_words[i];
}
}
}
else { //if the current alphabet is the first alphabet of the string or the last alphabet that needs to be printed
for (int f = 6; f >= 0; f--) {
//when f=6 column 1 till column 7 of the last alphabet will be printed
//when f=5 column 1 till column 6 of the last alphabet will be printed
//when f=4 column 1 till column 5 of the last alphabet will be printed
//when f=3 column 1 till column 4 of the last alphabet will be printed
//when f=2 column 1 till column 3 of the last alphabet will be printed
//when f=1 column 1 till column 2 of the last alphabet will be printed
//when f=0 the first column of last alphabet will be printed
for (int time = 0; time < scroll_speed; time++) {
for (int j = f; j >= 0; j--) {
digitalWrite(22, library_display[ii][0][j]) ;
digitalWrite(23, library_display[ii][1][j]);
digitalWrite(24, library_display[ii][2][j]);
digitalWrite(25, library_display[ii][3][j]);
digitalWrite(26, library_display[ii][4][j]);
digitalWrite(27, library_display[ii][5][j]);
digitalWrite(28, library_display[ii][6][j]);
/* for eample for f=6 we want j=6(the 7th column) to be printed
on the 8th column of the led matrix i.e pin13(6+j+7-f)=(6+6+7-6)=13
Similar pattern is followed for others*/
digitalWrite((6 + j + 7 - f), LOW);
digitalWrite((6 + j + 7 - f), HIGH);
}
}
}
}//of else
}//of the for loop that changes alphabet
}//of function Scroll left_to_right
void Scroll_right_to_left() {
int e;
int ii;
for (int i = 1; i <= length_word; i++) {
ii = index_words[i];
if (i == 1) { //loops inside this if will create an effect of scrolling in of the first alphabet from right to left
for (int f = 1; f <= 7; f++) {
//when f=1 first column of the first alphabet will be displayed in the right most column of led
//when f=2 first 2 column of the first alphabet will be displayed in the right most column of led
//when f=3 first 3 column of the first alphabet will be displayed in the right most column of led
//when f=4 first 4 column of the first alphabet will be displayed in the right most column of led
//when f=5 first 5 column of the first alphabet will be displayed in the right most column of led
//when f=6 first 6 column of the first alphabet will be displayed in the right most column of led
//when f=7 first 7 column of the first alphabet will be displayed in the right most column of led
for (int time = 0; time < scroll_speed; time++) {
for (int j = 0; j < f; j++) {
digitalWrite(22, library_display[ii][0][j]) ;
digitalWrite(23, library_display[ii][1][j]);
digitalWrite(24, library_display[ii][2][j]);
digitalWrite(25, library_display[ii][3][j]);
digitalWrite(26, library_display[ii][4][j]);
digitalWrite(27, library_display[ii][5][j]);
digitalWrite(28, library_display[ii][6][j]);
/*For example for f=5 we want j=0 to be printed on
the column number 4 i.e.pin9
so (6+j+8-f)=(6+0+8-5)=9
*/
digitalWrite((6 + j + 8 - f), LOW);
digitalWrite((6 + j + 8 - f), HIGH);
}
}
}
}
for (int time = 0; time < scroll_speed; time++) {
for (int j = 0; j < 8; j++) { //displays the alphabet in full form
digitalWrite(22, library_display[ii][0][j]) ;
digitalWrite(23, library_display[ii][1][j]);
digitalWrite(24, library_display[ii][2][j]);
digitalWrite(25, library_display[ii][3][j]);
digitalWrite(26, library_display[ii][4][j]);
digitalWrite(27, library_display[ii][5][j]);
digitalWrite(28, library_display[ii][6][j]);
digitalWrite((6 + j), LOW);
digitalWrite((6 + j), HIGH);
}
}
if (i + 1 <= length_word) { //this will check if there is an alphabet in the user entered string after the current one
//the for loops below this will create a scrolling effect by changing the columns of the alphabets that are being displayed
for (int f = 1; f < 8; f++) {
for (int time = 0; time < scroll_speed; time++) {
for (int j = 0; j < 8; j++) {
e = j + f;
// when f=1 then led matrix displays last 7 columns of of current alphabet and 1st column of next alphabet
//when f=2 the led matrix displays last 6 columns of of current alphabet and 1st and 2nd column of next alphabet
//when f=3 then led matrix displays last 5 columns of of current alphabet and first 3 column of next alphabet
//when f=4 led matrix displays last 4 columns of of current alphabet and first 4 column of next alphabet
//when f=5 led matrix displays last 3 columns of of current alphabet and first 5 column of next alphabet
//when f=6 displays last 2 columns of of current alphabet and first 6 column of next alphabet
//when f=7 then led matrix displays last column of of current alphabet and first 7 column of next alphabet
if (e == 8) {
e = 0;
ii = index_words[i + 1];
}
else if (e == 9) {
e = 1;
ii = index_words[i + 1];
}
else if (e == 10) {
e = 2;
ii = index_words[i + 1];
}
else if (e == 11) {
e = 3;
ii = index_words[i + 1];
}
else if (e == 12) {
e = 4;
ii = index_words[i + 1];
}
else if (e == 13) {
e = 5;
ii = index_words[i + 1];
}
else if (e == 14) {
e = 6;
ii = index_words[i + 1];
}
digitalWrite(22, library_display[ii][0][e]) ;
digitalWrite(23, library_display[ii][1][e]);
digitalWrite(24, library_display[ii][2][e]);
digitalWrite(25, library_display[ii][3][e]);
digitalWrite(26, library_display[ii][4][e]);
digitalWrite(27, library_display[ii][5][e]);
digitalWrite(28, library_display[ii][6][e]);
digitalWrite((6 + j), LOW);
digitalWrite((6 + j), HIGH);
}
ii = index_words[i];
}
}
}
else { //if the current alphabet is the last one then it will scroll out
for (int f = 6; f >= 0; f--) {
//when f=6 the last alphabet will be printed from the 2nd column
//when f=5 the last alphabet will be printed from the 3rd column
//when f=4 the last alphabet will be printed from the 4th column
//when f=3 the last alphabet will be printed from the 5th column
//when f=2 the last alphabet will be printed from the 6th column
//when f=1 the last alphabet will be printed from the 7th column
//when f=0 the last column of the last alphabet will be printed
for (int time = 0; time < scroll_speed; time++) {
for (int j = 0; j < f; j++) {
/*for example when f=6 then for j=0 [j+7-f]=1 and when j=f=6 [j+7-f]=7
so this means the alphabet will start printing from its 2nd column(index 1)
and will print till its last column(index 7)
similar pattern will be followed as the value of f changes */
digitalWrite(22, library_display[ii][0][j + 7 - f]) ;
digitalWrite(23, library_display[ii][1][j + 7 - f]);
digitalWrite(24, library_display[ii][2][j + 7 - f]);
digitalWrite(25, library_display[ii][3][j + 7 - f]);
digitalWrite(26, library_display[ii][4][j + 7 - f]);
digitalWrite(27, library_display[ii][5][j + 7 - f]);
digitalWrite(28, library_display[ii][6][j + 7 - f]);
digitalWrite((6 + j), LOW);
digitalWrite((6 + j), HIGH);
}
}
}
}//of else
}//of the for loop that changes alphabet
}