I need help to merge IR and serial both

Hi everyone,

I've encountered an issue while trying to merge two codes from the internet—one for IR and the other for serial communication. Individually, both codes work perfectly fine, but when I attempt to combine them, the serial code stops responding.

Here's what I'm trying to achieve:

#include <SoftwareSerial.h>
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 2
String device;
bool dev1st,dev2st,dev3st,dev4st,dev5st,dev6st;
int currentspeed;

// Define IR remote codes for each speed setting
#define code1 0xDA25FBE2 // IR code for speed 0 (dimmer level 0)
#define code2 0xD22DFBE2 // IR code for speed 1 (dimmer level 40)
#define code3 0xD926FBE2 // IR code for speed 2 (dimmer level 80)
#define code4 0xF40BFBE2 // IR code for speed 3 (dimmer level 120)
#define code5 0xF50AFBE2 // IR code for speed 4 (dimmer level 160)
#define code6 0xF00FFBE2 // IR code for speed 5 (dimmer level 255)
#define code7 0xF30CFBE2 // IR code for Device 1

const int dev1Pin = 7; // D7 for device 1
const int dev2Pin = 8; // D8 for device 2
const int dimmer1 = 9;  // D9 for Dimmer
int pwmValue; // Declare pwmValue globally without initialization

SoftwareSerial mySerial(0, 1); // RX, TX - Use pins 0 and 1 for SoftwareSerial

void setup() {
    pinMode(dev1Pin, OUTPUT);
    pinMode(dev2Pin, OUTPUT);
    pinMode(dimmer1, OUTPUT);
    Serial.begin(115200); // Set baud rate for Serial communication
    mySerial.begin(115200); // Set baud rate for SoftwareSerial
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
    Serial.println("System Initialized");
}

void loop() {
    if (IrReceiver.decode()) {

    // Check which IR code was received and adjust dimmer accordingly
    if (IrReceiver.decodedIRData.decodedRawData == code1) {
      analogWrite(dimmer1, 0); // Set dimmer level to 0
      Serial.println("Dimmer set to level 0");
    } else if (IrReceiver.decodedIRData.decodedRawData == code2) {
      analogWrite(dimmer1, 40); // Set dimmer level to 40
      Serial.println("Dimmer set to level 40");
    } else if (IrReceiver.decodedIRData.decodedRawData == code3) {
      analogWrite(dimmer1, 80); // Set dimmer level to 80
      Serial.println("Dimmer set to level 80");
    } else if (IrReceiver.decodedIRData.decodedRawData == code4) {
      analogWrite(dimmer1, 120); // Set dimmer level to 120
      Serial.println("Dimmer set to level 120");
    }else if (IrReceiver.decodedIRData.decodedRawData == code5) {
      analogWrite(dimmer1, 160); // Set dimmer level to 160
      Serial.println("Dimmer set to level 160");
    }else if (IrReceiver.decodedIRData.decodedRawData == code6) {
      analogWrite(dimmer1, 255); // Set dimmer level to 255
      Serial.println("Dimmer set to level 255");
    }else if (IrReceiver.decodedIRData.decodedRawData == code7){ //Code to turn the LED ON/OFF
      if(!dev1st){ // if the LED was turned off, then we turn it on 
        digitalWrite(dev1Pin,HIGH);
        dev1st=1; //LED is now turned on
        Serial.println("Dev1 is now turned on");
      }
      else{
        digitalWrite(dev1Pin,LOW); //if the LED was turned on, then we turn it off
        dev1st=0;
        Serial.println("Dev1 is now turned off");
      }}

    IrReceiver.resume(); // Receive the next IR code
  }

    else if (mySerial.available() > 0) {
        String command = mySerial.readStringUntil('\n');

    if (command.equals("/1on")) {
      digitalWrite(dev1Pin, HIGH);dev1st=1; // Turn on device 1
      Serial.println("Device 1 turned ON");
    } else if (command.equals("/1off")) {
      digitalWrite(dev1Pin, LOW);dev1st=0; // Turn off device 1
      Serial.println("Device 1 turned OFF");
    } else if (command.equals("/2on")) {
      digitalWrite(dev2Pin, HIGH);dev2st=1; // Turn on device 2
      Serial.println("Device 2 turned ON");
    } else if (command.equals("/2off")) {
      digitalWrite(dev2Pin, LOW);dev1st=0; // Turn off device 2
      Serial.println("Device 2 turned OFF");
    }

    else if (command.startsWith("/s/")) {
    // Extract the pwm speed value from the command
    int pwmspeed = command.substring(3).toInt();
            
    // Validate the pwm speed (0 to 100)
    if (pwmspeed >= 0 && pwmspeed <= 100) {
    // Calculate the corresponding pwmValue
    pwmValue = (int)(pwmspeed * 255.0 / 100.0);
                
    // Apply the calculated pwmValue to the LED
    analogWrite(dimmer1, pwmValue);
    currentspeed=pwmValue;
                
    // Print the pwmValue to Serial for confirmation
    Serial.print("PWM set to ");
    Serial.println(pwmValue);
    } else {
    Serial.println("Invalid PWM speed. Please send a value between 0 and 100. for dimmer or /1on, /1off dor dev1");
      }
    }
  }
}

