#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <Encoder.h>
#include <Bounce2.h>
// LCD and RS-232 configurations
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address, 20 columns, 4 rows
SoftwareSerial rs232(2, 3); // RX, TX pins for RS-232 communication
// Rotary encoder setup
Encoder myEncoder(4, 5); // Rotary encoder pins (A and B)
// Baud rates for RS-232 communication
int baudRates[] = {9600, 1200, 2400, 4800, 19200, 38400, 57600, 115200};
int selectedBaudRate = 0; // Default to 9600 baud
// Global variable to store the current mode
int currentMode = 0; // 0 = Send mode, 1 = Receive mode
// Push button setup using the Bounce2 library
const int buttonPin = 6; // Use pin 6 for the push button
Bounce debouncer = Bounce(); // Create a Bounce object
bool buttonPressed = false; // Track button press state
void setup() {
Serial.begin(9600);
rs232.begin(baudRates[selectedBaudRate]);
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Initialize the push button with debouncing
pinMode(buttonPin, INPUT_PULLUP);
debouncer.attach(buttonPin);
debouncer.interval(50); // Debounce interval in milliseconds
}
void loop() {
// Update the debouncer
debouncer.update();
// Check if the button was pressed and released
if (debouncer.fell()) {
buttonPressed = true;
}
// Check if the button was released after being pressed
if (debouncer.rose() && buttonPressed) {
// Toggle between Send and Receive modes
currentMode = 1 - currentMode;
displayMode();
buttonPressed = false; // Reset the button press state
}
// Perform actions based on the current mode
if (currentMode == 0) {
sendMode();
} else if (currentMode == 1) {
receiveMode();
}
}
// Send mode: send out the phrase "Testing Testing 123456"
void sendMode() {
lcd.home();
lcd.println("Send Mode");
delay(1500); // Display mode for 1500 ms
lcd.clear();
lcd.home();
lcd.print("Testing");
delay(500);
lcd.clear();
delay(200);
Serial.flush();
Serial.println("Testing");
Serial.flush();
delay(500);
}
// Receive mode: read data from the serial port and print it to the LCD
void receiveMode() {
lcd.home();
lcd.println("Receive Mode");
delay(1500); // Display mode for 1500 ms
lcd.clear();
if (Serial.available()) {
char incomingData = Serial.read();
lcd.home();
lcd.println(incomingData);
Serial.flush();
delay(500);
Serial.println(incomingData);
Serial.flush();
delay(500);
} else {
lcd.home();
lcd.println("Waiting");
delay(500);
lcd.clear();
delay(200);
Serial.flush();
delay(500);
Serial.println("Waiting");
Serial.flush();
delay(500);
}
}
void displayMode() {
lcd.home();
if (currentMode == 0) {
lcd.println("Send Mode");
} else if (currentMode == 1) {
lcd.println("Receive Mode");
}
delay(1500); // Display mode for 1500 ms
lcd.clear();
}