IR Remote decode giving 0's after tone() function

Hey guys, I’ve been working on a home security system and I’m using the IR remote for password input. everything works fine until I input the wrong password and attempt to loop the armed() function. for some reason, IR only decodes zeros after that point but it’s fine though out. I added a lot of lines to make sure which code is executing and the resume function is working. I am also up to date with the IRremote.h 3.1 libraries and I tried to use the new methods, and it’s working fine until it reaches some point (it will be marked in the code). also p.s. I have deleted much of my code to make it look convenient for you guys but there is an LCD and PIR sensor.

Edit#1:
I have found the problem and its the buzzer function I have. whenever I remove the execution of that it works as expected. once I put it back in the reading of the IR turns to 0’s only. you can find the buzz() function down in the comments. I really don’t understand how these things relate to each other.

Edit#2:
I realized that the tone function is the problem. because I tested using the buzzer in a for loop that alternates it on and off to create a frequency and it works. I understand the tone function is built differently. is there some way that it is interfering with the IRremote.h?

#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Servo.h>

//IR remote codes
#define IR_0 25 
//. . .
#define IR_hash 13
// notes
#define NOTE_A3 220
//. . . 
#define NOTE_B3 247



const int IR_RECEIVE_PIN = 7;

const int buzzer = 3;

const int PIR_pin = 5;

int pass[] = {0, 0, 0, 0};
int pos = 0;
bool passStart = true;

int str[] = {0, 0, 0, 0};
bool armStart;

IRrecv irrecv(IR_RECEIVE_PIN);
decode_results results;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servo;


void setup() {
  
  lcd.init();
  lcd.backlight();
  delay(1000);
  Serial.begin(9600);
  irrecv.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
  lcd.blink();
}



void PIR() {. . .}

void armed() {                                               // function after the password is set

  if (irrecv.decode()) {
    Serial.print("IR is: ");
    Serial.println(irrecv.decodedIRData.command);
    
  
   if (pos < 4 && (irrecv.decodedIRData.command == IR_0)) {str[pos] = 10; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_1)) {str[pos] = 11; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_2)) {str[pos] = 22; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_3)) {str[pos] = 33; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_4)) {str[pos] = 44; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_5)) {str[pos] = 55; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_6)) {str[pos] = 66; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_7)) {str[pos] = 77; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_8)) {str[pos] = 88; pos++;}
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_9)) {str[pos] = 99; pos++;}

    else if (pos >= 0 && irrecv.decodedIRData.command == IR_star) {pos--; str[pos] = 0;}
    else if (irrecv.decodedIRData.command == IR_hash) {
      str[0] = 0, str[1] = 0, str[2] = 0, str[3] = 0;
      
      if(test(pass, str)) {
      lcd.print("unlocked");
      Serial.println("unlocked");
      armStart = false; }
      else {                                 // !!!!!!!!!!!!!!!!Exactly after finishing this else block it goes back to printing 
                                                     //the serial at the beginning of the function, but all IR data is 0 
        pos = 0;
        buzz();                              // Later I realized this is the problem
        lcd.print("pass alarm");
        Serial.println("pass alarm");
        lcd.clear();
         
        
      }
      
    }
   irrecv.resume();
  }
  delay(350);
} 


void password() {. . .}                                      // password setting function

void loop() {
  while(passStart) password();
  while(armStart) armed();
}

here is the code for buzz():

void buzz() {

  for (int i = 0; i < 18; i++) {
    int d = 125;
    int t = 125;
    int notes[] = { E4, G4,  A4, A4,
                   0, A4, B4, C5, C5, 
                   0, C5, D5, B4, B4, 
                   0, A4, G4, A4, 0};
                   
    if (i == 2)  t = 125 * 2, d = 125 * 2;
    if (i == 7)  t = 125 * 2, d = 125 * 2;
    if (i == 12) t = 125 * 1, d = 125 * 2;
    if (i == 17) t = 125 * 1, d = 125 * 3;

    tone(buzzer, notes[i], t);
    delay(d);
  }
}

lcd.print(“unlocked”), Serial.println(“unlocked”), armStart = false;

Use

;

instead of:

,

The IRremote library was recently updated. Older code will not run on the new library. You can modify the code to work with the new library (see the IRremote library reference) or deleted the new library from your library folder and install an older version of the library. The older versions are available through the IDE library manager.

I have updated all the code. and its working fine until I ask it to loop the armed() function. i made sure the resume is executing but I get 0’s only and only after that point.

This construct here:

else if (pos < 4 && (irrecv.decodedIRData.command == IR_1)) str[pos] = 11, pos++;

may be written as:

else if (pos < 4 && (irrecv.decodedIRData.command == IR_1)) str[pos++] = 11 ;

Assuming you did not intend to use the comma ‘,’ operator.

ok guys so i found out why my readings are giving our zeros
there is a buzz function im using with the alarm in the else condition

