//W7 C Q2 - printing user defined text to LED matrix (no decoders or libraries)
//103069716 Aidan Burkitt
//Completed on 25/9/2022 SUN W7/W8
//The device prints a alphanumeric character and changes its position >
//to scroll from right to left. The character formatting is stored in >
//an external .cpp document called character_dictionary.cpp which is >
//referenced at start up. A desired set of characters is initialised >
//globally as a integer array under the name printText[]. The integers>
//relate to index within the character_dictionary.cpp main boolean >
//array. Currently my name and student number is printed to the LED >
//matrix. Two displays where included for testing and debugging as the>
//standard LED matrix was harder to read than the LED bar setup. It >
//should be noted that in a physical setting the LED bar matrix must >
//have resistors, it cannot take raw current from the Arduino. (Wokwi >
//doesn't simulate analogue electronics well.)
//UPDATE FOR CREDIT NOTES:
//The Serial is now used to define the desired text display on the LED>
//matrix. The Serial values are read and then processed by >
//readValues() and processValues() respectively. processValues() saves>
//to EEPROM. Everything else is same except for printText[] been >
//replaced with EEPROM references.
//for defined characters
#include "character_dictionary.hpp"
//To read and write from EEPROM
#include <EEPROM.h>
//From top to bottom
#define ROW_1 37
#define ROW_2 35
#define ROW_3 33
#define ROW_4 31
#define ROW_5 29
#define ROW_6 27
#define ROW_7 25
#define ROW_8 23
//from left to right
#define COL_1 13
#define COL_2 12
#define COL_3 11
#define COL_4 10
#define COL_5 9
#define COL_6 8
#define COL_7 7
#define COL_8 6
const int rows[] = {
ROW_1, ROW_2, ROW_3, ROW_4, ROW_5, ROW_6, ROW_7, ROW_8
};
const int cols[] = {
COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8
};
//functional preinitalisation
void printValues();
String readValues();
void processValues(String);
int TEXT_LENGTH;
void setup()
{
//Setup pin in and out
for (int i = 0; i < 8; i++)
{
pinMode(rows[i], OUTPUT);
pinMode(cols[i], OUTPUT);
}
Serial.begin(9600);
Serial.println("Enter Desired scrolling Text: ");
Serial.println("Note: text must be alphaNumeric!");
delay(1000);
String userInput = readValues();
processValues(userInput); //process values and save to EEPROM
}
//array representing what is displayed on screen - constantly bing changed
bool LED_temp[8][8] = {0};
//From Pass Plus - code referencing printText has been replaced with EEPROM.read() and write()
//dictionary reference - desired text
//int printText[] = {0, 1, 9, 4, 1, 14, 0, /*1st Name*/
// 2, 21, 18, 11, 9, 20, 20, 0, /*2nd Name*/
// 28, 27, 30, 27, 33, 36, 34, 28, 33, 0 /*Student number*/
// };
//Aidan Burkitt 103069716
//null - 0, letters 1 - 26, numbers 27 - 36
//length of text is 23
void loop()
{
for (int letter = 0; letter < TEXT_LENGTH; letter++)
{
for (int col = 0; col < 8; col++)
{
//changeValues(col, LED_characters[printText[letter]], LED_characters[printText[letter + 1]]);
changeValues(col, LED_characters[EEPROM.read(letter)], LED_characters[EEPROM.read(letter + 1)]);
printValues();
}
}
}
void changeValues(int passes, bool LED_printOLD[8][8], bool LED_printNEW[8][8]) {
for (int col = 0; col < 8; col++)
{
int offset = 0;
for (int row = 0; row < 8; row++)
{
//passes equal how many times has the character array been "shifted"
//passes equal how many new bits are now in array
//(8 - passes) equal how many old bits are still in array
int oldBits = 7 - passes; //reference to index (index starts at 0)
if (col <= oldBits) {
LED_temp[row][col] = LED_printOLD[row][col + passes];
} else if (col > oldBits) {
//when indexing col is outside of oldLetter index, change to new letter
//Found relationship through passes=-1*col+8. And let when equal to 0
LED_temp[row][col] = LED_printNEW[row][passes + col - 8];
}
}
//use to modify scroll speed
delay(10);
}
}
void printValues() {
//Displays all values from LED_temp[][] array
for (int row_j = 0; row_j < 8; row_j++)
{
for (int col_i = 0; col_i < 8; col_i++)
{
digitalWrite(cols[col_i], LED_temp[row_j][col_i]);
}
//setting and resetting rows. Necessary.
digitalWrite(rows[row_j], LOW);
delay(1);
digitalWrite(rows[row_j], HIGH);
}
}
//Used to troubleShot LED display - displays current LED matrix states
void troubleShot() {
for (int col = 0; col < 8; col++)
{
for (int row = 0; row < 8; row++)
{
Serial.print(LED_temp[col][row]); Serial.print(", ");
}
Serial.println();
}
}
//requests USER to define text for the LED matrix. Outputs the user's request as a string
String readValues()
{
while (Serial.available() == 0)
{
//Waits until Serial is available for reading
//Serial.print("DEBUG: waiting for Serial to be available");
}
//stores input text to a string
String inputText = Serial.readString();
Serial.print("Received text is: "); Serial.println(inputText);
return(inputText);
}
//processes user's text request. Converts to an array of indices to be used for the dictionary
void processValues(String inputText)
{
int processedText[inputText.length()+1] = {0};
TEXT_LENGTH = inputText.length()+1;
//additional first element is always 0, allows for animation
//endline character is also used
for (int p = 0; p < inputText.length(); p++)
{
//DEBUG: shows inputed text
//Serial.print(inputText[p]); Serial.print(",");
//switch converts ASCII corresponding dictionary index
switch (inputText[p]) {
case 32: //Space and endline, required to provide seemless transistion
case '\n':
processedText[p+1] = 0; //character_dictionary index for space
break;
case 48 ... 57: // for all numeric conditions
processedText[p+1] = inputText[p] - 48 + 27;
//48 is outset in ASCII code, plus 27 for 0 starting position
break;
case 65 ... 90: // for capital letter conditions
processedText[p+1] = inputText[p] - 65 + 1;
//65 is outset in ASCII code, plus 1 for A starting position
break;
case 97 ... 122: // for lowercase letter conditions
processedText[p+1] = inputText[p] - 97 + 1;
//97 is outset in ASCII code, plus 1 for A starting position
break;
default: //When invalid characters are inputted
Serial.println("INVALID character has been inputted.");
Serial.println("Please restart and re-enter desired scrolling string.");
Serial.println("Only alphanumeric (also space) characters allowed.");
break;
}
}
processedText[inputText.length()+1] = 0; //adds an extra null character on end
for (int p = 0; p < inputText.length()+1; p++){
EEPROM.write(p, processedText[p]); //write to EEPROM;
//DEBUG: check processed values
//Serial.print(processedText[p]); Serial.print(", ");
}
return;
}