//Example to display simple text on I2C OLED display (128x64, SSD1306)
/******************************************************
OLED display
*******************************************************/
#include <Wire.h> //Include Arduino library for I2C communication
#include <Adafruit_SSD1306.h> //Adafruit SSD1306 library for OLED display
#define OLED_ADDRESS 0x3C //Use your OLED I2C address
#define SCREEN_WIDTH 128 //OLED display width, in pixels
#define SCREEN_HEIGHT 64 //OLED display height, in pixels
Adafruit_SSD1306 oledDisplay(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);//Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
/******************************************************
Variable
- variable will change
*******************************************************/
int count = 0; //value to be displayed on OLED
int sensorValue = 0; // value read from the pot
/******************************************************
setup()
- Mainly used for initialization (executed once)
*******************************************************/
void setup() {
/* Serial Monitor */
Serial.begin(9600); //initialize serial communications - for Serial Monitor
/* OLED Display */
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!oledDisplay.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);// Don't proceed, loop forever
}
delay(2000); //pause for 2 seconds
oledDisplay.clearDisplay(); //clear OLED display buffer
oledDisplay.setTextSize(1); //set text size
oledDisplay.setTextColor(WHITE); //dark background, white text
//oledDisplay.setTextColor(BLACK); //bright background, black text.
oledDisplay.setCursor(5, 10); //set cursor position (x,y) = (0,3)
oledDisplay.println("Pot Reading:"); //display static text
oledDisplay.display(); // update display with all of the above graphics
}
/******************************************************
loop()
- To run the codes in an infinite manner
*******************************************************/
void loop() {
sensorValue = analogRead(A0);
oledDisplay.setTextSize(3); //set text size
oledDisplay.setCursor(10, 30); //set cursor position
oledDisplay.print(sensorValue); //display current count value and increment count by 1
oledDisplay.display(); // update display with all of the above graphics
delay(50);
/* Clear OLED display - count value before displaying a new value */
clearLine(10, 30, 3); //cursorX=0, cursorY=30, TextSize=2
}
/******************************************************
User-defined function
*******************************************************/
//Clear line starting at position (cursorX, cursorY) with TextSize
void clearLine(unsigned int cursorX, unsigned int cursorY, unsigned int TextSize)
{
//Character height is 7 pixel for text size = 1
for (int height =cursorY; height<=cursorY+(7*TextSize)-1; height++)
{
for (int width=cursorX; width<127; width++)
{
oledDisplay.drawPixel(width, height, BLACK);
}
}
}