#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <math.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address and dimensions
// Buttons
#define MENU 3
#define UP 2
#define DOWN 4
#define relay 7
#define PWM 9
#define temp_led_error 10
#define swr_led_error 11
int menu;
int up;
int down;
int del_speed = 150;
String formattedFloat;
int arraylenght;
int pwm;
int startup = 0;
String stats;
int ADCvalue;
float forwardPower;
float reflectedPower;
float rfinPower;
float vswr;
float LIMITS[] = {2000.0, 50.0, 25.0, 5.0, 90.0, 50.0}; // Limits for different parameters
int enable_disable[] = {0, 0, 0, 0, 1}; // Alarm enable or disable for different parameters
const int forwardPin = A0; // Analog input pin for forward power measurement
const int reflectedPin = A1; // Analog input pin for reflected power measurement
const int rfinPin = A2; // Analog input pin for RF input power measurements
const int temppin = A3; // Temperature sensor pin
const int TonePin = 13; // Pin for beeper
const int forwardRef = 1023; // Maximum value for forward power measurement (adjust based on your circuit)
const int reflectedRef = 1023; // Maximum value for reflected power measurement (adjust based on your circuit)
const int rfinRef = 1023; // Maximum value for RF input power measurements (adjust based on your circuit)
unsigned long previousMillis = 0; // Variable to store the previous timestamp
unsigned long interval = 1000; // Delay interval in milliseconds
const int toneDuration = 500;
const int toneInterval = 1000;
unsigned long prevTone = 0;
boolean tonePlaying = false;
bool FWD = false;
bool REF = false;
bool RIN = false;
bool SWR = false;
bool TEMP = false;
bool reset = true;
bool alarm = false;
void setup() {
Serial.begin(115200);
arraylenght = sizeof(LIMITS) / sizeof(LIMITS[0]) - 1;
// Check if retrievedData is NaN
int check = EEPROM.read(1);
Serial.println(check);
if (check == 255) {
Serial.println("Data is NaN!");
EEPROM.put(1, LIMITS[0]);
EEPROM.put(20, LIMITS[1]);
EEPROM.put(40, LIMITS[2]);
EEPROM.put(60, LIMITS[3]);
EEPROM.put(80, LIMITS[4]);
EEPROM.put(100, LIMITS[5]);
EEPROM.put(150, enable_disable[0]);
EEPROM.put(152, enable_disable[1]);
EEPROM.put(154, enable_disable[2]);
EEPROM.put(156, enable_disable[3]);
EEPROM.put(158, enable_disable[4]);
// Perform your desired action here
} else {
Serial.println("Data is valid.");
// Perform a different action here
}
EEPROM.get(1, LIMITS[0]);
EEPROM.get(20, LIMITS[1]);
EEPROM.get(40, LIMITS[2]);
EEPROM.get(60, LIMITS[3]);
EEPROM.get(80, LIMITS[4]);
EEPROM.get(100, LIMITS[5]);
EEPROM.get(150, enable_disable[0]);
EEPROM.get(152, enable_disable[1]);
EEPROM.get(154, enable_disable[2]);
EEPROM.get(156, enable_disable[3]);
EEPROM.get(158, enable_disable[4]);
for (int i = 0; i <= arraylenght ; i ++) {
Serial.println(LIMITS[i]);
}
for (int i = 0; i <= 4 ; i ++) {
Serial.print("enable_disable ");
Serial.println(enable_disable[i]);
}
lcd.init(); // Initialize the LCD
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("********************");
lcd.setCursor(0, 1);
lcd.print("*CJTECH ELECTRONICS*");
lcd.setCursor(0, 2);
lcd.print("*BROADCASTING SVCS.*");
lcd.setCursor(0, 3);
lcd.print("********************");
pinMode(TonePin, OUTPUT);
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay(150);
tone(TonePin, 2000);
delay(150);
noTone(TonePin);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("********************");
lcd.setCursor(0, 1);
lcd.print("******Ver. 4.0******");
lcd.setCursor(0, 2);
lcd.print("******06/03/23******");
lcd.setCursor(0, 3);
lcd.print("********************");
delay(3000);
lcd.clear();
pinMode(MENU, INPUT_PULLUP);
pinMode(UP, INPUT_PULLUP);
pinMode(DOWN, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(PWM, OUTPUT);
pinMode(temp_led_error, OUTPUT);
pinMode(swr_led_error, OUTPUT);
digitalWrite(temp_led_error, LOW);
digitalWrite(swr_led_error, LOW);
digitalWrite(relay, LOW);
analogWrite(PWM, 0);
// Progress bar graph
//lcd.setCursor(0, 0);
//lcd.print("********************");
lcd.setCursor(0, 1);
lcd.print("***Initializing..***");
//lcd.setCursor(0, 2);
//lcd.print("********************");
// lcd.setCursor(0, 3);
// lcd.print("********************");
// Progress bar animation
int progress = 0;
int barWidth = 19;
while (progress <= barWidth) {
lcd.setCursor(progress, 2);
lcd.write(255); // Display a solid block character
delay(300);
progress++;
}
lcd.clear();
}
void loop() {
PWM_smoth();
data_all(); //where data is collected
detect_click(); //detect any click in buttons
setting_routine(); //go to settings
warnings();
}
void setting_routine() {
if (!menu) {
lcd.clear();
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay(150);
noTone (TonePin);
int Switch = 6;
while (1) {
startup = 0; //return to 0 to do again smoth startup after exit menu
//float LIMITS[] = {FWD, REF, RIN, SWR, TEMP, PWM};
//float LIMITS[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
lcd.setCursor(8, 0); // Set the cursor to the position for VSWR value
lcd.print("MENU"); // Clear the previous VSWR value
detect_click();
if (!up) {
lcd.clear();
if (Switch <= 1) {
Switch = 8;
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
//lcd.clear();
delay(100);
} else {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
Switch --;
//lcd.clear();
delay(100);
}
/*
if (Switch == 6) {
lcd.setCursor(0, 1);
lcd.print("FWD ");
}
if (Switch == 7 || Switch == 6) {
lcd.clear();
}
*/
}
if (!down) {
lcd.clear();
if (Switch >= 8) {
Switch = 1;
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
//lcd.clear();
delay(100);
} else {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
Switch ++;
//lcd.clear();
delay(100);
}
/*
if (Switch == 2) {
lcd.setCursor(0, 1);
lcd.print("FWD ");
}
if (Switch == 7 || Switch == 1) {
lcd.clear();
}
*/
}
switch (Switch) {
case 1:
lcd.setCursor(0, 1);
lcd.print("FWD ");
lcd.print("<==");
lcd.setCursor(0, 2);
lcd.print("REF ");
formattedFloat = formatFloat(LIMITS[1], 0);
lcd.print(formattedFloat);
//lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("PIN ");
//lcd.print(" ");
formattedFloat = formatFloat(LIMITS[2], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 1);
lcd.print("SWR ");
//lcd.print(" ");
formattedFloat = formatFloat(LIMITS[3], 1);
lcd.print(formattedFloat);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
//lcd.print(" ");
formattedFloat = formatFloat(LIMITS[4], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 3);
lcd.print("EXIT ");
lcd.print(" ");
break;
case 2:
lcd.setCursor(0, 1);
lcd.print("FWD ");
formattedFloat = formatFloat(LIMITS[0], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 2);
lcd.print("REF ");
lcd.print("<==");
lcd.setCursor(0, 3);
lcd.print("PIN ");
formattedFloat = formatFloat(LIMITS[2], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 1);
lcd.print("SWR ");
formattedFloat = formatFloat(LIMITS[3], 1);
lcd.print(formattedFloat);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
formattedFloat = formatFloat(LIMITS[4], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 3);
lcd.print("EXIT ");
lcd.print(" ");
break;
case 3:
lcd.setCursor(0, 1);
lcd.print("FWD ");
formattedFloat = formatFloat(LIMITS[0], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 2);
lcd.print("REF ");
formattedFloat = formatFloat(LIMITS[1], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 3);
lcd.print("PIN ");
lcd.print("<==");
lcd.setCursor(10, 1);
lcd.print("SWR ");
formattedFloat = formatFloat(LIMITS[3], 1);
lcd.print(formattedFloat);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
formattedFloat = formatFloat(LIMITS[4], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 3);
lcd.print("EXIT ");
lcd.print(" ");
break;
case 4:
lcd.setCursor(0, 1);
lcd.print("FWD ");
formattedFloat = formatFloat(LIMITS[0], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 2);
lcd.print("REF ");
formattedFloat = formatFloat(LIMITS[1], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 3);
lcd.print("PIN ");
formattedFloat = formatFloat(LIMITS[2], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 1);
lcd.print("SWR ");
lcd.print("<==");
lcd.setCursor(10, 2);
lcd.print("TEMP ");
formattedFloat = formatFloat(LIMITS[4], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 3);
lcd.print("EXIT ");
lcd.print(" ");
break;
case 5:
lcd.setCursor(0, 1);
lcd.print("FWD ");
formattedFloat = formatFloat(LIMITS[0], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 2);
lcd.print("REF ");
formattedFloat = formatFloat(LIMITS[1], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 3);
lcd.print("PIN ");
formattedFloat = formatFloat(LIMITS[2], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 1);
lcd.print("SWR ");
formattedFloat = formatFloat(LIMITS[3], 1);
lcd.print(formattedFloat);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
lcd.print("<==");
lcd.setCursor(10, 3);
lcd.print("EXIT ");
lcd.print(" ");
break;
case 6:
lcd.setCursor(0, 1);
lcd.print("FWD ");
formattedFloat = formatFloat(LIMITS[0], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 2);
lcd.print("REF ");
formattedFloat = formatFloat(LIMITS[1], 0);
lcd.print(formattedFloat);
lcd.setCursor(0, 3);
lcd.print("PIN ");
formattedFloat = formatFloat(LIMITS[2], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 1);
lcd.print("SWR ");
formattedFloat = formatFloat(LIMITS[3], 1);
lcd.print(formattedFloat);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
formattedFloat = formatFloat(LIMITS[4], 0);
lcd.print(formattedFloat);
lcd.setCursor(10, 3);
lcd.print("EXIT ");
lcd.print("<==");
break;
case 7:
pwm = map(LIMITS[5], 0, 255, 0, 100);
Serial.print(LIMITS[5]);
Serial.print(" ");
Serial.println(pwm);
lcd.setCursor(0, 1);
lcd.print("DRIVE ");
lcd.print("<== ");
formattedFloat = formatFloat(pwm, 0);
lcd.print(formattedFloat);
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("ON/OFF Alarm ");
break;
case 8:
pwm = map(LIMITS[5], 0, 255, 0, 100);
Serial.print(LIMITS[5]);
Serial.print(" ");
Serial.println(pwm);
lcd.setCursor(0, 1);
lcd.print("DRIVE ");
formattedFloat = formatFloat(pwm, 0);
lcd.print(formattedFloat);
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("ON/OFF Alarm ");
lcd.print("<== ");
} //switch end
//exit memu
if (Switch == 6 && !menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
break;
}
//FWD
if (Switch == 1 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("FWD");
delay(500);
while (1) {
detect_click();
if (!menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
break;
}
if (!up && LIMITS[0] <= 2990.0) {
LIMITS[0] += 10.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
noTone (TonePin);
}
}
if (!down && LIMITS[0] >= 10.0) {
LIMITS[0] -= 10.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (down && up ) {
del_speed = 150;
}
lcd.setCursor(0, 2);
lcd.print(LIMITS[0]);
lcd.print(" ");
}
}
//REF
if (Switch == 2 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("REF");
delay(500);
while (1) {
detect_click();
if (!menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
break;
}
if (!up && LIMITS[1] <= 99.0) {
LIMITS[1] += 1.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (!down && LIMITS[1] >= 1.0) {
LIMITS[1] -= 1.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (down && up ) {
del_speed = 150;
noTone (TonePin);
}
lcd.setCursor(0, 2);
lcd.print(LIMITS[1]);
lcd.print(" ");
}
}
//RIN
if (Switch == 3 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("PIN");
delay(500);
while (1) {
detect_click();
if (!menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
break;
}
if (!up && LIMITS[2] <= 49.0) {
LIMITS[2] += 1.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (!down && LIMITS[2] >= 1.0) {
LIMITS[2] -= 1.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
noTone (TonePin);
}
}
if (down && up ) {
del_speed = 150;
}
lcd.setCursor(0, 2);
lcd.print(LIMITS[2]);
lcd.print(" ");
}
}
//SWR
if (Switch == 4 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("SWR");
delay(500);
while (1) {
detect_click();
if (!menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
break;
}
if (!up && LIMITS[3] <= 10.0) {
LIMITS[3] += 0.1;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (!down && LIMITS[3] >= 1.0) {
LIMITS[3] -= .1;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
noTone (TonePin);
}
}
if (down && up ) {
del_speed = 150;
}
lcd.setCursor(0, 2);
lcd.print(LIMITS[3]);
lcd.print(" ");
}
}
//TEMP
if (Switch == 5 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("TEMP");
delay(500);
while (1) {
detect_click();
if (!menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
break;
}
if (!up && LIMITS[4] <= 99.0) {
LIMITS[4] += 1.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (!down && LIMITS[4] >= 1.0) {
LIMITS[4] -= 1.0;
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (down && up ) {
del_speed = 150;
noTone (TonePin);
}
lcd.setCursor(0, 2);
lcd.print(LIMITS[4]);
lcd.print(" ");
}
}
//PWM
if (Switch == 7 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(8, 0);
lcd.print("DRIVE");
pwm = map(LIMITS[5], 0, 255, 0, 100);
delay(500);
while (1) {
detect_click();
if (!menu) {
analogWrite(PWM, 0); //PWM at pin 9
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
lcd.clear();
break;
}
// Increment or decrement the PWM value based on button presses
if (!up) {
pwm = constrain(pwm + 1, 0, 100); // Increment the PWM value, constrained between 0 and 100
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
if (!down) {
pwm = constrain(pwm - 1, 0, 100); // Decrement the PWM value, constrained between 0 and 100
tone(TonePin, 2000);
delay (del_speed);
noTone (TonePin);
delay(del_speed);
if (del_speed > 20) {
del_speed -= 20;
}
}
// Map the PWM value from 0-100 to 0-255 range
LIMITS[5] = map(pwm, 0, 100, 0, 255);
analogWrite(PWM, LIMITS[5] );
//Serial.println(LIMITS[5]);
if (down && up ) {
del_speed = 150;
noTone (TonePin);
}
lcd.setCursor(0, 2);
lcd.print(pwm);
lcd.print(" ");
lcd.print("%");
}
if (alarm && LIMITS[5] != 0) {
digitalWrite(temp_led_error, LOW);
digitalWrite(swr_led_error, LOW);
alarm = false; //reset alarm
}
}
//enable or diable alarms
if (Switch == 8 && !menu) {
lcd.clear();
lcd.setCursor(0, 0); // Set the cursor to the position for VSWR value
lcd.print("UP to select");
lcd.setCursor(0, 1); // Set the cursor to the position for VSWR value
lcd.print("DOWN to Set");
lcd.setCursor(0, 2); // Set the cursor to the position for VSWR value
lcd.print("MENU to exit");
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
delay(3000);
Serial.println("Edit alarms");
lcd.clear();
int ed = 1;
while (1) {
//float LIMITS[] = {FWD, REF, RIN, SWR, TEMP, PWM};
//float LIMITS[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
//enable_disable[] = { 0, 1, 1, 1, 1};
//TEMP && !SWR && !REF && !RIN && !FWD)
lcd.setCursor(0, 0); // Set the cursor to the position for VSWR value
lcd.print("UP select, DOWN Set");
detect_click();
if (!menu) {
analogWrite(PWM, 0); //PWM at pin 9
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
lcd.clear();
break;
}
if (!up) {
tone(TonePin, 2000);
delay (200);
noTone (TonePin);
ed ++;
if (ed == 6) {
ed = 1;
}
lcd.clear();
}
if (!down) {
enable_disable[ed - 1] = !enable_disable[ed - 1];
Serial.println(enable_disable[ed - 1]);
tone(TonePin, 2000);
delay (200);
noTone (TonePin);
}
//FWD,REF,RIN,SWR,TEMP
//int enable_disable[] = {1, 1, 1, 1, 1};
switch (ed) {
case 1:
lcd.setCursor(0, 1);
lcd.print("FWD ");
stats = ONOFF(enable_disable[0]);
lcd.print(stats);
lcd.print(" <");
lcd.setCursor(0, 2);
lcd.print("SWR ");
stats = ONOFF(enable_disable[1]);
lcd.print(stats);
lcd.setCursor(0, 3);
lcd.print("REF ");
stats = ONOFF(enable_disable[2]);
lcd.print(stats);
lcd.setCursor(10, 1);
lcd.print("RIN ");
stats = ONOFF(enable_disable[3]);
lcd.print(stats);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
stats = ONOFF(enable_disable[4]);
lcd.print(stats);
break;
case 2:
lcd.setCursor(0, 1);
lcd.print("FWD ");
stats = ONOFF(enable_disable[0]);
lcd.print(stats);
lcd.setCursor(0, 2);
lcd.print("SWR ");
stats = ONOFF(enable_disable[1]);
lcd.print(stats);
lcd.print(" <");
lcd.setCursor(0, 3);
lcd.print("REF ");
stats = ONOFF(enable_disable[2]);
lcd.print(stats);
lcd.setCursor(10, 1);
lcd.print("RIN ");
stats = ONOFF(enable_disable[3]);
lcd.print(stats);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
stats = ONOFF(enable_disable[4]);
lcd.print(stats);
break;
case 3:
lcd.setCursor(0, 1);
lcd.print("FWD ");
stats = ONOFF(enable_disable[0]);
lcd.print(stats);
lcd.setCursor(0, 2);
lcd.print("SWR ");
stats = ONOFF(enable_disable[1]);
lcd.print(stats);
lcd.setCursor(0, 3);
lcd.print("REF ");
stats = ONOFF(enable_disable[2]);
lcd.print(stats);
lcd.print(" <");
lcd.setCursor(10, 1);
lcd.print("RIN ");
stats = ONOFF(enable_disable[3]);
lcd.print(stats);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
stats = ONOFF(enable_disable[4]);
lcd.print(stats);
break;
case 4:
lcd.setCursor(0, 1);
lcd.print("FWD ");
stats = ONOFF(enable_disable[0]);
lcd.print(stats);
lcd.setCursor(0, 2);
lcd.print("SWR ");
stats = ONOFF(enable_disable[1]);
lcd.print(stats);
lcd.setCursor(0, 3);
lcd.print("REF ");
stats = ONOFF(enable_disable[2]);
lcd.print(stats);
lcd.setCursor(10, 1);
lcd.print("RIN ");
stats = ONOFF(enable_disable[3]);
lcd.print(stats);
lcd.print(" <");
lcd.setCursor(10, 2);
lcd.print("TEMP ");
stats = ONOFF(enable_disable[4]);
lcd.print(stats);
break;
case 5:
lcd.setCursor(0, 1);
lcd.print("FWD ");
stats = ONOFF(enable_disable[0]);
lcd.print(stats);
lcd.setCursor(0, 2);
lcd.print("SWR ");
stats = ONOFF(enable_disable[1]);
lcd.print(stats);
lcd.setCursor(0, 3);
lcd.print("REF ");
stats = ONOFF(enable_disable[2]);
lcd.print(stats);
lcd.setCursor(10, 1);
lcd.print("RIN ");
stats = ONOFF(enable_disable[3]);
lcd.print(stats);
lcd.setCursor(10, 2);
lcd.print("TEMP ");
stats = ONOFF(enable_disable[4]);
lcd.print(stats);
lcd.print(" <");
break;
}
}
}
}
//save limits
EEPROM.put(1, LIMITS[0]);
EEPROM.put(20, LIMITS[1]);
EEPROM.put(40, LIMITS[2]);
EEPROM.put(60, LIMITS[3]);
EEPROM.put(80, LIMITS[4]);
EEPROM.put(100, LIMITS[5]);
//save alarm if enable or disable
EEPROM.put(150, enable_disable[0]);
EEPROM.put(152, enable_disable[1]);
EEPROM.put(154, enable_disable[2]);
EEPROM.put(156, enable_disable[3]);
EEPROM.put(158, enable_disable[4]);
for (int i = 0; i <= arraylenght ; i ++) {
Serial.println(LIMITS[i]);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" SAVING CONFIGURED ");
lcd.setCursor(0, 1);
lcd.print(" SETTINGS ");
int progress = 0;
int barWidth = 19;
while (progress <= barWidth) {
lcd.setCursor(progress, 2);
lcd.write(255); // Display a solid block character
delay(150);
progress++;
}
lcd.clear();
}
}
void detect_click() {
menu = digitalRead(MENU);
up = digitalRead(UP);
down = digitalRead(DOWN);
}
void PWM_smoth() {
//PWM smooth start
if (startup == 3) {
Serial.println("Start Smoth Startup");
for (int i = 0; i <= LIMITS[5]; i++ ) {
pwm = map( LIMITS[5], 0, 255, 0, 100);
float Voltage = ( LIMITS[5] / 255) * 5;
analogWrite(PWM, i); //PWM at pin 9
Serial.print("set_pwm_value ");
Serial.print(i);
Serial.print(" pwm_actual_voltage ");
Serial.print(Voltage);
Serial.println(" v");
data_all();
warnings();
detect_click();
setting_routine();
if (!TEMP && !SWR && !REF && !RIN && !FWD) {
//do nothing
} else {
Serial.println("PWM force stop");
analogWrite(PWM, 0);
startup = 3;
break;
}
delay(100);
}
Serial.println("Smoth Startup Done");
}
}
void data_all() {
unsigned long currentMillis = millis(); // Get the current timestamp
// Check if the desired interval has elapsed
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Update the previous timestamp
//counter for startup pwm
if (startup < 4 && !TEMP && !SWR && !REF && !RIN && !FWD) {
startup ++;
if (startup < 3) {
Serial.print("Start up in ");
Serial.println(startup);
}
}
// MAIN DISPLAY FOR TEMPERATURE
ADCvalue = analogRead(temppin);
ADCvalue = ADCvalue / 2;
// FWD, REF, RFIN CALCULATION
forwardPower = analogRead(forwardPin) * 3000.0 / forwardRef; // Calculate forward power in percentage
reflectedPower = analogRead(reflectedPin) * 100.0 / reflectedRef; // Calculate reflected power in percentage
rfinPower = analogRead(rfinPin) * 50.0 / rfinRef; // Calculate reflected power in percentage
// VSWR CALCULATION
vswr = (1 + sqrt(reflectedPower / forwardPower)) / (1 - sqrt(reflectedPower / forwardPower)); // Calculate VSWR
if (!TEMP && !SWR && !REF && !RIN && !FWD) {
//analogWrite(PWM, LIMITS[5]); //PWM at pin 9
lcd.setCursor(10, 3); // top left
lcd.print("TMP: "); // print the value at LCD
//lcd.print(ADCvalue);
formattedFloat = formatFloat(ADCvalue, 0);
lcd.print(formattedFloat);
lcd.write(0xDF); // Display degree symbol
lcd.print("C");
if (ADCvalue <= 99) {
lcd.print(" ");
}
// MAIN DISPLAY FOR VSWR
lcd.setCursor(0, 3); // Set the cursor to the position for VSWR value
lcd.print("SWR:"); // Clear the previous VSWR value
lcd.setCursor(5, 3); // Set the cursor to the position for VSWR value
//lcd.print(vswr); // Display the VSWR value on LCD
formattedFloat = formatFloat(vswr, 1);
lcd.print(formattedFloat);
lcd.print(" ");
// MAIN DISPLAY FOR FWD
lcd.setCursor(15, 0);
lcd.print("WATTS");
lcd.setCursor(0, 0); // Set the cursor to the position for power meter
lcd.print("FWD: "); // Display "F:" for forward power
//lcd.print(forwardPower); // Display forward power on LCD
formattedFloat = formatFloat(forwardPower, 1);
lcd.print(formattedFloat);
lcd.print(" ");
// MAIN DISPLAY FOR REF
lcd.setCursor(15, 1);
lcd.print("WATTS");
lcd.setCursor(0, 1);
lcd.print("REF: "); // Display "R:" for reflected power
//lcd.print(reflectedPower); // Display reflected power on LCD
formattedFloat = formatFloat(reflectedPower, 1);
lcd.print(formattedFloat);
lcd.print(" ");
// MAIN DISPLAY FOR RFIN
lcd.setCursor(15, 2);
lcd.print("WATTS");
lcd.setCursor(0, 2);
lcd.print("PIN: ");
//lcd.print(rfinPower); // Display reflected power on LCD
formattedFloat = formatFloat(rfinPower, 1);
lcd.print(formattedFloat);
lcd.print(" ");
//clearing LCD 1 time only when reset
if (!reset) {
lcd.clear();
reset = true;
}
} else {
//clearing LCD 1 time only when reset
if (reset) {
lcd.clear();
reset = false;
}
}
//delay(500); // Delay for 1 second before next measurement "use millis '"
}
}
//convert float to string to avoid 2 decimal point to fit on screen
String formatFloat(float value, int decimalPlaces) {
char buffer[10]; // Buffer to store the formatted string
dtostrf(value, 6, decimalPlaces, buffer); // Convert float to string with desired decimal places
// Remove leading spaces
int length = strlen(buffer);
int leadingSpaces = 0;
while (buffer[leadingSpaces] == ' ') {
leadingSpaces++;
}
// Return the formatted float value without leading spaces
return String(buffer + leadingSpaces);
}
void warnings() {
/*
forwardPower, reflectedPower, rfinPower, vswr, ADCvalue
//float LIMITS[] = {FWD, REF, RIN, SWR, TEMP};
//float LIMITS[] = {0.0, 0.0, 0.0, 0.0, 0.0};
*/
if (ADCvalue >= LIMITS[4] && enable_disable[4] == 1) {
Serial.println("Warning @ ADCvalue");
Serial.print(ADCvalue);
Serial.print(" ");
Serial.println(LIMITS[4]);
lcd.setCursor(0, 0);
lcd.print("*******ALARM!*******");
lcd.setCursor(0, 1);
lcd.print("*****HIGH TEMP*****");
lcd.setCursor(0, 2);
lcd.print("VALUE: ");
lcd.print(ADCvalue);
lcd.setCursor(0, 3);
lcd.print("LIMIT: ");
lcd.print(LIMITS[4]);
digitalWrite(temp_led_error, HIGH);
TEMP = true;
} else {
TEMP = false;
}
if (vswr >= LIMITS[3] && enable_disable[1] == 1) {
Serial.println("Warning @ vswr");
Serial.print(vswr);
Serial.print(" ");
Serial.println(LIMITS[3]);
lcd.setCursor(0, 0);
lcd.print("*******ALARM!*******");
lcd.setCursor(0, 1);
lcd.print("*****HIGH SWR*****");
lcd.setCursor(0, 2);
lcd.print("VALUE: ");
lcd.print(vswr);
lcd.setCursor(0, 3);
lcd.print("LIMIT: ");
lcd.print(LIMITS[3]);
digitalWrite(swr_led_error, HIGH);
SWR = true;
} else {
SWR = false;
}
if (forwardPower >= LIMITS[0] && enable_disable[0] == 1) {
Serial.println("Warning @ forwardPower");
Serial.print(forwardPower);
Serial.print(" ");
Serial.println(LIMITS[0]);
lcd.setCursor(0, 0);
lcd.print("*******ALARM!*******");
lcd.setCursor(0, 1);
lcd.print("*****HIGH FWD*****");
lcd.setCursor(0, 2);
lcd.print("VALUE: ");
lcd.print(forwardPower);
lcd.setCursor(0, 3);
lcd.print("LIMIT: ");
lcd.print(LIMITS[0]);
FWD = true;
} else {
FWD = false;
}
if (reflectedPower >= LIMITS[1] && enable_disable[2] == 1) {
Serial.println("Warning @ reflectedPower");
Serial.print(reflectedPower);
Serial.print(" ");
Serial.println(LIMITS[1]);
lcd.setCursor(0, 0);
lcd.print("*******ALARM!*******");
lcd.setCursor(0, 1);
lcd.print("*****HIGH REF*****");
lcd.setCursor(0, 2);
lcd.print("VALUE: ");
lcd.print(reflectedPower);
lcd.setCursor(0, 3);
lcd.print("LIMIT: ");
lcd.print(LIMITS[1]);
REF = true;
} else {
REF = false;
}
if (rfinPower >= LIMITS[2] && enable_disable[3] == 1) {
Serial.println("Warning @ rfinPower");
Serial.print(rfinPower);
Serial.print(" ");
Serial.println(LIMITS[2]);
lcd.setCursor(0, 0);
lcd.print("*******ALARM!*******");
lcd.setCursor(0, 1);
lcd.print("*****HIGH PIN*****");
lcd.setCursor(0, 2);
lcd.print("VALUE: ");
lcd.print(rfinPower);
lcd.setCursor(0, 3);
lcd.print("LIMIT: ");
lcd.print(LIMITS[2]);
RIN = true;
} else {
RIN = false;
}
if (!TEMP && !SWR && !REF && !RIN && !FWD) {
digitalWrite(relay, HIGH);
} else {
digitalWrite(relay, LOW);
analogWrite(PWM, 0);
if (!alarm) {
Serial.println("reset pwm to zero");
EEPROM.put(LIMITS[5], 0);
LIMITS[5] = 0;
delay(10);
}
alarm = true;
tone(TonePin, 2000);
delay(100);
noTone(TonePin);
delay(100);
}
}
String ONOFF(int status) {
String ON_OFF;
if (status == 1) {
ON_OFF = "ON ";
} else {
ON_OFF = "OFF";
}
return ON_OFF;
}
FPS: 0
Power: 0.00W
Power: 0.00W