#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define lebar 128
#define tinggi 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define RED 11
#define GREEN 10
#define BLUE 9
#define potR A0
#define potG A1
#define potB A2
int valueR;
int valueG;
int valueB;
Adafruit_SSD1306 oled(lebar, tinggi, &Wire, OLED_RESET);
void printCenter(String text, String mode, int tinggiTeks = 0, int ukuranText = 1, String warna = "putih") {
int16_t x1, y1;
uint16_t w, h;
uint16_t textColor;
if (warna == "putih") {
textColor = SSD1306_WHITE;
} else if (warna == "hitam") {
textColor = SSD1306_BLACK;
} else if (warna == "invert") {
textColor = SSD1306_INVERSE;
}
if (mode == "normal") {
oled.getTextBounds(text, 0, tinggiTeks, &x1, &y1, &w, &h);
while (w > lebar && text.length() > 0) {
text.remove(text.length() - 1);
text.trim();
text += "...";
oled.getTextBounds(text, 0, tinggiTeks, &x1, &y1, &w, &h);
}
int x = (lebar - w) / 2;
oled.setTextSize(ukuranText);
oled.setTextColor(textColor);
oled.setCursor(x, tinggiTeks);
oled.println(text);
} else if (mode == "full") {
oled.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
while (w > lebar && text.length() > 0) {
text.remove(text.length() - 1);
text.trim();
text += "...";
oled.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
}
int x = (lebar - w) / 2;
int y = (tinggi - h) / 2;
oled.setTextSize(ukuranText);
oled.setTextColor(textColor);
oled.setCursor(x, y);
oled.println(text);
}
}
void Color() {
int potValueR = analogRead(potR);
int potValueG = analogRead(potG);
int potValueB = analogRead(potB);
valueR = map(potValueR, 0, 1023, 0, 255);
valueG = map(potValueG, 0, 1023, 0, 255);
valueB = map(potValueB, 0, 1023, 0, 255);
String dataMerah = "Merah = " + String(valueR) + "%";
String dataHijau = "Hijau = " + String(valueG) + "%";
String dataBiru = "Biru = " + String(valueB) + "%";
oled.clearDisplay();
printCenter(dataMerah, "normal", 18);
printCenter(dataHijau, "full");
printCenter(dataBiru, "normal", 38);
oled.display();
}
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("OLED gagal dimulai"));
while (true);
}
oled.clearDisplay();
printCenter("Color RGB Maker", "full");
oled.display();
delay(3000);
}
void loop() {
Color();
analogWrite(RED, valueR);
analogWrite(GREEN, valueG);
analogWrite(BLUE, valueB);
delay(50);
}