// Transmitter code (Arduino UNO to Arduino UNO communication)
// this version includes the use of potentiometer to set the transmission bitrate (bps) in the setup funtion (to change the bitrate, reset the MCU )
// The bitrate is selected by adjusting the potentiometer and pressing the pushbutton to exit WHILE loop
// In the LOOP function the pushbutton is used to restart transmission (resend message)
// The receiver part can be updated with pushbutton, that resets receiver (ground the reset pin) to wait for new incoming message
#include <LiquidCrystal.h>
// Pin assignments
#define buttonPin 2
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define LCD_RS 8
#define LCD_EN 9
// Not needed in manchester encoding !!!
//#define TX_CLOCK 10
#define TX_DATA 11
// Transmit rate in bps
//#define TX_RATE 5
int TX_RATE;
// Initialize the LCD screen
bool buttonPressed = false; // Flag to indicate button press
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
const char *message = "Hello, world!";
void setup() {
//pinMode(TX_CLOCK, OUTPUT); !!!
pinMode(TX_DATA, OUTPUT);
const int analogPin = A5; // Analog pin for the potentiometer to change tranismit bitrate continuously -> Arduino RESET REQUIRED to update bitrate value
bool buttonPressed = false; // Flag to indicate button press
int Voltage;
// Initialize the LCD screen
lcd.begin(16, 2);
lcd.setCursor(0, 0); // move LCD cursor to column 0, row 0 [upper left position (0, 0)]
lcd.print("Select bitrate"); // print message
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as the input with internal pull-up resistor
// Button polling loop in setup()
Serial.println("Adjust transmit bitrate value using the potentiometer");
while (!buttonPressed) {
Voltage = analogRead(analogPin); //analog voltage reading
delay(100); //a short delay for stability, adjust as needed
TX_RATE = map(Voltage, 0, 1024, 1, 20); // set transmit bitrate in the range from 1 to 20 bps
Serial.print("Transmit bitrate (bps): ");
Serial.println(TX_RATE);
lcd.setCursor(0, 1); // move LCD cursor to column 0, row 0 [upper left position (0, 0)]
lcd.print(TX_RATE); // print message
lcd.print(" bps"); // print message
if (digitalRead(buttonPin) == LOW) { // Check if the button is pressed
buttonPressed = true;
delay(300); // Debounce delay
lcd.clear();
}
delay(100); // Small delay to avoid continuous checking
}
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // Check if the button is pressed
delay(300); // Debounce delay
send_message();
}
}
void send_message() {
lcd.setCursor(0, 0);
lcd.print("Sending: "); // Clear any old message, maintain space
// Loop through each byte of the message
for (int byte_idx = 0; byte_idx < strlen(message); byte_idx++) {
char tx_byte = message[byte_idx]; // Extract each byte
// Show current byte being sent on the first line of LCD
lcd.setCursor(9, 0); // Position on the right for clarity
lcd.write(tx_byte); // Show character being sent
// Loop through each bit for Manchester encoding
for (int bit_idx = 0; bit_idx < 8; bit_idx++) {
bool tx_bit = tx_byte & (0x80 >> bit_idx); // Extract each bit
// Send the Manchester-encoded version of the current bit
// Note: Here we're assuming HIGH then LOW for '1', and LOW then HIGH for '0'
digitalWrite(TX_DATA, tx_bit ? HIGH : LOW); // First phase
delay((1000 / TX_RATE) / 2); // Half period
digitalWrite(TX_DATA, tx_bit ? LOW : HIGH); // Second phase
delay((1000 / TX_RATE) / 2); // Half period
// Clear the second line of the display for new data
if (bit_idx == 0) { // Only clear once at the start of each byte
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous data
}
// Update the LCD with Manchester encoded bits
lcd.setCursor(bit_idx * 2, 1); // Move cursor for each Manchester bit
lcd.print(tx_bit ? "10" : "01"); // Print Manchester encoding
// Delay here just for visualization, adjust as needed or remove for faster execution
}
// Additional delay between bytes for readability, adjust as needed
delay(1000);
}
digitalWrite(TX_DATA, LOW); // Ensure LED is off after sending
lcd.setCursor(0, 0); // Reset cursor position or clear screen as needed after sending
}