1

I am trying to read input from the serial monitor on my Arduino UNO using the new Arduino IDE 2.0 and send a specific infrared signal depending on the command.

I do this using the following code:

#include <Arduino.h>

/*
 * Define macros for input and output pin etc.
 */
#include "PinDefinitionsAndMore.h"
#include <IRremote.h>

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);

    Serial.begin(115200);
    while(!Serial) ;

    // Just to know which program is running on my Arduino
    Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));

    /*
     * The IR library setup. That's all!
     */
    IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK); // Specify send pin and enable feedback LED at default feedback LED pin

    Serial.print(F("Ready to send IR signals at pin "));
    Serial.println(IR_SEND_PIN);
    Serial.println("Commands: start, stop, links, rechts, heim, max");
}

/*
 * Set up the data to be sent.
 * For most protocols, the data is build up with a constant 8 (or 16 byte) address
 * and a variable 8 bit command.
 * There are exceptions like Sony and Denon, which have 5 bit address.
 */
uint16_t sAddress = 0x5540;
uint8_t sCommand_Start_Stop = 0x44;
uint8_t sCommand_Left = 0x44;
uint8_t sCommand_Right = 0x44;
uint8_t sCommand_Home = 0x44;
uint8_t sCommand_Max = 0x44;

uint8_t sRepeats = 1;

bool is_running = false;
String command;

void loop() {
  if(Serial.available()){
        command = Serial.readStringUntil('\n');

        Serial.print(F("Command received: "));
        Serial.println(command);

        if(command.equals("start")){
            sendIRCommand(sAddress, sCommand_Start_Stop);
            is_running = 1;
        } else if(command.equals("stop")){
            sendIRCommand(sAddress, sCommand_Start_Stop);
            is_running = 0;
        } else if(command.equals("links")){
            sendIRCommand(sAddress, sCommand_Left);
        } else if(command.equals("rechts")){
            sendIRCommand(sAddress, sCommand_Right);
        } else if(command.equals("heim")){
            sendIRCommand(sAddress, sCommand_Home);
        } else if(command.equals("max")){
            sendIRCommand(sAddress, sCommand_Max);
        } else{
            Serial.println("Invalid command!");
        }
    }
}

void sendIRCommand(int irAddress, int irCommand) {
  /*
     * Print current send values
     */
    Serial.println();
    Serial.print(F("Send now: address=0x"));
    Serial.print(irAddress, HEX);
    Serial.print(F(" command=0x"));
    Serial.print(irCommand, HEX);
    Serial.println();

    Serial.println(F("Send NEC with 16 bit address"));
    Serial.flush();

    IrSender.sendNEC(irAddress, irCommand, sRepeats);
}

In the process I encounter the problem that the serial monitor constantly reads unknown characters as alleged inputs, although I don't send anything. Because of the two unknown characters, even the proper commands can't be recognized correctly, which is a big problem. Here is the extract of the serial monitor:

15:53:42.987 -> START c:\PATH\TO\SCRIPT\command-sender.ino from Aug 16 2021
15:53:45.093 -> Using library version 3.1.1
15:53:45.094 -> Ready to send IR signals at pin 3
15:53:45.096 -> Commands: start, stop, links, rechts, heim, max
15:53:45.100 -> Command received: ��
15:53:47.107 -> Invalid command!
15:53:47.107 -> Command received: ��
15:53:49.122 -> Invalid comma��
15:53:49.122 -> Command received: ��
15:53:50.113 -> Invalid command!
15:53:50.117 -> Command received: ��
15:53:52.131 -> Invalid command!
15:53:52.131 -> Command received: ��
15:53:54.131 -> Invalid command!
15:53:54.131 -> Command received: ��
15:53:56.142 -> Invalid command!
15:53:56.142 -> Command received: ��
15:53:57.142 -> Invalid co+���Command received: ��
15:53:58.147 -> Invalid command!
15:53:58.147 -> Command received: ��start
15:53:58.932 -> Invalid command!
15:53:58.933 -> Command received: ��
15:54:00.950 -> Invalid command!

I checked the baudrate, use Serial.print and wait in the setup until the serial is initialized before I send data. I use 115200baud in the serial monitor in the IDE as well as New Line. All without success. How can I fix this problem?

Here is the URL for the PinDefinitionsAndMore.h if this is the root of the problem.

Update

When using VisualStudio Code with the PlatformIO extension, sending the commands works just fine. Here the unknown characters don't show up neither. It almost seems like the problem is related to the Arduino IDE 2.0 in some way.

9
  • did you wired something to pin 0 or 1?
    – Juraj
    Aug 16 at 14:19
  • @Juraj No, pin 0 + 1 are not used. There is also no physical connection of the two pins on the back of the board. Pin 2 + 3 are used for sensors
    – JohnDizzle
    Aug 16 at 14:32
  • Can you please replace Serial.println(command); by Serial.println(command, HEX); and then post the serial output again. That should at least show us, what data is actually received.
    – chrisl
    Aug 16 at 16:17
  • @chrisl, Serial.println(command, HEX)???
    – Juraj
    Aug 16 at 16:53
  • 1
    Mhh, then this seems to be a problem with what is send by the Arduino IDE 2.0 through serial (it should not send all high bytes with normal text). Since it is still beta, it might be a bug.
    – chrisl
    Aug 17 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.