2

I want this program to receive multiple signals from the elegoo ir remote and produce different tones using the passive buzzer. I have attempted to add onto the basic example provided by adding a makeSound() function.

The serial monitor outputs different values for a button if I hit it multiple times. Is there a way to make it so that each button will give a consistent value so I can keep the pitch the same for each button?

#include "IRremote.h"
#include "IR.h"
#include "pitches.h"

IRrecv irrecv(RECEIVER);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
}

void loop()
{
  int tmpValue;
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
        tmpValue = results.value;
        Serial.println(tmpValue);
        makeSound(tmpValue);
    }
    irrecv.resume(); // receive the next value
  }

void makeSound(int tmpValue) {
Serial.println(tmpValue);

  if (tmpValue > 0) {
    Serial.println("In if statement");
    tone(8, NOTE_D5);
    delay(500);
    noTone(8);
  }
  if (tmpValue < 0) {
    Serial.println("In if statement");
    tone(8, NOTE_G1);
    delay(500);
    noTone(8);
  }


}
2
  • If you separate out the problem into having something to do with the remote code or the tone code, you remove the other from the question and you're more likely to get useful help.
    – timemage
    Jun 24 at 1:06
  • simplify the code ... receive data and print it ... nothing more ... do not decode ... do not activate buzzer ... when that works as expected, then decode
    – jsotola
    Jun 24 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.