#include <Adafruit_GFX.h> // https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_SSD1306.h> // https://github.com/adafruit/Adafruit_SSD1306
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_MIDDLE SCREEN_HEIGHT / 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int counter, buttonPin = 2, offset = 20, torque[128]; // <-- store 128 values
bool firstOne = 1, firstZero = 1; // vertical line indicator
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
display.drawLine(counter, 0, counter, 63, BLACK); // verticle line to erase trace
display.display();
readButton();
writeBuffer();
}
void readButton() {
if (digitalRead(buttonPin)) {
if (firstOne) { // draw vertical line on first transition
display.drawLine(counter, SCREEN_MIDDLE + offset, counter, SCREEN_MIDDLE - offset, WHITE);
firstOne = 0; // disable "one" transition
firstZero = 1; // enable "zero" transition
}
torque[counter] = SCREEN_MIDDLE - offset;
display.drawPixel(counter, SCREEN_MIDDLE - offset, WHITE);
} else {
if (firstZero) {
display.drawLine(counter, SCREEN_MIDDLE - offset, counter, SCREEN_MIDDLE + offset, WHITE);
firstZero = 0;
firstOne = 1;
}
torque[counter] = SCREEN_MIDDLE + offset;
display.drawPixel(counter, SCREEN_MIDDLE + offset, WHITE);
}
display.display();
}
void writeBuffer() {
counter++; // next pixel
if (counter == 128) { // OLED right edge
for (int j = 0; j < SCREEN_WIDTH / 16; j++) { // 8 rows
for (int i = 0; i < SCREEN_WIDTH / 8; i++) { // 16 columns
if ((torque[j * 8 + i]) < 100) Serial.print(F(" ")); // pad tens
if ((torque[j * 8 + i]) < 10) Serial.print(F(" ")); // pad ones
Serial.print(torque[j * 8 + i]); // data block
Serial.print(F(" ")); // spacing
}
Serial.println(); // next line
counter = 0; // reset column counter
}
for (int i = 0; i < 16 * 4; i++) // separate 128 readings blocks
Serial.print(F("="));
}
}Loading
ssd1306
ssd1306