The problem seems to be with the integration of these two functionalities. Any suggestions or insights on how to successfully combine IR and serial communication codes would be greatly appreciated.

Your topic has been moved. Please do not post in "Uncategorised"; see the sticky topics in Uncategorized - Arduino Forum.

Most Arduino boards use pins 0 and 1 for hardware serial (program upload, troubleshooting.). Use dofferent pins for software serial.

You can talk to only one device at a time with the serial port.

I am very surprised that software serial will work at 115200. The fastest that i have seen is 38400.

which arduino do you have ?

this might be an issue

but seprately it works fine

Arduino Nano

Probably better to use a board with a 2nd hardware serial port, the IR and SoftwareSerial interrupts are likely interfering with each other, particularly at the 115200 baud rate, which rarely works by itself on a nano.

ive created new code for serial and its working

#include <SoftwareSerial.h>
String device;
bool dev1st,dev2st;
int currentspeed;
const int dev1Pin = 7; // D7 for device 1
const int dev2Pin = 8; // D8 for device 2
const int dimmer1 = 9;  // D9 for Dimmer
int pwmValue; // Declare pwmValue globally without initialization

SoftwareSerial mySerial(0, 1); // RX, TX - Use pins 0 and 1 for SoftwareSerial

void setup() {
    pinMode(dev1Pin, OUTPUT);
    pinMode(dev2Pin, OUTPUT);
    pinMode(dimmer1, OUTPUT);
    Serial.begin(115200); // Set baud rate for Serial communication
    mySerial.begin(115200); // Set baud rate for SoftwareSerial
    Serial.println("System Initialized");
}

void loop() {
     if (Serial.available() > 0) {
    // Read the incoming command
    String command = Serial.readStringUntil('\n');

    if (command.equals("/1on")) {
      dev1st = 1; // Set device 1 ON
      digitalWrite(dev1Pin, dev1st);
      Serial.println("Device 1 turned ON");
    } else if (command.equals("/1off")) {
      dev1st = 0; // Set device 1 OFF
      digitalWrite(dev1Pin, dev1st);
      Serial.println("Device 1 turned OFF");
    } else if (command.equals("/2on")) {
      dev2st = 1; // Set device 2 ON
      digitalWrite(dev2Pin, dev2st);
      Serial.println("Device 2 turned ON");
    } else if (command.equals("/2off")) {
      dev2st = 0; // Set device 2 OFF
      digitalWrite(dev2Pin, dev2st);
      Serial.println("Device 2 turned OFF");
    }else if (command.startsWith("/s/")) {
      // Extract the pwm speed value from the command
      int pwmspeed = command.substring(3).toInt();
      
      // Validate the pwm speed (0 to 100)
      if (pwmspeed >= 0 && pwmspeed <= 100) {
        // Calculate the corresponding pwmValue
        int pwmValue = map(pwmspeed, 0, 100, 0, 255);
        
        // Apply the calculated pwmValue to the dimmer
        analogWrite(dimmer1, pwmValue);
        currentspeed = pwmspeed; // Update current speed
        
        // Print the pwmValue to Serial for confirmation
        Serial.print("Dimmer set to ");
        Serial.print(pwmspeed);
        Serial.println("%");
      }
    } else {
      // Invalid command
      Serial.println("Invalid command.");
    }
  
  }
}

