Buzzer disturbing with IR sensor

I'm a mostly beginner at coding, but i've been trying to make my robot make a noise with a buzzer when it opens it's mouth. Although when it opens its mouth and buzzes it messes up the IR sensor I'm controling the robot with. It makes the IR sensor scramble the HEX code the IR remote sends to the sensor.
Here's my code

#include <IRremote.h>
#include <Servo.h>
IRrecv IR(3);
Servo Mservo;
Servo Lservo;
Servo Rservo;
int pos = 0;
const int Buzzer = 4;

void setup() {
  Lservo.attach(8);
  Rservo.attach(12);
  Mservo.attach(13);
  Mservo.write(pos);
  Lservo.write(pos);
  Rservo.write(pos);
  pinMode(Buzzer, OUTPUT);
  IR.enableIRIn();
  Serial.begin(9600);
}

void loop() {
  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData, HEX);//left eye
    if(IR.decodedIRData.decodedRawData == 0xBB44FF00){
      for (int i = 0; i < 1; i++) //repeat
{
    for(pos = 0; pos < 60; pos++){                                  
    Lservo.write(pos);               
    delay(20);                   
  } 
  delay(500);
  for(pos = 60; pos > -60; pos--){                                
    Lservo.write(pos);           
    delay(20);       
  } 
}
    }
    delay(50);
    IR.resume();
  }
  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData, HEX);//right eye
    if(IR.decodedIRData.decodedRawData == 0xBC43FF00){
      for (int i = 0; i < 1; i++) //repeat
{
    for(pos = 0; pos < 60; pos++){                                  
    Rservo.write(pos);               
    delay(20);                   
  } 
  delay(500);
  for(pos = 60; pos > -60; pos--){                                
    Rservo.write(pos);           
    delay(20);       
  } 
}
    }
    delay(100);
    IR.resume();
  }
  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData, HEX);//restart eyes
    if(IR.decodedIRData.decodedRawData == 0xBA45FF00){
      for (int i = 0; i < 1; i++) //repeat
{
    for(pos = pos; pos > 0; pos++){                                  
    Lservo.write(pos);               
    delay(20);                   
  } 
  delay(500);
  for(pos = pos; pos < 0; pos--){                                
    Lservo.write(pos);           
    delay(20);       
  } 
}
    }
    delay(100);
    IR.resume();
  }
  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData, HEX);//mouth
    if(IR.decodedIRData.decodedRawData == 0xEA15FF00){
      for (int i = 0; i < 1; i++) //repeat
{
    for(pos = 0; pos > -30; pos--){                                  
    Mservo.write(pos);               
    delay(20);           
  }
  tone(Buzzer, 500);
  delay(1000);
  noTone(Buzzer);
  for(pos = -30; pos < 30; pos++){                                
    Mservo.write(pos);           
    delay(20);       
  } 
}
    }
    delay(100);
    IR.resume();
  }
}

From the FAQs and hints section in the IRremote repository:

Receiving does not run if analogWrite() or tone() is used.

The receiver sample interval of 50 µs is generated by a timer. On many boards this must be a hardware timer. On some boards where a software timer is available, the software timer is used.
Be aware that the hardware timer used for receiving should not be used for analogWrite().
On the Uno and other AVR boards the receiver timer is the same as the tone timer, so receiving will stop after a tone() command. See ReceiveDemo example how to deal with it.

1 Like

I don't understand. What do you say I do?

If you read the section of the FAQs and hints I quoted, it told you exactly what to do.

I'm sorry but I don't quite understand what it's telling me to do. Could you put it in a way I would understand, please?

hey thanks for this! i might be too late but i read from your sent link that tone() disrupts the IR receiver, meaning you need to restart the IR receiver timer in order to accept another input from remote. turns out, the random hex codes showing when pressing button shows that the IR receiver is not working properly.

Solution:
after tone() function, add IrReceiver.restartTimer(8000); this solves the problem! I've been troubleshooting my code and my hardware for 5 hours. gosh!

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