/*
Using LCD Screen without library to display a
scrolling list of options.
For "template" uses the same code as my other project:
LCD Simple Without Libraries (Tutorial)
https://wokwi.com/projects/390024026784443393
&
LCD Scrolling List
https://wokwi.com/projects/390034357673137153
NOTE:
Data will be seen in the serial monitor since the TX and RX pins
are being used to send the bits to the screen
*/
#define RS 13
#define E 12
#define SCROLL_DOWN_PIN 9
#define TOGGLE 10
#define BLUE_PIN A0
#define GREEN_PIN A1
#define RED_PIN A2
const String options[3] = {
"RED",
"GREEN",
"BLUE",
};
const int amountOfOptions = 3;
int index = 0;
int counter = 0;
//Color RED
int R = 1;
int G = 0;
int B = 0;
/*--------------------------------------------------------------------------------------*/
void setup() {
DDRD = 0XFF;
pinMode(RS, OUTPUT);
pinMode(E, OUTPUT);
pinMode(SCROLL_DOWN_PIN, INPUT);
pinMode(TOGGLE, INPUT);
screenOnBlink();
clearDisplay();
returnHome();
printString(options[index]);
index = (index + 1 + amountOfOptions) % amountOfOptions;
cursorToSecondLine();
printString(options[index]);
cursorToSecondLine();
insertArrow();
}
void loop() {
int scrollDownPinValue = digitalRead(SCROLL_DOWN_PIN);
int togglePin = digitalRead(TOGGLE);
/*
Reversing the mapping because COM is Anode so RGB values
are "reversed", i.e. for a 255 color RED a 0 is needed on
the pin
*/
analogWrite(RED_PIN, map(R, 0, 1, 255, 0));
analogWrite(GREEN_PIN, map(G, 0, 1, 255, 0));
analogWrite(BLUE_PIN, map(B, 0, 1, 255, 0));
if (scrollDownPinValue) {
clearDisplay();
scrollDown();
delay(200);
}
if (togglePin) {
if (options[index] == "RED") {
R = !R;
} else if (options[index] == "GREEN") {
G = !G;
} else if (options[index] == "BLUE") {
B = !B;
}
updateDisplay();
delay(200);
}
}
void updateDisplay() {
clearDisplay();
returnHome();
index = (index - 1 + amountOfOptions) % amountOfOptions;
printString(options[index]);
index = (index + 1 + amountOfOptions) % amountOfOptions;
cursorToSecondLine();
printString(options[index]);
cursorToSecondLine();
insertArrow();
}
void scrollDown() {
clearDisplay();
returnHome();
printString(options[index]);
index = (index + 1 + amountOfOptions) % amountOfOptions;
cursorToSecondLine();
printString(options[index]);
cursorToSecondLine();
insertArrow();
}
void readSentData() {
digitalWrite(E, HIGH);
digitalWrite(E, LOW);
}
void returnHome() {
digitalWrite(RS, LOW);
PORTD = 0X02;
readSentData();
}
void clearDisplay() {
digitalWrite(RS, LOW);
PORTD = 0X01;
readSentData();
}
void cursorToSecondLine() {
digitalWrite(RS, LOW);
PORTD = 0XC0;
readSentData();
}
void screenOnBlink() {
digitalWrite(RS, LOW);
PORTD = 0X0C;
readSentData();
}
void printCharacter(char character) {
digitalWrite(RS, HIGH);
PORTD = (int) character;
readSentData();
}
void insertArrow(){
digitalWrite(RS, HIGH);
PORTD = 0X7E;
readSentData();
}
void printString(String stringToPrint) {
String newString = stringToPrint;
if (stringToPrint == "RED") {
newString += " ->";
newString += (R ? " [ON]" : " [OFF]");
} else if (stringToPrint == "GREEN") {
newString += " -> ";
newString += (G ? " [ON]" : " [OFF]");
} else if (stringToPrint == "BLUE") {
newString += " ->";
newString += (B ? " [ON]" : " [OFF]");
}
digitalWrite(RS, HIGH);
int stringSize = newString.length();
printCharacter(' ');
for (int i = 0; i < stringSize; i++) {
printCharacter(newString[i]);
}
}