now please guide me how to merge both

https://www.google.com/search?q=irremote+software+serial&oq=irremote+software+serial&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTE0NzE0ajBqN6gCD7ACAQ&client=ms-android-verizon&sourceid=chrome-mobile&ie=UTF-8

the original old Nano or one of the fancy new ones ?

on the old one this would conflict with Serial

Nano Controller: Atmega328au

so this won't work reliably. use pin 2 and 3 for example for SoftwareSerial and lower the baud rate may be to 9600 to get started

Hey experts, you said serial and software serial don't work on the same pins...right? What if I only use serial? Because my ESP writes data to the serial port and reads data from the serial port? What do you think?

Please help me to change the code too. Thanks in advance

image

IR Remote Code:

#include <IRremote.hpp>
#define IR_RECEIVE_PIN 2
bool dev1st,dev2st;

// Define IR remote codes for each speed setting
#define code1 0xDA25FBE2 // IR code for speed 0 (dimmer level 0)
#define code2 0xD22DFBE2 // IR code for speed 1 (dimmer level 40)
#define code3 0xD926FBE2 // IR code for speed 2 (dimmer level 80)
#define code4 0xF40BFBE2 // IR code for speed 3 (dimmer level 120)
#define code5 0xF50AFBE2 // IR code for speed 4 (dimmer level 160)
#define code6 0xF00FFBE2 // IR code for speed 5 (dimmer level 255)
#define code7 0xF30CFBE2 // IR code for Device 1

const int dimmerPin = 9; // Define the pin connected to the dimmer control
const int dev1 = 7;

void setup() {
  Serial.begin(115200);
  IrReceiver.begin(IR_RECEIVE_PIN); // Start the receiver
  pinMode(dimmerPin, OUTPUT); // Set dimmer pin as an output
  pinMode(dev1, OUTPUT);
}

void loop() {
  if (IrReceiver.decode()) {

    // Check which IR code was received and adjust dimmer accordingly
    if (IrReceiver.decodedIRData.decodedRawData == code1) {
      analogWrite(dimmerPin, 0); // Set dimmer level to 0
      Serial.println("Dimmer set to level 0");
    } else if (IrReceiver.decodedIRData.decodedRawData == code2) {
      analogWrite(dimmerPin, 40); // Set dimmer level to 40
      Serial.println("Dimmer set to level 40");
    } else if (IrReceiver.decodedIRData.decodedRawData == code3) {
      analogWrite(dimmerPin, 80); // Set dimmer level to 80
      Serial.println("Dimmer set to level 80");
    } else if (IrReceiver.decodedIRData.decodedRawData == code4) {
      analogWrite(dimmerPin, 120); // Set dimmer level to 120
      Serial.println("Dimmer set to level 120");
    }else if (IrReceiver.decodedIRData.decodedRawData == code5) {
      analogWrite(dimmerPin, 160); // Set dimmer level to 160
      Serial.println("Dimmer set to level 160");
    }else if (IrReceiver.decodedIRData.decodedRawData == code6) {
      analogWrite(dimmerPin, 255); // Set dimmer level to 255
      Serial.println("Dimmer set to level 255");
    }else if (IrReceiver.decodedIRData.decodedRawData == code7){ //Code to turn the LED ON/OFF
      if(!dev1st){ // if the LED was turned off, then we turn it on 
        digitalWrite(dev1,HIGH);
        dev1st=1; //LED is now turned on
      }
      else{
        digitalWrite(dev1,LOW); //if the LED was turned on, then we turn it off
        dev1st=0;
      }}

    IrReceiver.resume(); // Receive the next IR code
  }
}

