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.
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);
}