// https://forum.arduino.cc/t/calipers-angle-measurement/1393255/4
#include <TinyDebug.h> // for serial monitor output
// tdPrintln("Not using SRAM"); // example Serial.println();
// #include <avr/pgmspace.h> // for memory
#include "SSD1306_minimal.h"
SSD1306_Mini oled; // 0.42" display 72x40
byte buttonpin = PB3, potpin = A2;
int potval, potvalold, minval, maxval;
char vals[6];
#define CHARWIDTH 6 // pixel width of a character and kern
void setup() {
oled.init(0x3C); // Initialize display with address
oled.clear(); // Clears the display
pinMode(buttonpin, INPUT_PULLUP);
pinMode(potpin, OUTPUT);
calibrate_pot();
show_min_max();
}
void loop() {
read_pot();
}
int read_pot() {
potval = analogRead(potpin);
if (potval != potvalold) {
oled.cursorTo(1, 1);
oled.printString(" "); // clear display
if (potval < minval) {
oled.cursorTo(1, 1);
oled.printString(" < MINVAL ");
}
if (potval > maxval) {
oled.cursorTo(1, 1);
oled.printString(" > MAXVAL ");
}
if ((potval >= minval) && (potval <= maxval)) {
int_to_char_array(potval, 1);
}
}
potvalold = potval;
}
void calibrate_pot() {
for (byte i = 0; i < 2; i++) {
oled.cursorTo(0, 0); // col 0 to 7, row 0 to 3
if (i == 0) {
oled.printString("SET MIN.");
tdPrintln("Move pot to set min. Then press button.");
} else {
oled.printString("SET MAX.");
tdPrintln("Move pot to set max. Then press button.");
}
press_to_continue();
if (i == 0)
// minval = read_pot();
minval = analogRead(potpin);
else
// maxval = read_pot();
maxval = analogRead(potpin);
oled.cursorTo(0, 1);
if (i == 0)
oled.printString("min set.");
else
oled.printString("max set.");
press_to_continue();
oled.clear();
}
}
void show_min_max() {
oled.cursorTo(2, 0);
oled.printString("MIN");
int_to_char_array(minval, 0);
oled.cursorTo(2, 1);
oled.printString("MAX");
int_to_char_array(maxval, 1);
press_to_continue();
oled.clear();
}
void int_to_char_array(int val, int row) {
int tens = val / 1000;
int ones = (val - (tens * 1000)) / 100;
int tnts = (val - (tens * 1000) - (ones * 100)) / 10;
int hnds = (val - (tens * 1000) - (ones * 100) - (tnts * 10)) / 1;
vals[0] = tens + '0';
vals[1] = ones + '0';
vals[2] = '.';
vals[3] = tnts + '0';
vals[4] = hnds + '0';
for (byte i = 0; i < 5; i++) {
oled.cursorTo((5 * CHARWIDTH) + (i * CHARWIDTH), row);
oled.printChar(vals[i]);
}
}
void press_to_continue() {
oled.cursorTo(0, 3);
oled.printString(">button<");
while (digitalRead(buttonpin) == HIGH);
delay(250); // fast readings need delay
}<------ SDA
<---- SCL
MIN --->
<--- MAX
CALIPER