Serial Com Code:

#include <SoftwareSerial.h>
String device;
bool dev1st,dev2st;
int currentspeed;
const int dev1Pin = 7; // D7 for device 1
const int dev2Pin = 8; // D8 for device 2
const int dimmer1 = 9;  // D9 for Dimmer
int pwmValue; // Declare pwmValue globally without initialization

SoftwareSerial mySerial(0, 1); // RX, TX - Use pins 0 and 1 for SoftwareSerial

void setup() {
    pinMode(dev1Pin, OUTPUT);
    pinMode(dev2Pin, OUTPUT);
    pinMode(dimmer1, OUTPUT);
    Serial.begin(115200); // Set baud rate for Serial communication
    mySerial.begin(115200); // Set baud rate for SoftwareSerial
    Serial.println("System Initialized");
}

void loop() {
     if (Serial.available() > 0) {
    // Read the incoming command
    String command = Serial.readStringUntil('\n');

    if (command.equals("/1on")) {
      dev1st = 1; // Set device 1 ON
      digitalWrite(dev1Pin, dev1st);
      Serial.println("Device 1 turned ON");
    } else if (command.equals("/1off")) {
      dev1st = 0; // Set device 1 OFF
      digitalWrite(dev1Pin, dev1st);
      Serial.println("Device 1 turned OFF");
    } else if (command.equals("/2on")) {
      dev2st = 1; // Set device 2 ON
      digitalWrite(dev2Pin, dev2st);
      Serial.println("Device 2 turned ON");
    } else if (command.equals("/2off")) {
      dev2st = 0; // Set device 2 OFF
      digitalWrite(dev2Pin, dev2st);
      Serial.println("Device 2 turned OFF");
    }else if (command.startsWith("/s/")) {
      // Extract the pwm speed value from the command
      int pwmspeed = command.substring(3).toInt();
      
      // Validate the pwm speed (0 to 100)
      if (pwmspeed >= 0 && pwmspeed <= 100) {
        // Calculate the corresponding pwmValue
        int pwmValue = map(pwmspeed, 0, 100, 0, 255);
        
        // Apply the calculated pwmValue to the dimmer
        analogWrite(dimmer1, pwmValue);
        currentspeed = pwmspeed; // Update current speed
        
        // Print the pwmValue to Serial for confirmation
        Serial.print("Dimmer set to ");
        Serial.print(pwmspeed);
        Serial.println("%");
      }
    } else {
      // Invalid command
      Serial.println("Invalid command.");
    }
  
  }
}

both codes works fine individually but stop working when merged

Go for this

Software serial requires interrupts and the IR receiver is quite demanding so it might not work well

what if i use default serial instead of software serial?

I am unclear about what is connected to the Serial port and do you need debug / informational messages to go to the serial monitor ?

What ESP are you talking about?

im using esp-01 ................. im trying to make home automation hardware (controlled by both IR and WIFI)

can someone check this:

i merge both codes using ChatGPT

#include <IRremote.hpp>

#define IR_RECEIVE_PIN 2

bool dev1st, dev2st;
int currentspeed;
const int dev1Pin = 7; // D7 for device 1
const int dev2Pin = 8; // D8 for device 2
const int dimmer1 = 9; // D9 for Dimmer
int pwmValue; // Declare pwmValue globally without initialization

// Define IR remote codes
#define code1 0xDA25FBE2 // Speed 0 (dimmer level 0)
#define code2 0xD22DFBE2 // Speed 1 (dimmer level 40)
#define code3 0xD926FBE2 // Speed 2 (dimmer level 80)
#define code4 0xF40BFBE2 // Speed 3 (dimmer level 120)
#define code5 0xF50AFBE2 // Speed 4 (dimmer level 160)
#define code6 0xF00FFBE2 // Speed 5 (dimmer level 255)
#define code7 0xF30CFBE2 // Device 1 Toggle


void setup() {
  Serial.begin(115200); // Initialize Serial communication
  IrReceiver.begin(IR_RECEIVE_PIN); // Start the IR receiver

  pinMode(dev1Pin, OUTPUT); // Set device 1 pin as output
  pinMode(dev2Pin, OUTPUT); // Set device 2 pin as output
  pinMode(dimmer1, OUTPUT); // Set dimmer pin as output
  Serial.println("System Initialized");
}

void loop() {
  // Check for IR remote commands
  if (IrReceiver.decode()) {
    // Check which IR code was received and perform actions
    if (IrReceiver.decodedIRData.decodedRawData == code1) {
      analogWrite(dimmer1, 0); // Set dimmer level to 0
      Serial.println("Dimmer set to level 0");
    } else if (IrReceiver.decodedIRData.decodedRawData == code2) {
      analogWrite(dimmer1, 40); // Set dimmer level to 40
      Serial.println("Dimmer set to level 40");
    } else if (IrReceiver.decodedIRData.decodedRawData == code3) {
      analogWrite(dimmer1, 80); // Set dimmer level to 80
      Serial.println("Dimmer set to level 80");
    } else if (IrReceiver.decodedIRData.decodedRawData == code4) {
      analogWrite(dimmer1, 120); // Set dimmer level to 120
      Serial.println("Dimmer set to level 120");
    } else if (IrReceiver.decodedIRData.decodedRawData == code5) {
      analogWrite(dimmer1, 160); // Set dimmer level to 160
      Serial.println("Dimmer set to level 160");
    } else if (IrReceiver.decodedIRData.decodedRawData == code6) {
      analogWrite(dimmer1, 255); // Set dimmer level to 255
      Serial.println("Dimmer set to level 255");
    } else if (IrReceiver.decodedIRData.decodedRawData == code7) {
      // Toggle Device 1
      dev1st = !dev1st;
      digitalWrite(dev1Pin, dev1st);
      Serial.println(dev1st ? "Device 1 turned ON" : "Device 1 turned OFF");
    }

    IrReceiver.resume(); // Receive the next IR code
  }

  // Check for Serial commands
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');

    if (command.equals("/1on")) {
      digitalWrite(dev1Pin, HIGH); // Turn on Device 1
      Serial.println("Device 1 turned ON");
    } else if (command.equals("/1off")) {
      digitalWrite(dev1Pin, LOW); // Turn off Device 1
      Serial.println("Device 1 turned OFF");
    } else if (command.equals("/2on")) {
      digitalWrite(dev2Pin, HIGH); // Turn on Device 2
      Serial.println("Device 2 turned ON");
    } else if (command.equals("/2off")) {
      digitalWrite(dev2Pin, LOW); // Turn off Device 2
      Serial.println("Device 2 turned OFF");
    } else if (command.startsWith("/s/")) {
      int pwmspeed = command.substring(3).toInt(); // Extract pwm speed value

      if (pwmspeed >= 0 && pwmspeed <= 100) {
        pwmValue = map(pwmspeed, 0, 100, 0, 255); // Map pwm speed to PWM value
        analogWrite(dimmer1, pwmValue); // Set dimmer to calculated PWM value
        currentspeed = pwmspeed; // Update current speed
        Serial.print("Dimmer set to ");
        Serial.print(pwmspeed);
        Serial.println("%");
      }
    } else {
      Serial.println("Invalid command.");
    }
  }
}