Using the built in serial connection

Discussion in 'UDOO KEY' started by Logan Larsen, Jun 2, 2024.

  1. Logan Larsen

    Logan Larsen UDOOer

    Joined:
    Jun 2, 2024
    Messages:
    1
    Likes Received:
    0
    Hello! I am trying to build a Bluetooth media controller to connect to my phone while in the car and have buttons for skip, volume, etc...
    I am having trouble getting the internal serial connection between the two boards working. I tried on my own, and am now getting help from the google AI chat thing, with the same results. I'll post the code here, what am I doing wrong?

    I have very limited knowledge of programming these things, I'm just finding sample code from other places and pasting in what I think will work. Yes I know there's an rp2 w, but I don't have one of those and do have one of these from when it was like $9.

    for the rp2040
    Code:
    #include <Arduino.h>
    
    const int ledPin = 25; // LED pin
    const int buttonPin = 27; // Pushbutton pin
    const int baudRate = 9600; // Match ESP32 baud rate
    
    int ledState = LOW; // Initial LED state (off)
    bool buttonPressed = false; // Flag to track button press
    
    void setup() {
      Serial.begin(baudRate); // Default serial for communication
      pinMode(ledPin, OUTPUT); // Set LED pin as output
      pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
    }
    
    void loop() {
      // Check for button press (active low with pull-up)
      buttonPressed = !digitalRead(buttonPin);
      if (buttonPressed && !digitalRead(buttonPin)) { // Debounce (check twice for stable press)
        String message = "RP2040: Toggle LED"; // Message with identifier
        Serial.println(message); // Send message to ESP32
        ledState = !ledState; // Toggle LED state
        digitalWrite(ledPin, ledState); // Update LED
        delay(100); // Short delay to debounce button further (optional)
      }
    }
    
    ESP32
    Code:
    const int ledPin = 32; // Blue LED pin
    const int baudRate = 9600; // Adjust if needed
    
    int ledState = LOW; // Initial LED state (off)
    
    void setup() {
      Serial.begin(baudRate); // Default serial for communication with your computer (optional)
      pinMode(ledPin, OUTPUT); // Set LED pin as output
    }
    
    void loop() {
      if (Serial.available()) {
        String message = Serial.readStringUntil('\n');
        Serial.println(message); // Print received message for debugging (optional)
        ledState = !ledState; // Toggle LED state
        digitalWrite(ledPin, ledState); // Update LED
      }
    }
    
     
  2. Jack Jokky

    Jack Jokky UDOOer

    Joined:
    Oct 22, 2024
    Messages:
    4
    Likes Received:
    0

    Have you thought about adding more features, like a play/pause button or even voice commands? That could really take it to the next level! Also, don’t stress too much about the coding challenges—everyone starts somewhere, and troubleshooting is part of the fun. It’s like a puzzle waiting to be solved! Can’t wait to see how it turns out.
     

Share This Page