I'm working on code to control multiple OLED displays. My ultimate goal here is to have 8 separate displays that count days. Each display is wired to its own push button. When their respective push buttons get pressed, it resets the day count. If the day count exceeds a threshold set for each individual display, the text turns to red and blinks.
I got my code to work for one display. I attempted to expand to two displays and the displays now update painfully slow and blink erratically. I'm very new to arduino so I suspect I've coded this in a super inefficient way and was hoping y'all can point me in the right direction.
Here is my code to control just a single display - which worked quite well.
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_GC9A01A.h"
Adafruit_GC9A01A tft( 10, 7);
//Adafruit_GC9A01A tft_2( 6, 7);
//Adafruit_GC9A01A tft_3( pin, 7 );
//Adafruit_GC9A01A tft_4( pin, 7 );
//Adafruit_GC9A01A tft_5( pin, 7 );
//Adafruit_GC9A01A tft_6( pin, 7 );
//Adafruit_GC9A01A tft_7( pin, 7 );
//Adafruit_GC9A01A tft_8( pin, 7 );
int day_1 = 1;
int threshold_1 = 5;
unsigned long previousMillis = 0;
const long interval = 5000;
const long interval_blink_on = 1000;
const long interval_blink_off = 300;
unsigned long previousMillisBlink = 0;
bool on = true;
const int buttonPin_1 = 4;
int buttonState_1 = 0;
void setup() {
Serial.begin(9600);
Serial.println("GC9A01A Test!");
tft.begin();
tft.setRotation(3);
tft.setTextColor(GC9A01A_WHITE);
tft.fillScreen(GC9A01A_BLACK);
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1);
}
void loop() {
unsigned long currentMillis = millis(); //this will be used to keep track of days
unsigned long currentMillisBlink = millis(); //this will be used to keep track of blink intervals
buttonState_1 = digitalRead(buttonPin_1);
if (buttonState_1 == HIGH) { //if button is pressed...
day_1 = 0; //reset day to 0
tft.setTextColor(GC9A01A_WHITE); //set tect color to white
tft.fillScreen(GC9A01A_BLACK); //wipe screen
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1); //print day count
}
if (currentMillis - previousMillis >= interval) { //check if interval has passed
previousMillis = currentMillis; //reset timer/interval
day_1++; //add 1 to day counter
if (day_1 > 9) { //if day is 2 digit number, reduce text size
tft.fillScreen(GC9A01A_BLACK);
tft.setCursor(35, 70);
tft.setTextSize(15);
tft.println(day_1);
} else {
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1); //print new day count
}
}
if (day_1 >= threshold_1) { //if the day count is over specified threshold...
tft.setTextColor(GC9A01A_RED, GC9A01A_BLACK); //set text color to red
if ((on == true) && (currentMillisBlink - previousMillisBlink >= interval_blink_on)) { //If the text is showing and the blink interval is up
tft.setTextColor(GC9A01A_BLACK); //set text to black to erase the text later
previousMillisBlink = currentMillisBlink; //reset the blink interval
on = !on; //show that text is off
if (day_1 > 9) {
tft.setCursor(35, 70);
tft.setTextSize(15);
tft.println(day_1);
} else {
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1); //erase the text
}
}
if ((on == false) && (currentMillisBlink - previousMillisBlink >= interval_blink_off)) { //if the screen is blank and the blink interval is up
tft.setTextColor(GC9A01A_RED); //set text color to red
previousMillisBlink = currentMillisBlink; //reset blink interval
on = !on; //show that text is now on
if (day_1 > 9) {
tft.setCursor(35, 70);
tft.setTextSize(15);
tft.println(day_1);
} else {
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1); //display the text the correct size according to # of digits
}
}
}
else {
tft.setTextColor(GC9A01A_WHITE, GC9A01A_BLACK); //set text to white if the threshold is not met
}
}
And here is my code when I tried to bring in a second display.
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_GC9A01A.h"
Adafruit_GC9A01A tft( 10, 7);
Adafruit_GC9A01A tft_2( 6, 7);
//Adafruit_GC9A01A tft_3( pin, 7 );
//Adafruit_GC9A01A tft_4( pin, 7 );
//Adafruit_GC9A01A tft_5( pin, 7 );
//Adafruit_GC9A01A tft_6( pin, 7 );
//Adafruit_GC9A01A tft_7( pin, 7 );
//Adafruit_GC9A01A tft_8( pin, 7 );
int day_1 = 1;
int day_2 = 1;
//int day_3 = 9;
//int day_4 = 9;
//int day_5 = 9;
//int day_6 = 9;
//int day_7 = 9;
//int day_8 = 9;
int threshold_1 = 5;
int threshold_2 = 2;
//int threshold_3 = 3;
//int threshold_4 = 4;
//int threshold_5 = 5;
//int threshold_6 = 6;
//int threshold_7 = 7;
//int threshold_8 = 8;
unsigned long previousMillis = 0;
const long interval = 5000;
const long interval_blink_on = 1000;
const long interval_blink_off = 400;
unsigned long previousMillisBlink = 0;
bool on = true;
const int buttonPin_1 = 4;
const int buttonPin_2 = 3;
int buttonState_1 = 0;
int buttonState_2 = 0;
void setup() {
Serial.begin(9600);
Serial.println("GC9A01A Test!");
Serial.println(millis());
tft.begin();
tft.setRotation(3);
tft.setTextColor(GC9A01A_WHITE);
tft.fillScreen(GC9A01A_BLACK);
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1);
tft_2.begin();
tft_2.setRotation(3);
tft_2.setTextColor(GC9A01A_WHITE);
tft_2.fillScreen(GC9A01A_BLACK);
tft_2.setCursor(70, 60);
tft_2.setTextSize(20);
tft_2.println(day_2);
}
void loop() {
unsigned long currentMillis = millis(); //this will be used to keep track of days
unsigned long currentMillisBlink = millis(); //this will be used to keep track of blink intervals
buttonState_1 = digitalRead(buttonPin_1);
buttonState_2 = digitalRead(buttonPin_2);
//buttonState_3 = digitalRead(buttonPin_3);
//buttonState_4 = digitalRead(buttonPin_4);
//buttonState_5 = digitalRead(buttonPin_5);
//buttonState_6 = digitalRead(buttonPin_6);
//buttonState_7 = digitalRead(buttonPin_7);
//buttonState_8 = digitalRead(buttonPin_8);
if (buttonState_1 == HIGH) { //if button is pressed...
day_1 = 0; //reset day to 0
tft.setTextColor(GC9A01A_WHITE);
tft.fillScreen(GC9A01A_BLACK);
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1); //print day
}
if (currentMillis - previousMillis >= interval) { //if 1 day has passed...
previousMillis = currentMillis;
day_1++; //add 1 to day counter
if (day_1 > 9) {
tft.fillScreen(GC9A01A_BLACK);
tft.setCursor(35, 70);
tft.setTextSize(15);
tft.println(day_1);
} else {
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1); //print new day count
}
day_2++; //add 1 to day counter
if (day_2 > 9) {
tft_2.fillScreen(GC9A01A_BLACK);
tft_2.setCursor(35, 70);
tft_2.setTextSize(15);
tft_2.println(day_2);
} else {
tft_2.setCursor(70, 60);
tft_2.setTextSize(20);
tft_2.println(day_2);
}
if (day_1 >= threshold_1) { //if the day count is over specified threshold...
tft.setTextColor(GC9A01A_RED, GC9A01A_BLACK);
if ((on == true) && (currentMillisBlink - previousMillisBlink >= interval_blink_on)) {
tft.setTextColor(GC9A01A_BLACK);
previousMillisBlink = currentMillisBlink;
on = !on;
if (day_1 > 9) {
tft.setCursor(35, 70);
tft.setTextSize(15);
tft.println(day_1);
} else {
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1);
}
}
if ((on == false) && (currentMillisBlink - previousMillisBlink >= interval_blink_off)) {
tft.setTextColor(GC9A01A_RED);
previousMillisBlink = currentMillisBlink;
on = !on; //change text to red and blink
if (day_1 > 9) {
tft.setCursor(35, 70);
tft.setTextSize(15);
tft.println(day_1);
} else {
tft.setCursor(70, 60);
tft.setTextSize(20);
tft.println(day_1);
}
}
}
else {
tft.setTextColor(GC9A01A_WHITE, GC9A01A_BLACK);
}
if (buttonState_2 == HIGH) { //if button is pressed...
day_2 = 0; //reset day to 0
tft_2.setTextColor(GC9A01A_WHITE);
tft_2.fillScreen(GC9A01A_BLACK);
tft_2.setCursor(70, 60);
tft_2.setTextSize(20);
tft_2.println(day_2); //print day
}
if (day_2 >= threshold_2) { //if the day count is over specified threshold...
tft_2.setTextColor(GC9A01A_RED, GC9A01A_BLACK);
if ((on == true) && (currentMillisBlink - previousMillisBlink >= interval_blink_on)) {
tft_2.setTextColor(GC9A01A_BLACK);
previousMillisBlink = currentMillisBlink;
on = !on;
if (day_2 > 9) {
tft_2.setCursor(35, 70);
tft_2.setTextSize(15);
tft_2.println(day_2);
} else {
tft_2.setCursor(70, 60);
tft_2.setTextSize(20);
tft_2.println(day_2);
}
}
if ((on == false) && (currentMillisBlink - previousMillisBlink >= interval_blink_off)) {
tft_2.setTextColor(GC9A01A_RED);
previousMillisBlink = currentMillisBlink;
on = !on; //change text to red and blink
if (day_2 > 9) {
tft_2.setCursor(35, 70);
tft_2.setTextSize(15);
tft_2.println(day_2);
} else {
tft_2.setCursor(70, 60);
tft_2.setTextSize(20);
tft_2.println(day_1);
}
}
else {
tft_2.setTextColor(GC9A01A_WHITE, GC9A01A_BLACK);
}
}
}
}