Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tone.cpp #587

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ Version Modified By Date Comments
0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62)
0010 jipp 15/04/13 added additional define check #2923
0011 Constant T 29/12/2024 added function melody()
*************************************************/

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "Arduino.h"
#include "pins_arduino.h"



#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
#define TCCR2A TCCR2
#define TCCR2B TCCR2
Expand Down Expand Up @@ -476,7 +479,47 @@ void disableTimer(uint8_t _timer)
}
}

int melody(int pin, int notes[], int noteSize, int durations[], int durationSize)
{
static int currentNoteIndex = 0;
static unsigned long noteStartTime = 0;
static bool isPlaying = false;

while((noteSize != durationSize) || (noteSize < durationSize) || (noteSize > durationSize))
{
return -1;
}

if (!isPlaying)
{
isPlaying = true;
currentNoteIndex = 0;
noteStartTime = millis();
tone(pin, notes[currentNoteIndex]);
}
else
{
unsigned long currentTime = millis();
if (currentTime - noteStartTime >= durations[currentNoteIndex])
{
noTone(pin);
currentNoteIndex++;
if (currentNoteIndex < noteSize)
{
tone(pin, notes[currentNoteIndex]);
noteStartTime = currentTime;
}
else
{
isPlaying = false;
currentNoteIndex = 0;
return 0;
}
}
}

return 1;
}
void noTone(uint8_t _pin)
{
int8_t _timer = -1;
Expand Down
Loading