0

I am building a security system, and found the following code

#define NOOT_B0  31
#define NOOT_C1  33
#define NOOT_D1  37
#define NOOT_E1  41
#define NOOT_F1  44
#define NOOT_G1  49
#define NOOT_A1  55
#define NOOT_B1  62
#define NOOT_C2  65
#define NOOT_D2  73
#define NOOT_E2  82
#define NOOT_F2  87
#define NOOT_G2  98
#define NOOT_A2  110
#define NOOT_B2  123
#define NOOT_C3  131
#define NOOT_D3  147
#define NOOT_E3  165
#define NOOT_F3  175
#define NOOT_G3  196
#define NOOT_A3  220
#define NOOT_B3  247
#define NOOT_C4  262
#define NOOT_D4  294
#define NOOT_E4  330
#define NOOT_F4  349
#define NOOT_G4  392
#define NOOT_A4  440
#define NOOT_B4  494
#define NOOT_C5  523
#define NOOT_D5  587
#define NOOT_E5  659
#define NOOT_F5  698
#define NOOT_G5  784
#define NOOT_A5  880
#define NOOT_B5  988
#define NOOT_C6  1047
#define NOOT_D6  1175
#define NOOT_E6  1319
#define NOOT_F6  1397
#define NOOT_G6  1568
#define NOOT_A6  1760
#define NOOT_B6  1976
#define NOOT_C7  2093
#define NOOT_D7  2349
#define NOOT_E7  2637
#define NOOT_F7  2794
#define NOOT_G7  3136
#define NOOT_A7  3520
#define NOOT_B7  3951
#define NOOT_C8  4186
#define NOOT_D8  4699


 int melody[] = {
// NOOT_C4, NOOT_G3, NOOT_G3, NOOT_A3, NOOT_G3, 0, NOOT_B3, NOOT_C4
NOOT_D8, NOOT_D8, NOOT_D8, NOOT_D8, NOOT_D8, NOOT_D8
};
int NOOTDurations[] = {
 4, 4, 4, 4, 4, 4, 4, 4
 };
 void setup() {
    // iterate over the NOOTs of the melody:
  for (int thisNOOT = 0; thisNOOT < 8; thisNOOT++) {

// to calculate the NOOT duration, take one second divided by the NOOT type.
//e.g. quarter NOOT = 1000 / 4, eighth NOOT = 1000/8, etc.
int NOOTDuration = 1000 / NOOTDurations[thisNOOT];
tone(8, melody[thisNOOT], NOOTDuration);

// to distinguish the NOOTs, set a minimum time between them.
// the NOOT's duration + 30% seems to work well:
int pauseBetweenNOOTs = NOOTDuration * 1.50;
delay(pauseBetweenNOOTs);
// stop the tone playing:
noTone(8);
  }
 }

void loop() {
int melody[] = {
 // NOOT_C4, NOOT_G3, NOOT_G3, NOOT_A3, NOOT_G3, 0, NOOT_B3, NOOT_C4
 NOOT_D8, NOOT_D8, NOOT_D8, NOOT_D8, NOOT_D8, NOOT_D8
  };
 int NOOTDurations[] = {
  4, 4, 4, 4, 4, 4, 4, 4
  };

  }

How can i let the specific tone loop? Right now it's not within the loop and i am struggling to make it work in the loop. Thanks in advance!

  • So put the parts you think you need in the loop() and see what happens. If it doesn't work, post a specific question. – Mark Smith Dec 27 '17 at 14:37
1
#include "Arduino.h"

const byte TONE_PIN = 4;
const int ALARM_BEEP_1 = 4186;
const int ALARM_BEEP_2 = 4699;

const int ALARM_TONE_LENGTH = 200;
const int ALARM_TONE_PAUSE = 800;
const int ALARM_TONE_REPEAT = 6;

boolean alarm = false;

void setup()
{
  alarm = true;
}

void loop()
{
  if (alarm) {
    alarmSound();
  }
}

void alarmSound() {
  static unsigned long next = millis();
  static byte count = 0;
  if (millis() > next) {
    next += ALARM_TONE_LENGTH;
    count++;
    if (count == ALARM_TONE_REPEAT) {
      next += ALARM_TONE_PAUSE;
      count = 0;
    }
    tone(TONE_PIN, (count % 2) ? ALARM_BEEP_1 : ALARM_BEEP_2, ALARM_TONE_LENGTH);
  }
}

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.