Problems with Hex codes randomizing

Building a proximity alarm that sets off a speaker with a certain tone when it detects something within ten centimeters. You can also turn it on and off with a controller. All of this works fine. BUT, when the alarm has been tripped, suddenly the hex inputs start showing up with random numbers, therefore breaking the whole code. Removing the code related to the speaker playing suddenly makes it work. Haven't been able to find any similar issues anywhere. Does anyone know what's up with that? Code attached.

int melody[] = {
  NOTE_D5, -4, NOTE_E5, -4, NOTE_A4, 4, //1
  NOTE_E5, -4, NOTE_FS5, -4, NOTE_A5, 16, NOTE_G5, 16, NOTE_FS5, 8,
  NOTE_D5, -4, NOTE_E5, -4, NOTE_A4, 2,
  NOTE_A4, 16, NOTE_A4, 16, NOTE_B4, 16, NOTE_D5, 8, NOTE_D5, 16,
};

int speakerwire = 6;
int thisNote = 0;
int tempo = 114;
int notes = sizeof(melody) / sizeof(melody[0]) / 2;

// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

// Everything above this is just the song, don't worry about it

bool on;
bool off;
bool audiotrigger;

int trigger_pin = 2;

int echo_pin = 3;

int greenlight = 4;
int yellowlight = 5;
int redlight = 9;

const int RECV_PIN = 7;
IRrecv IR(RECV_PIN);

int pulsetime;

int distance;

void setup ( ) {

  Serial.begin (9600);

  pinMode (trigger_pin, OUTPUT);
  pinMode (echo_pin, INPUT);
  pinMode (greenlight, OUTPUT);
  pinMode (redlight, OUTPUT);
  pinMode (yellowlight, OUTPUT);
  pinMode (speakerwire, OUTPUT);

  IR.enableIRIn();
  Serial.begin(9600);
}

void loop ( ) {

  if (IR.decode()) {
    Serial.println(IR.decodedIRData.decodedRawData, HEX);
    delay(1000);
    IR.resume();
  }


  if (IR.decodedIRData.decodedRawData == 0xBA45FF00) {
    on = true;
    off = false;
    Serial.println("IR code is 0xBA45FF00");
  }
  if (IR.decodedIRData.decodedRawData == 0xB847FF00) {
    off = true;
    on = false;
    Serial.println("IR code is 0xB847FF00");
  }

  while (on == true)
  {

    digitalWrite (trigger_pin, HIGH);

    delayMicroseconds (10);

    digitalWrite (trigger_pin, LOW);

    pulsetime = pulseIn (echo_pin, HIGH);

    distance = (pulsetime * 0.034) / 2;

    if (IR.decode()) {
      Serial.println(IR.decodedIRData.decodedRawData, HEX);
      delay(1000);
      IR.resume();
    }

    if (IR.decodedIRData.decodedRawData == 0xB847FF00) {
      off = true;
      on = false;
      Serial.println("IR code is 0xB847FF00");
    }
    if (distance <= 10)

    {

      Serial.println (" Door Open ");

      Serial.print (" Distance = ");
      Serial.println (distance);

      digitalWrite(redlight, HIGH);
      digitalWrite(yellowlight, LOW);
      digitalWrite(greenlight, LOW);

      audiotrigger = true;
      delay (500);



      if (IR.decode()) {
        Serial.println(IR.decodedIRData.decodedRawData, HEX);
        delay(1000);
        IR.resume();
      }

      if (IR.decodedIRData.decodedRawData == 0xB847FF00) {
        off = true;
        on = false;
        Serial.println("IR code is 0xB847FF00 in loop");
      }
    }
    else if ((distance > 10) && (distance <= 20))

    {

      Serial.println (" Door Closed ");

      Serial.print (" Distance = ");
      Serial.println (distance);

      digitalWrite(redlight, LOW);
      digitalWrite(yellowlight, HIGH);
      digitalWrite(greenlight, LOW);

      delay (500);


      if (IR.decode()) {
        Serial.println(IR.decodedIRData.decodedRawData, HEX);
        delay(1000);
        IR.resume();
      }

      if (IR.decodedIRData.decodedRawData == 0xB847FF00) {
        off = true;
        on = false;
        Serial.println("IR code is 0xB847FF00 in loop");
      }
    }
    else if (distance > 20)

    {

      Serial.println (" Door Closed ");

      Serial.print (" Distance = ");
      Serial.println (distance);

      digitalWrite(redlight, LOW);
      digitalWrite(yellowlight, LOW);
      digitalWrite(greenlight, HIGH);

      delay (500);


      if (IR.decode()) {
        Serial.println(IR.decodedIRData.decodedRawData, HEX);
        delay(1000);
        IR.resume();
      }

      if (IR.decodedIRData.decodedRawData == 0xB847FF00) {
        off = true;
        on = false;
        Serial.println("IR code is 0xB847FF00 in loop");
      }
    }

    if (audiotrigger == true) {
      for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

        // calculates the duration of each note
        divider = melody[thisNote + 1];
        if (divider > 0) {
          // regular note, just proceed
          noteDuration = (wholenote) / divider;
        } else if (divider < 0) {
          // dotted notes are represented with negative durations!!
          noteDuration = (wholenote) / abs(divider);
          noteDuration *= 1.5; // increases the duration in half for dotted notes
        }

        // we only play the note for 90% of the duration, leaving 10% as a pause
        tone(speakerwire, melody[thisNote], noteDuration * 0.9);

        // Wait for the specief duration before playing the next note.
        delay(noteDuration);

        // stop the waveform generation before the next note.
        noTone(speakerwire);
      }
      audiotrigger = false;
    }

  } /// end of while

  while (off == true) {

    digitalWrite(redlight, LOW);
    digitalWrite(yellowlight, LOW);
    digitalWrite(greenlight, LOW);

    digitalWrite (trigger_pin, LOW);

    delayMicroseconds (10);

    digitalWrite (trigger_pin, LOW);

    pulsetime = pulseIn (echo_pin, LOW);

    if (IR.decode()) {
      Serial.println(IR.decodedIRData.decodedRawData, HEX);
      delay(1000);
      IR.resume();
    }

    if (IR.decodedIRData.decodedRawData == 0xBA45FF00) {
      on = true;
      off = false;
    }

  } // end while

}

Your code is a mixture of delays and time critical sections. It is a cause why you had to use IR.decode() four times in a loop().

You should to rewrite the code as non-blocking mode using millis() instead of delays.

2 Likes

Honestly just trying to get by here. Got thrown into this project for class without actually learning anything really, so my apologies if it's a bit... Not good. I will look into this.

In terms of rewrite, This part where you are Testing the received code should only be in the part where you actually have received something

Read about state machines as well

For extra information and examples look at

1 Like

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