UART-Based LEDs Control

Solving Approach

How do you plan to solve it?

 

 ┌──────────────┐ UART TX/RX ┌──────────────┐
│ Microcontroller 1         │ Microcontroller 2
│                            │
│ Potentiometer → ADC        │ UART RX (ADC value)
│ LED1 (ON/OFF)              │ LED2 (PWM brightness)
│ UART RX (button command)   │ Button
│ Serial prints LED1 status  │ Serial prints ADC value
└──────────────┘             └──────────────┘
 

 

 

Code

/*Paste your code here*/
#include <Arduino.h>
#include <string.h>

/* ================= ROLE SELECT ================= */
// #define MCU1    // Pot + LED1
#define MCU2  // LED2 + Button

/* ================= COMMON CONFIG ================= */
#define BAUDRATE 9600
#define RX_BUFFER_SIZE 32

char rxBuffer[RX_BUFFER_SIZE];
uint8_t rxIndex = 0;
bool led1State = false;


/* ================= COMMON UART RX ================= */
void uartRxTask(void);
void handleCommand(const char *cmd);

/* ================= SETUP ================= */
void setup() {
  Serial.begin(BAUDRATE);

#ifdef MCU1
  pinMode(4, OUTPUT);
#endif

#ifdef MCU2
  pinMode(5, OUTPUT);
  pinMode(7, INPUT_PULLUP);
#endif
}

/* ================= LOOP ================= */
void loop() {
  uartRxTask();

#ifdef MCU1
  mcu1_task();
#endif

#ifdef MCU2
  mcu2_task();
#endif
}

/* ================= UART RX ================= */
void uartRxTask(void) {
  while (Serial.available()) {
    char ch = Serial.read();

    if (ch == '\n' || ch == '\r') {
      if (rxIndex > 0) {
        rxBuffer[rxIndex] = '\0';
        handleCommand(rxBuffer);
        rxIndex = 0;
      }
    } else if (rxIndex < RX_BUFFER_SIZE - 1) {
      rxBuffer[rxIndex++] = ch;
    }
  }
}

void handleCommand(const char *cmd) {

#ifdef MCU2
  /* MCU2 receives ADC value */
  if (strncmp(cmd, "ADC=", 4) == 0) {
    int adc = atoi(cmd + 4);
    int pwm = map(adc, 0, 1023, 0, 255);
    analogWrite(5, pwm);

    Serial.print("Received ADC: ");
    Serial.println(adc);
    return;
  }
#endif

#ifdef MCU1
  /* MCU1 receives toggle command */
  if (strcmp(cmd, "LED1=ON") == 0) 
  {
    led1State = 1;
    digitalWrite(4, led1State);

    Serial.print("LED1 ");
    Serial.println("ON");
  }
  else if (strcmp(cmd, "LED1=OFF") == 0) 
  {
    led1State = 0;
    digitalWrite(4, led1State);

    Serial.print("LED1 ");
    Serial.println("OFF");
  }

#endif
}

#ifdef MCU1

#define POT_PIN A0
#define LED1_PIN 4

void mcu1_task(void) {
  static unsigned long lastSend = 0;

  if (millis() - lastSend > 200) {
    int adc = analogRead(POT_PIN);

    Serial.print("ADC=");
    Serial.println(adc);

    lastSend = millis();
  }
}

#endif

#ifdef MCU2

#define LED2_PIN 5
#define BUTTON_PIN 7

// bool lastButtonState = HIGH;

void mcu2_task(void) 
{
  bool buttonState = digitalRead(BUTTON_PIN);
  delay(200); // debounce
  buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == HIGH) 
  {
    Serial.println("LED1=OFF");
    delay(200); // debounce
  }
  if (buttonState == LOW) 
  {
    Serial.println("LED1=ON");
    delay(200); // debounce
  }

  // lastButtonState = buttonState;
}

#endif

 

Output

Video

Add a video of the output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

Screenshot of Serial Terminal 

Add a Screenshot of the serial terminal showing the output.


 

Upvote
Downvote

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!