// ----------------- Include any necessary libraries here -----------------------
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "univ_logo.h"
// ---------------------- IO & peripherals Constants ----------------------------
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
#define POT_PIN 35 // Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
// ---------------------------- Object creation ---------------------------------
// create an OLED display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// -------------------------- Functions definition ------------------------------
// function that displays a bitmap on oled display
void display_bitmap(Adafruit_SSD1306 &disp, const unsigned char* image,
unsigned int height, unsigned int width) {
disp.clearDisplay();
disp.setCursor(0, 0);
disp.drawBitmap(32, 0, image, height, width, WHITE);
disp.display();
delay(2000);
disp.clearDisplay();
disp.display();
}
// ------------------------------ Main program ---------------------------------
// this "setup" function is executed only once to configure the IO and peripherals
void setup() {
// Initialize oled interface
Serial.begin(115200);
// Initalize analog inputs
pinMode(POT_PIN, INPUT);
// initialize OLED display with I2C address 0x3C
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
// wait two seconds for initializing
delay(2000);
// set text size
display.setTextSize(1);
// set text color
display.setTextColor(WHITE);
// clear display
display.clearDisplay();
// display medea university logo
display_bitmap(display, univ_image, IMG_H, IMG_W);
}
// this "loop" function is executed indefinetly (in an infinite loop / superloop)
void loop() {
// read input voltage
int adc_value = analogRead(POT_PIN); // result is in 12bits format (0 - 4095)
float analog_value = (float)adc_value/4095.0 * 3.3; // convert analog reading to a voltage (0 - 3.3V)
// display the result on serial monitor (with uart interface)
Serial.println("Pot = " + String(analog_value,2) + " V");
// display the result on oled display
display.clearDisplay(); // clear display
display.setCursor(0, 2); // set position to display (x,y)
display.print("Pot = " + String(analog_value,2) + " V"); // set text
display.display(); // display on OLED
// get a new reading and display after waiting 100ms
delay(100);
// repeat form the first instruction of the loop function
}