problem using IRreceive & tone() in same Arduino sketch

Discussion in 'Embedded Systems and Microcontrollers' started by flat5, Apr 7, 2015.

  1. flat5

    Thread Starter Active Member

    Nov 13, 2008
    403
    17
    Maybe I can get help in this forum.
    Proper numbers are returned except when the 'tone' function is added.
    After first time only '0' is returned.
    Code (Text):
    1.  
    2. /*
    3. IR_remote_detector sketch
    4. An IR remote receiver is connected to pin 11. Have tried 2-6 & 11.
    5. Decode problem when using tone function. Always returns 0 after first time.
    6. The LED on pin 13 toggles each time a button on the remote is pressed.
    7. */
    8.  
    9. #include <IRremote.h>                    //adds the library code to the sketch
    10.  
    11. const int irReceiverPin = 11;            //pin the receiver is connected to
    12. const int ledPin = 13;
    13.  
    14. IRrecv irrecv(irReceiverPin);            //create an IRrecv object
    15. decode_results decodedSignal;         //stores results from IR detectorvoid setup()
    16. unsigned long value;
    17. void setup ()
    18. {
    19. Serial.begin(9600);
    20. pinMode(ledPin, OUTPUT);
    21. irrecv.enableIRIn();                   // Start the receiver object
    22. }
    23.  
    24. boolean lightState = false;     //keep track of whether the LED is on
    25. unsigned long last = millis();  //remember when we last received an IR message
    26.  
    27. void loop()
    28. {
    29. if (irrecv.decode(&decodedSignal) == true) //this is true if a message has
    30.                                             //been received
    31. {
    32.  value = decodedSignal.value;
    33.    delay(200); // extra debounce
    34.  Serial.print(value);
    35.  Serial.print("  ");
    36.  
    37.  
    38.  tone(8,600,100);   // comment out this line and the IR receiver works as expected!
    39.  
    40.  
    41.  if (millis() - last > 250) {       //has it been 1/4 sec since last message? debounce?
    42.    lightState = !lightState;              //Yes: toggle the LED
    43.    digitalWrite(ledPin, lightState);
    44.  }
    45.  last = millis();
    46.  irrecv.resume();                         // watch out for another message
    47. }
    48. }
    49.  
     
  2. JWHassler

    Member

    Sep 25, 2013
    201
    33
  3. flat5

    Thread Starter Active Member

    Nov 13, 2008
    403
    17
    Thank you, JWH! My Google searches found nothing useful.
    I thought it was an interrupt problem but what do I know :)
    It gives me a place to start. I'm reading about it all...

    First I'll try another IR library but what I'm reading is they may all have the problem without modification.
     
Loading...