#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int ANALOG_INPUT_PIN = A0;
const int MIN_ANALOG_INPUT = 0;
const int MAX_ANALOG_INPUT = 1023;
const int DELAY_LOOP_MS = 5; // change to slow down how often to read and graph value
int InputBuffer[SCREEN_WIDTH]; //fast way to store values
int BufferIndex = 0; // tracks where we are in the circular buffer
// status bar
boolean drawStatusBarIndicator = true; // change to show/hide status bar
int Graph_Height = SCREEN_HEIGHT;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(0, 0);
display.println("Screen initialized!");
display.display();
delay(1000);
display.clearDisplay();
if(drawStatusBarIndicator){
Graph_Height = SCREEN_HEIGHT - 10;
}
}
void loop() {
// Clear the display on each frame. We draw from the _circularBuffer
display.clearDisplay();
// Read and store the analog data into a circular buffer
int analog_Val = analogRead(ANALOG_INPUT_PIN);
Serial.println(analog_Val);
InputBuffer[BufferIndex++] = analog_Val;
// Set the circular buffer index back to zero when it reaches the
// right of the screen
if(BufferIndex >= display.width()){
BufferIndex = 0;
}
if(drawStatusBarIndicator){
drawStatusBar(analog_Val);
}
// Draw the line graph based on data in _circularBuffer
int xPos = 0;
for (int i = BufferIndex; i < display.width(); i++){
int analog_Val = InputBuffer[i];
drawLine(xPos, analog_Val);
xPos++;
}
for(int i = 0; i < BufferIndex; i++){
int analog_Val = InputBuffer[i];
drawLine(xPos, analog_Val);
xPos++;;
}
display.display();
delay(DELAY_LOOP_MS);
}
/**
* Draw the line at the given x position and analog value
*/
void drawLine(int xPos, int analogVal){
int lineHeight = map(analogVal, MIN_ANALOG_INPUT, MAX_ANALOG_INPUT, 0, Graph_Height);
int yPos = display.height() - lineHeight;
display.drawFastVLine(xPos, yPos, lineHeight, SSD1306_WHITE);
}
/**
* Draws the status bar at top of screen with fps and analog value
*/
void drawStatusBar(int analogVal) {
// erase status bar by drawing all black
display.fillRect(0, 0, display.width(), 8, SSD1306_BLACK);
// Draw current val
display.setCursor(0, 0);
display.print(analogVal);
// Draw frame count
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds("RUN", 0, 0, &x1, &y1, &w, &h);
display.setCursor(display.width() - w, 0);
display.print("RUN");
}