Devices & Components
Arduino Nano
CA3089
0.91 Inch 128x32 IIC I2C Blue OLED LCD Display DIY Oled Module SSD1306 Driver IC DC 3.3V 5V For Arduino PIC
Hardware & Tools
Soldering Iron Kit
Software & Tools
Arduino IDE
Project description
Code
Code
cpp
...
1// by mircemk April, 2026 2 3#include <Wire.h> 4#include <Adafruit_GFX.h> 5#include <Adafruit_SSD1306.h> 6 7#define SCREEN_WIDTH 128 8#define SCREEN_HEIGHT 64 9Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 10 11// ---- UI (поголем BOX долу) ---- 12const int BAR_Y = 2; 13const int BAR_H = 12; 14const int BAR_X = 2; 15const int BAR_W = 124; 16 17const int BOX_X = 6; // помал margin 18const int BOX_Y = 20; // спуштено нагоре малку 19const int BOX_W = 116; // пошироко 20const int BOX_H = 44; // повисоко (скоро до дното) 21const int BOX_R = 8; 22 23// ---- Analog ---- 24const int ANALOG_PIN = A0; 25 26// ======= РАНГ (ADC) ======= 27int IN_MIN = 60; 28int IN_MAX = 1000; 29 30static inline int clampInt(int v, int lo, int hi) { 31 if (v < lo) return lo; 32 if (v > hi) return hi; 33 return v; 34} 35 36static inline int mapLongToInt(long x, long in_min, long in_max, long out_min, long out_max) { 37 if (in_max == in_min) return (int)out_min; 38 return (int)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); 39} 40 41void drawCenteredTextInBox(const char* txt, int textSize) { 42 display.setTextSize(textSize); 43 44 int16_t x1, y1; 45 uint16_t w, h; 46 display.getTextBounds(txt, 0, 0, &x1, &y1, &w, &h); 47 48 int tx = BOX_X + (BOX_W - (int)w) / 2; 49 int ty = BOX_Y + (BOX_H - (int)h) / 2; 50 51 display.setCursor(tx, ty); 52 display.print(txt); 53} 54 55void setup() { 56 analogReference(DEFAULT); 57 Wire.begin(); 58 59 if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 60 while (1) {} 61 } 62 display.clearDisplay(); 63 display.setTextColor(SSD1306_WHITE); 64 display.display(); 65} 66 67void loop() { 68 int raw = analogRead(ANALOG_PIN); 69 70 int a = IN_MIN, b = IN_MAX; 71 if (a > b) { int t = a; a = b; b = t; } 72 73 int rawWin = clampInt(raw, a, b); 74 75 int rel1000 = mapLongToInt(rawWin, a, b, 0, 1000); 76 int barFill = mapLongToInt(rawWin, a, b, 0, BAR_W); 77 78 display.clearDisplay(); 79 80 // BAR 81 display.drawRect(BAR_X, BAR_Y, BAR_W, BAR_H, SSD1306_WHITE); 82 int innerW = barFill - 2; 83 if (innerW < 0) innerW = 0; 84 if (innerW > BAR_W - 2) innerW = BAR_W - 2; 85 display.fillRect(BAR_X + 1, BAR_Y + 1, innerW, BAR_H - 2, SSD1306_WHITE); 86 87 // BOX 88 display.drawRoundRect(BOX_X, BOX_Y, BOX_W, BOX_H, BOX_R, SSD1306_WHITE); 89 90 // број (auto size: 0-999 големо, 1000 помало) 91 char buf[6]; 92 snprintf(buf, sizeof(buf), "%d", rel1000); 93 94 int size = (rel1000 >= 1000) ? 3 : 4; // 1000 да собере 95 drawCenteredTextInBox(buf, size); 96 97 display.display(); 98 delay(1000); 99}
Documentation
Schematic CA3089
...
Schematic CA3089.jpg

Comments
Only logged in users can leave comments