the buzzer is on pin 3 and the IR sensor is on 7…
I am not sure whats interfering here.

void buzz() {
  for (int i = 0; i < 18; i++) {
    int d = 125;
    int t = 125;
    int notes[] = {NOTE_E4,
                   NOTE_G4, NOTE_A4, NOTE_A4,
                   0, 
                   NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 
                   0, 
                   NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 
                   0, 
                   NOTE_A4, NOTE_G4, NOTE_A4, 
                   0};
    if (i == 2)  t = 125 * 2, d = 125 * 2;
    if (i == 7)  t = 125 * 2, d = 125 * 2;
    if (i == 12) t = 125 * 1, d = 125 * 2;
    if (i == 17) t = 125 * 1, d = 125 * 3;

    tone(buzzer, notes[i], t);
    delay(d);
  }
void armed() {

  Serial.print("attempt"); 
  Serial.print(String(str[0])[1]);
  Serial.print(String(str[1])[1]);
  Serial.print(String(str[2])[1]);
  Serial.println(String(str[3])[1]);
  Serial.println(pos);
  


  if (irrecv.decode()) {
    Serial.print("IR is: ");
    Serial.println(irrecv.decodedIRData.command);
    
  
    if (pos < 4 && (irrecv.decodedIRData.command == IR_0)) str[pos] = 10, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_1)) str[pos] = 11, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_2)) str[pos] = 22, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_3)) str[pos] = 33, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_4)) str[pos] = 44, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_5)) str[pos] = 55, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_6)) str[pos] = 66, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_7)) str[pos] = 77, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_8)) str[pos] = 88, pos++;
    else if (pos < 4 && (irrecv.decodedIRData.command == IR_9)) str[pos] = 99, pos++;

    else if (pos >= 0 && irrecv.decodedIRData.command == IR_star) pos--, str[pos] = 0;
    else if (irrecv.decodedIRData.command == IR_hash) { // hash is the "enter" button...
      lcd.noBlink();
      lcd.setCursor(0,1);

      //TODO: Delete the serial printing
      Serial.print("attempt"); 
      Serial.print(String(str[0])[1]);
      Serial.print(String(str[1])[1]);
      Serial.print(String(str[2])[1]);
      Serial.println(String(str[3])[1]);

      
      
      if(test(pass, str)) {
        str[0] = 0, str[1] = 0, str[2] = 0, str[3] = 0;
        lcd.print("unlocked");
        Serial.println("unlocked");
        armStart = false;
        delay(2000);
        lcd.clear();
        Serial.println("end");
        pos = 0;
        
      }
        **else** {
          pos = 0;
          lcd.print("pass alarm");
          Serial.println("pass alarm");
          **buzz();** 
         lcd.clear();
          
        }
      
    }
   irrecv.resume();
   Serial.println("resume");
  }
  delay(350);
} ```

You are using the comma operator throughout your sketch; check the C++ reference to see if what you are writing is appropriate.

so far I’ve been using the comma just to avoid creating a block and it’s been working fine. is there a problem with this kind of usage?

Perhaps the comma in C++ will be of interest.

The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). (emphasis added).

Is that what you want to do?

no i think that is when you are assigning
for example:

x = 1, 2;           // x will equal to 1

mind you the what the comma does is execute each separately
so first: x = 1 then 2 (this line does nothing so in this case it doesn’t do anything)

what I’m doing is saving myself space so instead of writing:

else if (pos < 4 && (irrecv.decodedIRData.command == IR_9))  {
   str[pos] = 99;
   pos++;
}
// i write this and it executes from left to right
else if (pos < 4 && (irrecv.decodedIRData.command == IR_9)) str[pos] = 99, pos++;

else if (pos < 4 && (irrecv.decodedIRData.command == IR_9)) str[pos] = 99, pos++;

You are taking shortcuts that will bite . . . butt.

Suggest you use this format as everything is exposed and there is no chance for ambiguity:


else if (pos < 4 && (irrecv.decodedIRData.command == IR_9))  
{
   str[pos] = 99;
   pos++;
}

Or at a minimum (which IMO is crap):


else if (pos < 4 && (irrecv.decodedIRData.command == IR_9)) {str[pos] = 99; pos++;}

1 Like

Thanks will do!
still my problem is with the buzz() function. I dont understant why when I execute that funtion, the IR sensor only reads 0’s after. i dont think its a hardware problem because when i removed it the code worked just as I expected it to. I’ve put the code to the buzz() above. maybe you know something?

Look, no one wants to hunt for things in a thread.

When you want to add information or update code, do so in a new post (especially after people have referred to what was said previously).

Changes to early posts without explicit reference to it makes things an Easter egg hunt.

One exception might be correcting a spelling mistake or adding punctuation.

Once a post appears after your post, consider your post locked.


This writer will not search for changes further up a thread.