Buzzer without tone

Hi. I want to use Buzzer (Arduino NANO) without tone() function.
Problem is, that i want some real delay between sound effects, but if i give it delay() its just stop playing at all.

Here is my code:

void loop() {
      //gggd fffd#
      //1.G
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1275);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1275);


      //2.G
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1275);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1275);

      //3.G
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1275);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1275);

     //4.D
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1700);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1700);
 
      //5.F
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1432);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1432);

           //6.F
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1432);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1432);

                 //7.F
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1432);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1432);

                       //8.D#
      digitalWrite(SIRENKA,1);
      delayMicroseconds(1135);
      digitalWrite(SIRENKA,0);
      delayMicroseconds(1135);

Thanks in advance

Your code is incomplete.

It's also not a tutorial.

Code does not compile here either, most of it is missing.

And unless you tell us what this 'Buzzer' is and how its connected\wired no-one can advise why its 'just stop playing at all'

Lots more information required.

You can use beep() function of this buzzer library. Here is an example of beeping without using delay

You can't play a note by playing one cycle of the note. You have to REPEAT the cycle for the duration of the note. Here is one way to do it. I put the cycles in functions so they would not need to be copied for multiple notes.

const unsigned long NoteLength = 250;
const byte SIRENKA = 4;


void setup()
{
  pinMode(SIRENKA, OUTPUT);
}


void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long noteStartMillis = 0;
  static int note = 0;


  //gggd fffd#
  switch (note)
  {
    case 0:
    case 1:
    case 2:
      G();
      break;


    case 3:
      D();
      break;


    case 4:
    case 5:
    case 6:
      F_Natural();   // Can't use the name 'F()' because of the F macro
      break;


    case 7:
      D_Sharp();
      break;


    default:
      delay(500);
      noteStartMillis = currentMillis;
      note = 0;
      break;
  }


  if (currentMillis - noteStartMillis >= NoteLength)
  {
    noteStartMillis = currentMillis;
    note++;
  }
}


void G()
{
  digitalWrite(SIRENKA, 1);
  delayMicroseconds(1275);
  digitalWrite(SIRENKA, 0);
  delayMicroseconds(1275);
}


void D()
{
  digitalWrite(SIRENKA, 1);
  delayMicroseconds(1700);
  digitalWrite(SIRENKA, 0);
  delayMicroseconds(1700);
}


void F_Natural()  // Can't use the name 'F()' because of the F macro
{
  digitalWrite(SIRENKA, 1);
  delayMicroseconds(1432);
  digitalWrite(SIRENKA, 0);
  delayMicroseconds(1432);
}


void D_Sharp()
{
  digitalWrite(SIRENKA, 1);
  delayMicroseconds(1135);
  digitalWrite(SIRENKA, 0);
  delayMicroseconds(1135);
}

You can also use the timer1.h library (just like tone() does) and write your own ISR.

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