#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display dimensions
#define OLED_WIDTH 128 // define the width of OLED
#define OLED_HEIGHT 64 // define the height of OLED
//initializes a character array with constants which is stored in program memory
const char studentName[] PROGMEM = "Joshua Tan";
const char studentID[] PROGMEM = "103982664";
const char studentCode[] PROGMEM = "ENG2009 Engineering Technology Inquiry Project";
const char studentYear[] PROGMEM = "Semester 1 2023";
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);// initialize the OLED display
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialise the display (communication protocol, I2C Address)
display.display(); //updates the display
}
void loop() {
display.clearDisplay(); //clear the display bufffer, help with new content
display.setTextSize(1); //sets the size of the text to white
display.setTextColor(WHITE); //sets the text colour to white
//uint8_t is data type, strlen_P is used to determine the lengtt of variables
//scroll the Student name from right to left
//variable i is used in outerloop (overall scrolling), variable j is used in inner loop (independant control)
for (uint8_t i = 0; i < strlen_P(studentName) + OLED_WIDTH; i++) { //initializes a loop counter with i, increment by 1 each loop
display.setCursor(0 - i, 0); //sets the cursor position for writing at 0-i in x, 0 in y
for (uint8_t j = 0; j < min(OLED_WIDTH, strlen_P(studentName) - i) - 1; j++) { //initializes a loop counter with j, increment by 1 each loop
display.write(pgm_read_byte(&studentName[j])); //reads the j-th character from the student name (in Progmem), and iterates displaying sequentially
}
// scroll the student ID from right to left
display.setCursor(0 - i, 10); //sets the cursor position for writing at 0-i in x, 10 in y
for (uint8_t j = 0; j < min(OLED_WIDTH, strlen_P(studentID) - i) - 1; j++) { //initializes a loop counter with j, increment by 1 each loop
display.write(pgm_read_byte(&studentID[j])); //reads the j-th character from the student ID (in Progmem), and iterates displaying sequentially
}
//scroll the student code from right to left
display.setCursor(0 - i, 25); //sets the cursor position for writing at 0-i in x, 25 in y
for (uint8_t j = 0; j < min(OLED_WIDTH, strlen_P(studentCode) - i) - 1; j++) { //initializes a loop counter with j, increment by 1 each loop
display.write(pgm_read_byte(&studentCode[j])); //reads the j-th character from the student code (in Progmem), and iterates displaying sequentially
}
//scroll the student code from right to left
display.setCursor(0 - i, 50); //sets the cursor position for writing at 0-i in x, 50 in y
for (uint8_t j = 0; j < min(OLED_WIDTH, strlen_P(studentYear) - i) - 1; j++) {//initializes a loop counter with j, increment by 1 each loop
display.write(pgm_read_byte(&studentYear[j])); //reads the j-th character from the student code (in Progmem), and iterates displaying sequentially
}
//still in the loop
display.display();//update the display buffer every loop
delay(500); //introduces a delay of 500ms
display.clearDisplay(); //clear the display, removing previously displayed text- allows for next iteration
}
}