Help with dual tone siren..

Hi, Thanks before hand, for your help...

For my second project with arduino, I´m trying to emulate a multi tone siren with flashing leds, specifically this one Kingvox Siren I managed the simple tones like the high-low, low-high using "Tones.h" up to like the 8th or 9th melody on the video, even some dual tones generating a for loop with the wanted frequency on one pin and offsetting the same by 100hz the second pin and and mixing them with 1K resistors... like so...


#include <Tone.h>

Tone Tone1;
Tone Tone2;

//SIREN 1 --> TRIANGLE
void Siren1(int count, int dl)
{

while (count --)
{
int i;
for (i = 587; i <= 1397; i++) {
Tone1.play(i);
delay(dl);
}

for (i = 1397; i >= 587; i--) {
Tone1.play(i);
delay(dl);
}

}
}

// SIREN 2 --> DUAL TRIANGLE

void Siren2(int count, int dl)
{
while (count --)
{

int i;
for (i = 587; i <= 1397; i++) {
Tone1.play(i);
Tone2.play(i - 100);
delay(dl);
}

for (i = 1397; i >= 587; i--) {
Tone1.play(i);
Tone2.play(i - 100);
delay(dl);
}

}
}
void setup()
{

Tone1.begin(7);
Tone2.begin( 8 );

Siren1(7, .2);
Siren2(7, .2);
Tone2.stop();

Siren1(1, 3);
Siren2(1, 3);
Tone2.stop();

Tone1.stop();
Tone2.stop();

}

void loop() {

}


Now this works fine for me, in Siren2, Pin7 plays Tone1 and Pin8 plays Tone2 what seem simultaneously, but I ran into trouble and I can´t simulate this...

Wave

I figure it´s the same as above code but different count and delays....I just dón´t have enough coding experience to put them together in one loop or two but running concurrent on pin7 & pin8

any help would be greatly apreciated... :confused:

I figure it´s the same as above code but different count and delays

What you want is no delay()s because while they occur nothing else can happen.

Look at the BlinkWithoutDelay example in the IDE to see how to use millis() for timing without blocking the free running of the program.

Then look at Several things at the same time for an extended tutorial on the technique.

UKHeliBob : I´ve read your suggestions before many times, But was unable to implement millis(), well yes I did but with same results, what I mean is that in setup() each function was called consecutively not concurrent....

what I can´t figure out is, to use millis inside each Siren function or within loop() using millis() to switch between Siren1(7,.2) and Siren1(7,3) ??

The problem with your current code is that it executes one function then executes a second function. Each of the function calls monopolise the processor until they are complete so two things cannot happen at once.

To use millis() so that the code is not locked into a function you have to adopt a different approach. For instance, in loop() take pin 7 HIGH and record the current value of millis() (or micros() for higher frequencies). Then each time round loop() check whether the required period has passed and if so change the state of pin 7, record the time and keep going like that. If you use a fixed period you will get a fixed frequency but if you read the period from a table (an array) of values you can produce a varying frequency.

Because the code is not blocked you can also do the same thing on a different pin withing loop() to produce a different frequency on the second pin.

UKHeliBob : Understood, so I´m back to square one, again :astonished: Thats a big table even for a certain bandwith (500hz - 2000hz) at 10hz increments it´s 150 values.!!

150? You've got nearly 32000 bytes of PROGMEM to hold values. What's the problem?

CrossRoads:
150? You've got nearly 32000 bytes of PROGMEM to hold values. What's the problem?

Just saying, I´m barely learning to code I played around with the samples and basic on-off stuff, this my second real project, I have not yet learned memory limits and how many bytes each value holds...??

How can I change this function to work with millis() or micros() in loop(),

void Siren1(int count,tnum, int dl)
{

  while (count --)
  {
    int i;
    for (i = 587; i <= 1397; i++) {
      Tone1.play(i);
      delay(dl);
    }


    for (i = 1397; i >= 587; i--) {
      Tone1.play(i);
      delay(dl);
    }


  }
}

I think the delays are interfering, cause it´s getting stuck at Siren1() in the code...

#include <Tone.h>
Tone Tone1;
Tone Tone2;

unsigned long previousMillis = 2;       

// constants won't change :
const long interval = 1000;           // interval (milliseconds)

void setup() {
  Tone1.begin(7);
  Tone2.begin(8);
  
}

//SIREN 1 --> TRIANGLE
void Siren1(int count, int dl)
{

  while (count --)
  {
    
    int i;
    for (i = 587; i <= 1397; i++) {
      Tone1.play(i);
      delay(dl);
    }


    for (i = 1397; i >= 587; i--) {
      Tone1.play(i);
      delay(dl);
    }


  }
}
//SIREN 2 --> TRIANGLE
void Siren2(int count, int dl)
{

  while (count --)
  {
    int i;
    for (i = 587; i <= 1397; i++) {
      Tone2.play(i);
      delay(dl);
    }


    for (i = 1397; i >= 587; i--) {
      Tone2.play(i);
      delay(dl);
    }


  }
}

void loop()
{
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
     previousMillis = currentMillis;   
        Siren1(1,3);
        }
  else {
    previousMillis = currentMillis; 
        Siren2(7,2);
    
  }
  Tone1.stop();
  Tone2.stop();
}

:frowning: Sounds like an atari video game !!

void Siren1(int count, int dl)
{

  while (count --)
  {
    int i;

    for (i = 587; i <= 1397; i++) {
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= dl) {
        previousMillis = currentMillis;
        Tone[1].play(i);
      }


      for (i = 1397; i >= 587; i--) {
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= dl) {
          previousMillis = currentMillis;
          Tone[0].play(i);
        }


      }
    }
}
}