Wiring
Sensor Forward A0 Tegangan maju (via coupler)
Sensor Reflected A1 Tegangan pantul (via coupler)
OLED SDA A4 Data I2C
OLED SCL A5 Clock I2C
OLED VCC 5V Power 5V
OLED GND GND Ground
Buzzer (+) D4 Alarm SWR tinggi
Buzzer (–) GND Ground
Kode Program
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define PIN_FORWARD A0
#define PIN_REFLECTED A1
#define BUZZER_PIN 4
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Kalibrasi daya
const float forwardScale = 1.28;
const float reflectedScale = 1.28;
// Fungsi pembacaan rata-rata analog
float readAverage(int pin, int samples = 10) {
long total = 0;
for (int i = 0; i < samples; i++) {
total += analogRead(pin);
delay(2); // delay kecil antar pembacaan
}
return (float)total / samples;
}
void setup() {
Serial.begin(9600);
pinMode(PIN_FORWARD, INPUT);
pinMode(PIN_REFLECTED, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED tidak ditemukan"));
while (true);
}
display.clearDisplay();
display.display();
}
void loop() {
// Baca tegangan dari coupler dengan rata-rata
float vForward = readAverage(PIN_FORWARD) * (5.0 / 1023.0);
float vReflected = readAverage(PIN_REFLECTED) * (5.0 / 1023.0);
// Hitung daya
float pForward = vForward * forwardScale;
float pReflected = vReflected * reflectedScale;
// Hitung SWR
float swr;
if (pForward == 0) {
swr = 1.0;
} else {
float sqrtR = sqrt(pReflected / pForward);
if (sqrtR >= 1.0) sqrtR = 0.999;
swr = (1 + sqrtR) / (1 - sqrtR);
}
// Alarm jika SWR tinggi
digitalWrite(BUZZER_PIN, (swr > 1.5) ? HIGH : LOW);
// Tampilan OLED
display.clearDisplay();
// Judul
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("PILIHAN");
display.setCursor(0, 16);
display.print("ONLINE");
// Data daya & SWR
display.setTextSize(1);
display.setCursor(0, 34);
display.print("FWD:");
display.print(pForward, 1);
display.print("W");
display.setCursor(64, 34);
display.print("REF:");
display.print(pReflected, 1);
display.print("W");
display.setCursor(0, 44);
display.print("SWR:");
display.print(swr, 2);
// Bar SWR
int barWidth = map(constrain(swr, 1, 5) * 10, 10, 50, 10, 120);
display.drawRect(0, 56, 128, 6, SSD1306_WHITE);
display.fillRect(0, 56, barWidth, 6, SSD1306_WHITE);
display.display();
delay(300);
}
0 comments:
Post a Comment