#include "pitches.h"
#include <TimerFreeTone.h>

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

int melody2[] = { 262, 196, 196, 220, 196, 0, 247, 262 };
int duration2[] = { 250, 125, 125, 250, 250, 250, 250, 250 };

int melody3[] = { 262, 196};
int duration3[] = { 250, 125};


#define TONE_PIN 8



void playTone(){

//   digitalWrite(TONE_PIN, HIGH);
//   delay(2000);
//   digitalWrite(TONE_PIN, LOW);
// return;
  for (int thisNote = 0; thisNote < 8; thisNote++) { // Loop through the notes in the array.
    TimerFreeTone(TONE_PIN, melody2[thisNote], duration2[thisNote]); // Play thisNote for duration.
    delay(50); // Short delay between notes.
  }
    
}

float sinVal;
int toneVal;

void playSound(bool silent){
  int max = 180;
  if(silent)
    max = 2;
  for(int x = 0; x < max ; x++){
    //convert angle of sinusoidal to radian measure
    sinVal = (sin(x*(3.1412/180)));
    //generate sound of different frequencies by sinusoidal value
    toneVal = 2000+(int(sinVal*1000));
    //Set a frequency for Pin-out 8
    tone(TONE_PIN, toneVal, 3);
    // TimerFreeTone(TONE_PIN, toneVal, 2, 5);
    delay(2);
  }
  noTone(TONE_PIN);
}


void playAlarm(bool silent){
  playSound(silent);
  delay(500);
}


