#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 5
#define data 6 // Connect to BH1415 Data Pin
#define clk 7 // Connect to BH1415 Clock Pin
#define ena 8 // Connect to BH1415 Chip Enable (CE) Pin
#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 temppin = A7; // Temperature sensor pin
const int VU_meter = A2; // 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 = 500; // Delay interval in milliseconds
const int toneDuration = 500;
const int toneInterval = 1000;
unsigned long prevTone = 0;
boolean tonePlaying = false;
boolean freq_change = false;
String mode_stats = "Mod:ST";
bool FWD = false;
bool REF = false;
bool RIN = false;
bool SWR = false;
bool TEMP = false;
bool reset = true;
bool alarm = false;
bool MD_change = true;
unsigned int st_mn = 9; // Start with mono mode (8) and (9) for stereo
unsigned int frequency = 891; // Start with frequency 101.1 MHz
unsigned short st = 0; // Mono stereo control
unsigned int mask = 0; // Bit selection control for sending to BH1415
byte G[8] = {
B11111,
B00000,
B11111,
B11111,
B11111,
B11111,
B00000,
B11111
};
byte T[8] = {
B11000,
B11100,
B11110,
B11111,
B11111,
B11110,
B11100,
B11000
};
//---------------------------------------------
void setFrequency() // Function to send bits to BH1415
{
digitalWrite(ena, HIGH); // Chip Enable HIGH
delayMicroseconds(10); // Initial delay after chip enable
for (int n = 0; n < 11; n++) // Send frequency bits
{
mask = (1 << n); // MASK STARTS WITH 00000000000, EACH TIME THROUGH THE "FOR" LOOP, IT ADDS 1 BIT TO THE LEFT, 0000000001, 0000000010, 0000000100, ETC.
// Select each bit to send
if ((frequency & mask) != 0) // FREQUENCY IN BINARY 100.8 = 1111110000
{
// If the bit is 1, activate the DATA output, and the chip interprets it as 1
digitalWrite(data, HIGH); // MASK = 0000000001
}
else
{
// If the bit is 0, deactivate the DATA output, and the chip interprets it as 0
digitalWrite(data, LOW);
}
send();
}
for (int n = 0; n <= 4; n++) // Send controls
{
mask = (1 << n); // Select each bit to send
//Serial.println(mask);
if (st_mn & mask) // If the bit is 0
{
digitalWrite(data, HIGH); // Activate the DATA output
}
else
{
digitalWrite(data, LOW); // Deactivate the DATA output
}
send();
}
digitalWrite(ena, LOW); // Chip Enable LOW
}
//--------------------------------------------------------------------------------------
void send() // Send each bit
{
delayMicroseconds(10); // Wait for 10 microseconds
digitalWrite(clk, HIGH); // Activate the clock output
delayMicroseconds(10); // Wait for 10 microseconds
digitalWrite(clk, LOW); // Deactivate the clock output
delayMicroseconds(10); // Wait for 10 microseconds
}
//---------------------------------------------
void displayVolume() {
int sensorValue = analogRead(VU_meter);
int volume = map(sensorValue, 0, 1023, 3, 19);
lcd.setCursor(0, 3);
lcd.print("VU ");
lcd.setCursor(volume + 1, 3);
for (int i = volume; i < 19; i++) {
lcd.write(byte(0));
}
lcd.setCursor(3, 3);
for (int i = 3; i < volume; i++) {
if (i != volume - 1) {
lcd.write(1);
} else {
lcd.write(2);
}
}
}
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]);
EEPROM.put(165, st);
// 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]);
EEPROM.get(165, st);
// 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.createChar(1, G);
lcd.createChar(2, T);
lcd.setCursor(0, 0);
lcd.print(" CJTECH ELECTRONICS ");
lcd.setCursor(0, 1);
lcd.print(" BROADCASTING ");
lcd.setCursor(0, 2);
lcd.print(" SERVICES ");
lcd.setCursor(0, 3);
lcd.print(" 06/03/23 Ver. 4.0 ");
pinMode(TonePin, OUTPUT);
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay(150);
tone(TonePin, 2000);
delay(150);
noTone(TonePin);
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
int progress = 0;
int barWidth = 19;
// Filling the progress bar
lcd.setCursor(3, 1);
lcd.print("Initializing...");
while (progress <= barWidth) {
lcd.setCursor(progress, 2);
lcd.write(255); // Display a solid block character
delay(30);
progress++;
}
// Clearing the progress bar
while (progress >= 0) {
lcd.setCursor(progress, 2);
lcd.print(" ");
delay(30);
progress--;
}
lcd.clear();
// Additional code for your sketch after the progress bar is done:
lcd.setCursor(0, 1);
lcd.print("Initialization done!");
delay(2000);
lcd.clear();
int datas;
EEPROM.get(1, datas);
if (datas == -1) {
frequency = 891;
EEPROM.put(1, frequency);
Serial.println("initail");
}
Serial.println(datas);
EEPROM.get(1, frequency);
pinMode(data, OUTPUT); // Data port as output
pinMode(clk, OUTPUT); // Clock port as output
pinMode(ena, OUTPUT); // Chip enable port as output
delay(200); // Wait for initialization
setFrequency(); // Send initial settings
delay(200); // Wait for the chip to configure
setFrequency(); // Send settings
delay(200); // Wait for the chip to configure
// lcd.setCursor(14, 0);
// lcd.print(mode_stats);
}
void loop() {
PWM_smoth();
data_all(); //where data is collected
detect_click(); //detect any click in buttons
setting_routine(); //go to settings
warnings();
displayVolume(); //volume unit
float fre = frequency;
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
lcd.print("Fre:");
lcd.print(fre / 10); // Display the frequency on the LCD
lcd.print("Mhz");
//moved to menu
//while (digitalRead(MENU) == LOW) {
if (MD_change) {
//st = ~st; // Set to zero, this ~ signal complements 1 // Change mono stereo condition
Serial.print("ST = : ");
Serial.println(st);
if (st == 0) {
setFrequency();
st_mn = 9; // treat as mono
delay(100);
setFrequency();
delay(200);
setFrequency();
delay(200);
lcd.setCursor(14, 0);
mode_stats = "Mod:ST";
lcd.print(mode_stats);
} else {
setFrequency();
lcd.setCursor(14, 0);
mode_stats = "Mod:MO";
lcd.print(mode_stats);
st_mn = 8; // treat as stereo
setFrequency(); // Send setting
delay(200); // Wait for the chip to configure
setFrequency(); // Send setting
delay(200); // Wait for the chip to configure
}
MD_change = false;
}
// else {
// eeprom_update();
// }
// }
if (digitalRead(UP) == LOW)
{
delay(20); // Debouncing
if (digitalRead(UP) == LOW)
{
digitalWrite(relay, LOW);
setFrequency();
frequency++;
if (frequency > 1080)
frequency = 880;
setFrequency();
delay(200);
setFrequency();
delay(200);
}
freq_change = true;
}
if (digitalRead(DOWN) == LOW)
{
delay(20); // Debouncing
if (digitalRead(DOWN) == LOW)
{
digitalWrite(relay, LOW);
frequency--;
if (frequency < 880)
frequency = 1080;
setFrequency();
delay(200);
setFrequency();
delay(200);
}
freq_change = true;
}
}
void eeprom_update() {
lcd.clear();
lcd.setCursor(4, 2);
lcd.print("Freq saved !!");
EEPROM.put(1, frequency);
Serial.println("EEPROM updated");
freq_change = false;
digitalWrite(relay, HIGH);
delay(1000);
while (1) {
int i = digitalRead(MENU);
if (i) {
lcd.clear();
setFrequency();
lcd.setCursor(14, 0);
lcd.print(mode_stats);
return;
}
}
// Keep relay turned on here.
digitalWrite(relay, HIGH);
}
void setting_routine() {
if (!menu) {
delay(1000);
lcd.clear();
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay(150);
noTone (TonePin);
int Switch = 1;
while (1) {
startup = 0; //return to 0 to do again smoth startup after exit menu
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 = 10;
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
delay(100);
} else {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
Switch --;
delay(100);
}
}
if (!down) {
lcd.clear();
if (Switch >= 10) {
Switch = 1;
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
delay(100);
} else {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
Switch ++;
delay(100);
}
}
Serial.println("Switch = " + String(Switch));
switch (Switch) {
case 1:
lcd.setCursor(4, 1);
lcd.print("FORWARD POWER");
formattedFloat = formatFloat(LIMITS[0], 0);
lcd.setCursor(0, 2);
lcd.print("CURRENT LIMIT:");
lcd.setCursor(16, 2);
lcd.print(formattedFloat);
break;
case 2:
lcd.setCursor(3, 1);
lcd.print("REFLECTED POWER");
formattedFloat = formatFloat(LIMITS[1], 0);
lcd.setCursor(0, 2);
lcd.print("CURRENT LIMIT:");
lcd.setCursor(16, 2);
lcd.print(formattedFloat);
break;
case 3:
lcd.setCursor(5, 1);
lcd.print("AUDIO MOD");
formattedFloat = formatFloat(LIMITS[2], 0);
lcd.setCursor(4, 2);
lcd.print("CURRENT MOD:");
lcd.setCursor(16, 2);
lcd.print(formattedFloat);
break;
case 4:
lcd.setCursor(0, 1);
lcd.print("STANDING WAVE RATIO");
formattedFloat = formatFloat(LIMITS[3], 1);
lcd.setCursor(1, 2);
lcd.print("CURRENT LIMIT:");
lcd.setCursor(16, 2);
lcd.print(formattedFloat);
break;
case 5:
lcd.setCursor(1, 1);
lcd.print("TEMPERATURE LIMIT");
formattedFloat = formatFloat(LIMITS[4], 0);
lcd.setCursor(1, 2);
lcd.print("CURRENT LIMIT:");
lcd.setCursor(16, 2);
lcd.print(formattedFloat);
break;
case 6:
//exit menu here
lcd.setCursor(0, 1);
lcd.print("PRESS ENTER TO EXIT");
break;
case 7:
pwm = map(LIMITS[5], 0, 255, 0, 100);
Serial.print(LIMITS[5]);
Serial.print(" ");
Serial.println(pwm);
lcd.setCursor(4, 1);
lcd.print("DRIVE POWER");
formattedFloat = formatFloat(pwm, 0);
lcd.setCursor(1, 2);
lcd.print("CURRENT DRIVE:");
lcd.setCursor(16, 2);
lcd.print(formattedFloat);
lcd.print("%");
break;
case 8:
pwm = map(LIMITS[5], 0, 255, 0, 100);
Serial.print(LIMITS[5]);
lcd.setCursor(4, 1);
lcd.print("ON/OFF Alarm ");
lcd.setCursor(1, 2);
lcd.print("PLEASE PRESS ENTER");
break;
case 9:
//st
//Serial.print("st");
lcd.setCursor(4, 1);
lcd.print("MONO / STEREO");
lcd.setCursor(1, 2);
if (st) {
lcd.print("mode stats: Mod:MO");
} else {
lcd.print("mode stats: Mod:ST");
}
lcd.setCursor(1, 3);
lcd.print("PLEASE PRESS ENTER");
break;
case 10:
//Serial.print("freq");
lcd.setCursor(6, 1);
lcd.print("Frequency");
lcd.setCursor(1, 2);
lcd.print("Frequency :" + String(frequency) + "Mhz");
break;
} //switch end
//exit menu
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] );
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) {
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);
}
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;
}
}
}
if (Switch == 9 && !menu) {
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
detect_click();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Mode Stats");
delay(500);
Serial.println("go to st menu");
while (1) {
detect_click();
if (!menu) {
tone(TonePin, 1200);
delay(150);
tone(TonePin, 1600);
delay (150);
tone(TonePin, 2000);
delay (150);
noTone (TonePin);
lcd.clear();
break;
}
if (!up || !down) {
st = ~st;
delay (200);
}
lcd.setCursor(0, 2);
lcd.print("Mode : ");
if (st) {
lcd.print("Mod:MO");
} else {
lcd.print("Mod:ST");
}
}
}
}
MD_change = true;
//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]);
EEPROM.put(165, st);
// for (int i = 0; i <= arraylenght ; i ++) {
// Serial.println(LIMITS[i]);
// }
lcd.clear();
// Progress bar graph
int progress = 0;
int barWidth = 19;
// Filling the progress bar
lcd.setCursor(2, 1);
lcd.print("Saving Settings!");
while (progress <= barWidth) {
lcd.setCursor(progress, 2);
lcd.write(255); // Display a solid block character
delay(30);
progress++;
}
// Clearing the progress bar
while (progress >= 0) {
lcd.setCursor(progress, 2);
lcd.print(" ");
delay(30);
progress--;
}
lcd.clear();
// Additional code for your sketch after the progress bar is done:
lcd.setCursor(2, 1);
lcd.print("Saving Settings!");
lcd.setCursor(7, 2);
lcd.print("Done!");
delay(2000);
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) * 1000.0 / forwardRef; // Calculate forward power in percentage
reflectedPower = analogRead(reflectedPin) * 100.0 / reflectedRef; // Calculate reflected power in percentage
// VSWR CALCULATION
vswr = (1 + sqrt(reflectedPower / forwardPower)) / (1 - sqrt(reflectedPower / forwardPower)); // Calculate VSWR
if (!TEMP && !SWR && !REF && !RIN && !FWD) {
// MAIN DISPLAY FOR TMP
lcd.setCursor(11, 1); // top left
lcd.print("Tep:"); // print the value at LCD
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(11, 2); // Set the cursor to the position for VSWR value
lcd.print("SWR:"); // Clear the previous VSWR value
lcd.setCursor(16, 2); // Set the cursor to the position for VSWR value
formattedFloat = formatFloat(vswr, 1);
lcd.print(formattedFloat);
lcd.print(" ");
// MAIN DISPLAY FOR FWD
lcd.setCursor(0, 1); // Set the cursor to the position for power meter
lcd.print("Pow:"); // Display "F:" for forward power
formattedFloat = formatFloat(forwardPower, 1);
lcd.print(formattedFloat);
lcd.print(" ");
// MAIN DISPLAY FOR REF
lcd.setCursor(0, 2);
lcd.print("Ref:"); // Display "R:" for reflected power
formattedFloat = formatFloat(reflectedPower, 1);
lcd.print(formattedFloat);
lcd.print(" ");
if (!reset) {
lcd.clear();
reset = true;
}
} else {
//clearing LCD 1 time only when reset
if (reset) {
lcd.clear();
reset = false;
}
}
}
}
//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. protect!!");
lcd.setCursor(0, 2);
lcd.print("Current Value: ");
lcd.print(ADCvalue);
lcd.setCursor(0, 3);
lcd.print("Limit Value: ");
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 VSWR. protect!!");
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 Forward Power!");
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 Reflected Power");
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 Input Power prt");
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