Miscounting FOR loop

After checking the Reference docs & the example programs & searching the forum, I did not see a similar problem solution.

I’m writing a large program in “test” pieces for my UNO using the 1.8.1 IDE. The program is to generate Morse code tones to a speaker (eventually).

I wrote a FOR loop to generate 3 short tones using TONE(). I added a print statement to see how many times the loop executed. The following code generates 2 short tones but displays 0,1,2 implying the loop executes 3 times! I would like to know why this code doesn’t generate the 3rd short tone?

The non loop letter A works fine. The dash for the letter B works fine.

unsigned long Dot = 100;
unsigned long Dash = 300;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Activate serial port - test only

}

void loop() {
// put your main code here, to run repeatedly:
// Letter A
tone(9, 440, Dot);
delay(200);
tone(9, 440, Dash);

delay(1000);

//Letter B
tone(9, 440, Dash);
delay(200);

for (int x = 0; x < 3; x++) {
tone(9, 440, Dot);
delay(200);

Serial.println(x);
}

delay(1500);
}

Use delay(1000); everywhere and report back.

I’ve never had much luck using the form of tone() which has the tone duration specified. I’ve always started the tone, waited the appropriate amount of time and then turn the tone off.

unsigned long Dot = 100;
unsigned long Dash = 300;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Activate serial port - test only
  // For Teensy
  while(!Serial);
}

void loop() {
  // put your main code here, to run repeatedly:

  // For each tone, start the tone and then wait the
  // appropriate time (Dot or Dash), turn the tone off
  // and then wait for one Dot time between each element
  // of a latter.

  // Letter A
  // Send the DIT
  tone(9, 440);
  delay(Dot);
  noTone(9);
  delay(Dot);

  // Send the DAH
  tone(9, 440);
  delay(Dash);
  noTone(9);
  
  delay(500);

  //Letter B
  tone(9, 440);
  delay(Dash);
  noTone(9);
  delay(Dot);
  for (int x = 0; x < 3; x++) {
    tone(9, 440);
    delay(Dot);
    noTone(9);
    delay(Dot);
//    Serial.println(x);
  }

  delay(1500);
}

Note that I’ve put your code in code tags so that it is easier to read and to copy and paste when testing: How to post code properly

Pete

Since Dash is 300, what do you think will happen here?

tone(9, 440, Dash) ;
delay(200) ;

.

To everyone: 1st sorry, never got an email notification that anybody responded. THANKS!

LarryD Changed 200 to 1000 which made 3 dots. Does this mean the delay executes before the tone() function completes?

Pete, That worked. Thanks!

LarryD,

I don't know what will happen! Any hints for me?

Chuck

Does this mean the delay executes before the tone() function completes?

The tone() function is non-blocking. It simply starts the noise-making process and returns. Timers handle the part about making the noise end on schedule.

Any hints for me?

Yes. Think about WHAT defines the length of the dash. It is NOT the tone() function.

PaulS

That would make sense then about tone() if non-blocking. Thanks for the clarification!

REALLY? Then I have no idea since the parameters for tone() include the duration