#include "letters.h"
#include <EEPROM.h>
//Defines column pins
#define C0 22
#define C1 23
#define C2 24
#define C3 25
#define C4 26
#define C5 27
#define C6 28
#define C7 29
//Defines row pins
#define R0 32
#define R1 33
#define R2 34
#define R3 35
#define R4 36
#define R5 37
#define R6 38
#define R7 39
String toPrint = "zach"; //Sets the default text
int printLen = 0;
//Keeps button state and time the button was pressed
bool btnDown = false;
unsigned long btnDownMillis;
//Speed tracker
char speed = 'm';
bool RTL = true; //Text goes right to left if true
const int rows[] = { //Creates an array of the rows
R0, R1, R2, R3, R4, R5, R6, R7
};
const int cols[] = { //Creates an array of the columns
C0, C1, C2, C3, C4, C5, C6, C7
};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i = 0; i < 8; i++) { //Sets all of the pins to output
pinMode(rows[i], OUTPUT);
pinMode(cols[i], OUTPUT);
}
clearMatrix(); //Clears the leds
for (int i = 0; i < toPrint.length(); i++) {
EEPROM.write(i, toPrint.charAt(i)); //Writes the print text to the EEPROM, would be unnessesary is Wokwi kept the EEPROM data
}
printLen = toPrint.length(); //Sets the print length
Serial.println("Please enter a word: "); //Prompts the user to end what they'd like the word to be
attachInterrupt(0, btn, CHANGE); //Creates interrupt for a change in the button state
}
void loop() {
int letterIndex;
if (Serial.available() > 0) { //Checks if anything has been placed into the Serial
Serial.flush();
toPrint = Serial.readString(); //Reads the input
toPrint.toLowerCase(); //Converts the string to lower case
for (int i = 0; i < toPrint.length() - 1; i++) { //Writes the string to the EEPROM
EEPROM.write(i, toPrint.charAt(i));
}
printLen = toPrint.length() - 1;
}
for (int letterStep = 0; letterStep < printLen; letterStep++) { //Steps through each letter
int usedLetterStep; //Allows for the value to be manipulated based on the direction
if (RTL) { //If going right to left
usedLetterStep = letterStep;
}
else { //Id going left to right
usedLetterStep = printLen - 1 - letterStep;
}
char let = EEPROM.read(usedLetterStep); //Reads the next letter from the EEPROM
for (int i = 0; i < 26; i++) { //Checks the letter against each entry in the alphabet array
if (letterArray[i] == let) {
letterIndex = i;
break;
}
if (i == 25) { //Non numeric character
letterIndex = 26;
}
}
for (int offset = -6; offset < 6; offset++) { //Sets the offset
int usedOffset;
if (RTL == false) { //Multiplies the offset by negative 1 for left to right
usedOffset = offset * -1;
}
else {
usedOffset = offset;
}
for (int column = 0; column < 8; column++) { //Steps through each column
if (usedOffset + column <= 8 && usedOffset + column >= 0) { //Checks if indicies are within the correct range
for (int row = 0; row < 8; row++) { //Steps through each row in the column
digitalWrite(rows[row], library_display[letterIndex][row][column + usedOffset]); //turns on if the character table is 1
}
}
digitalWrite(cols[column], LOW); //Sets the column targeted to low
delay(1); //delay to breifly flash the led on
digitalWrite(cols[column], HIGH); //Sets the column targeted to high
}
clearMatrix(); // clear matrix
//waits before drawing the next frame, length is determined by the speed that the display is currently set to
switch (speed) {
case 's': //Slow
delay(200);
break;
case 'm': //Medium
delay(100);
break;
case 'f': //Fast
delay(50);
break;
}
}
}
}
void clearMatrix() { //Clears the matrix
for(int i = 0; i < 8; i++) {
digitalWrite(rows[i], LOW); //Sets all the rows to low
digitalWrite(cols[i], HIGH); //Sets all the columns to high
}
}
void btn() { //Button interrupt code
if (btnDown == false) { //Button is being pressed in
btnDown = true; //Updates the state
btnDownMillis = millis(); //Records current milliseconds
}
else { //button released
btnDown = false;
if (btnDownMillis + 2000 <= millis()) { //if held for more than 2 seconds
btnDownMillis = 0;
RTL = true; //Sets direction to right to left
return;
}
else if (btnDownMillis + 1000 <= millis()) {
switch (speed) {
case 's':
speed = 'm';
break;
case 'm':
speed = 'f';
break;
case 'f':
speed = 's';
break;
}
return;
btnDownMillis = 0;
}
else if (btnDownMillis + 1000 >= millis()) { //if held for less than 1 second
btnDownMillis = 0;
RTL = false; //sets direction to left to right
return;
}
}
}