// 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
#define TX_PIN 11
// Transmit rate in bps
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!";
byte xmit;
void setup() {
//man.setupTransmit(TX_PIN);
pinMode(TX_PIN, OUTPUT);
const long analogPin = A5; // Analog pin for the potentiometer to change tranismit bitrate continuously -> Arduino RESET REQUIRED to update bitrate value
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, 1023, 1, 10); // 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
//man.transmit(message);
send_message();
}
}
void send_message() {
lcd.clear(); // Clear the LCD at the beginning
lcd.setCursor(0, 0);
lcd.print(message); // Display the message on the first line
unsigned long xmitdly = 500000UL / TX_RATE; // Recalculate delay based on potentially updated TX_RATE
for (int byte_idx = 0; byte_idx < strlen(message); byte_idx++) {
xmit = message[byte_idx];
// Highlight the current character being sent with an underscore
lcd.setCursor(byte_idx, 0); // Move to the character position
lcd.write('_'); // Place marker under the character
digitalWrite(TX_PIN, LOW);
delayMicroseconds(xmitdly);
byte bitv = 128;
// Clear the second line of the display for Manchester encoding
lcd.setCursor(0, 1);
lcd.print(" "); // Clear by printing spaces
lcd.setCursor(0, 1); // Reset cursor position for encoding display
for (int bit_idx = 0; bit_idx < 8; bit_idx++) {
lcd.setCursor(bit_idx * 2, 1); // Position for each bit's Manchester encoding
if (xmit & bitv) {
digitalWrite(TX_PIN, HIGH);
lcd.print("10");
} else {
digitalWrite(TX_PIN, LOW);
lcd.print("01");
}
delayMicroseconds(xmitdly);
digitalWrite(TX_PIN, !digitalRead(TX_PIN)); // Toggle for next part of Manchester bit
delayMicroseconds(xmitdly);
bitv = bitv >> 1; // Move to the next bit
}
// Remove the marker under the current character after it's been sent
lcd.setCursor(byte_idx, 0); // Move back to the character position
lcd.write(message[byte_idx]); // Redraw the original character
digitalWrite(TX_PIN, LOW);
}
}