Conflict between physical button and IR remote

Guys i am having this little project that i am trying to improve adding IR remote control.

It is working well if i don't have IR receiver in my project, but as soon as i am adding it, debugging the code i can see that it responding to physical button and also to remote control button, but whenever i any of the button code is calliing function performaction() and on the line where it should release first sounds it just freezing. (tone(speakerPin1, 700, 500):wink:

i think it can be some conflict between executing the performaction function and IR listening.
is there could be done another way to make it work? will attach below code and also screenshot from tinkercad.

code***

#include <IRremote.h>

const int buttonPin = 8;    // the number of the pushbutton pin
const int speakerPin1 = 5;  // the number of the first buzzer pin
const int speakerPin2 = 3;  // the number of the second buzzer pin
const int irReceiverPin = 9;
IRrecv irrecv(irReceiverPin);
decode_results results;

// Define variables to store button state
int buttonState = HIGH;
int lastButtonState = HIGH; // variable for reading the pushbutton status

void setup() {
  // Initialize the buzzer pins as outputs
  pinMode(speakerPin1, OUTPUT);
  pinMode(speakerPin2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(irReceiverPin, INPUT);
  irrecv.enableIRIn(); // Start the IR receiver
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    performAction();
    delay(2000);
    
  }
  
  if (irrecv.decode(&results)) {
    if (results.value == 0xFFFFFFFF) { // Example IR remote button code (replace with your remote's button code)
      performAction();
      delay(2000);
    }
    irrecv.resume(); // Receive the next value
  }

}


void performAction() {
  // Beep sequence for both speakers
  for (int i = 0; i < 4; i++) {
    // Beep for speaker 1
    tone(speakerPin1, 700, 500); // Adjust volume level and frequency
    // Beep for speaker 2
    tone(speakerPin2, 700, 500); // Adjust volume level and frequency
    delay(200);
    // Silence for both speakers
    noTone(speakerPin1);
    noTone(speakerPin2);
    delay(200);
  }
  delay(10); // Delay after beep sequence
}

code***

2 times delay(2000) inside loop() and you wonder why it's not working?

1 Like

You can only play one tone at a time

1 Like

I moved your topic to an appropriate forum category @mosya923.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

That was a small test that didn't help me, but in the end code i have remove it

you are right, i just found that IRremote library has this function tone() as well, that is why i was having conflict.

here is the final code, that works for both buttons independent of the HEX.

#include <IRremote.h>
const int buttonPin = 8;    // the number of the pushbutton pin
const int speakerPin1 = 5;  // the number of the first buzzer pin
const int speakerPin2 = 3;  // the number of the second buzzer pin
const int irReceiverPin = 9;
IRrecv irrecv(irReceiverPin);
decode_results results;

// Define variables to store button state
int buttonState = HIGH;
int lastButtonState = HIGH; // variable for reading the pushbutton status

void setup() {
  // Initialize the buzzer pins as outputs
  pinMode(speakerPin1, OUTPUT);
  pinMode(speakerPin2, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(irReceiverPin, INPUT);
  irrecv.enableIRIn(); // Start the IR receiver
  Serial.begin(9600);
}

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    performAction();    
  }
    if (irrecv.decode(&results)) {
    // Print the HEX code of the received IR signal
    Serial.println(results.value, HEX);
    performAction(); // React to any received IR signal
    irrecv.resume(); // Receive the next value
    }
}

void playTone(int pin, int frequency, int duration) {
  int period = 1000000L / frequency; // Calculate the period of the tone
  int pulseWidth = period / 15; // Adjust pulse width for lower volume
  
  for (long i = 0; i < duration * 1000L; i += period) {
    digitalWrite(pin, HIGH); // Set the speaker pin HIGH to generate a pulse
    delayMicroseconds(pulseWidth); // Wait for the pulse width duration
    digitalWrite(pin, LOW); // Set the speaker pin LOW to end the pulse
    delayMicroseconds(pulseWidth); // Wait for the remaining period
  }
}


void performAction() {
  // Beep sequence for both speakers
  for (int i = 0; i < 4; i++) {
    // Beep for speaker 1
    playTone(speakerPin1, 300, 300); // Adjust volume level and frequency
    // Beep for speaker 2
    playTone(speakerPin2, 300, 300); // Adjust volume level and frequency
    delay(200);
  }
  delay(200); // Delay after beep sequence
}

Which IR library ae you using?

#include <IRremote.h> 4.3.1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.