1

I have a buzzer and an IR led emitter so I have to use the tone function to sound the buzzer and to modulate the output of the LED. I have connected the led pin 5 of the arduino, the buzzer pin A0.

int tones[] = {261, 277, 294, 280, 230};
const int pinBuzzer = A0;
const int cr_pin = 5;

void setup(){
  Serial.begin(1200);
  tone(cr_pin, 38000);  /* For modulation at 38kHz */
  pinMode(pinBuzzer, OUTPUT);
}

void soundBuzzer() {
  for (int i = 0; i < sizeof tones; i ++) { 
    tone(pinBuzzer, tones[i]);
  } 
    noTone(pinBuzzer);  
}


void irLED() {
  Serial.println("Hello");
}

This is my code but I can not make the led and the buzzer work at the same time, apparently the buzzer is affected by the tone line (cr_pin, 38000); / * For modulation at 38kHz * /.

I tried to remove (cr_pin, 38000); / * For modulation at 38kHz * /. of the setup and leave code in the following way.

int tones[] = {261, 277, 294, 280, 230};
const int pinBuzzer = A0;
const int cr_pin = 5;

void setup(){
  Serial.begin(1200);
  pinMode(pinBuzzer, OUTPUT);
}

void soundBuzzer() {
  for (int i = 0; i < sizeof tones; i ++) { 
    tone(pinBuzzer, tones[i]);
  } 
    noTone(pinBuzzer);  
}


void irLED() {
tone(cr_pin, 38000);  /* For modulation at 38kHz */
      Serial.println("Hello");
noTone(cr_pin);
    }

In the first code the LED works but not the buzzer and in the second code the buzzer now works but not the LED.

1

Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency. Source: arduino.cc - tone().

noTone() Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated. Source: arduino.cc - noTone()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.