simple code question - siren

Hi,

I am trying to play a rising and falling note. I am hearing the initial note through the speaker, but no rising and falling. Can anyone see why it is not working? Many thanks :blush:

frequency = 2000;
forever{
  while((frequency) < (4000)){
    playNote(SPEAKER1,frequency,250);
    frequency = (frequency) + (1);
    
  }
  while((frequency) > (2000)){
    playNote(SPEAKER1,frequency,250);
    frequency = (frequency) - (1);
    
  }
  
}

First - post all your code!.

Second - no need for the forever (what ever that is) thats what loop() does for you.

Third - As you did not post ALL your code I have no idea what "playNote" is.

Mark

After the first while block ,frequency is 2001

After the second while block ,frequency is 2000

And then you start all over again

why do you put parentheses around almost everything ?

frequency = (frequency) + (1);

It would have been easier if you had posted all of your code but, I made a sketch that fixed your issues and prints frequency to the Serial monitor.

I commented out the lines that I could not use and added a delay to make it easier to see the results on the serial monitor.

Pay attention to the how many () I used in the beginning of the while() loops.

int frequency = 2000;

void setup()
{
  // This code will only run once, after each powerup or reset of the board
  Serial.begin(9600);
}

void loop()
{
  while(frequency < 4000)
  {
    //playNote(SPEAKER1,frequency,250);
    frequency = (frequency) + (1);
    Serial.println(frequency);
    delay(10);
  }
  Serial.println();

  while(frequency > 2000)
  {
    // playNote(SPEAKER1,frequency,250);
    frequency = (frequency) - (1);
    Serial.println(frequency);
    delay(10);
  }
  Serial.println();
}

Wow, thanks for all the replies. Sorry for the awkward code. Back to the books for me. :blush: