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.
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.
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
#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