user51074: I'm new to Arduino and I'm trying to use PWM to generate a tone through a buzzer. I've created a for loop, however, the for loop doesn't seem to break when the variable ms is < 1000000. 
int second = 1000000; //1 second in microseconds

void setup() {
  pinMode(3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //Play middle C for 1 second
  for(int ms=0; ms <= second; ms+=3830)
  {
    digitalWrite(3, HIGH);
    delayMicroseconds(1915);
    digitalWrite(3, LOW);
    delayMicroseconds(1915);
    Serial.println(ms);
  }
  Serial.write("Finished C");
}


We can see on the serial output that the value of ms exceeds 1000000 but the for loop is still going. Can anyone see where I am going wrong?

user10794: In addition to Majenko's answer (an int on this platform can't hold a number greater than 32767) you have a second problem. You are evidently trying to make a middle C tone (261.6 Hz) by toggling a pin with a period of 1/261.6 seconds. However your Serial.println takes time. In particular at 9600 baud each byte you send will take 1/960 of a second (because there are 8 data bits, one start bit and one stop bit per byte), and assuming the number you are printing is roughly 4 digits, plus carriage return plus linefeed, you are sending 6 bytes for each time around the loop.
6 bytes of 1/960 (1042 µs) would be 6252 µs which completely dwarves your delays of 1915 µs.
Whenever you try to do things with timing loops you need to take account that everything in the loop takes time, not just your delayMicroseconds statements.

user4143: An int can't hold 1000000.  It is a signed 16-bit variable, which means it can only hold between -32768 and +32767.
To store larger numbers you need to use long, or unsigned long which are signed and unsigned 32-bit variables.

51074: <p>I'm new to Arduino and I'm trying to use PWM to generate a tone through a buzzer. I've created a <code>for</code> loop, however, the <code>for</code> loop doesn't seem to break when the variable <code>ms</code> is &lt; 1000000. </p>

<pre><code>int second = 1000000; //1 second in microseconds

void setup() {
  pinMode(3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //Play middle C for 1 second
  for(int ms=0; ms &lt;= second; ms+=3830)
  {
    digitalWrite(3, HIGH);
    delayMicroseconds(1915);
    digitalWrite(3, LOW);
    delayMicroseconds(1915);
    Serial.println(ms);
  }
  Serial.write("Finished C");
}
</code></pre>

<p><a href="https://i.stack.imgur.com/6lg5C.png" rel="nofollow noreferrer" title="Screenshot of serial monitor"><img src="https://i.stack.imgur.com/6lg5C.png" alt="Screenshot of serial monitor" title="Screenshot of serial monitor"></a></p>

<p>We can see on the serial output that the value of <code>ms</code> exceeds 1000000 but the <code>for</code> loop is still going. Can anyone see where I am going wrong?</p>
 10794: <p>In addition to Majenko's answer (an <code>int</code> on this platform can't hold a number greater than 32767) you have a second problem. You are evidently trying to make a middle C tone (261.6 Hz) by toggling a pin with a period of 1/261.6 seconds. However your Serial.println <strong>takes time</strong>. In particular at 9600 baud each byte you send will take 1/960 of a second (because there are 8 data bits, one start bit and one stop bit per byte), and assuming the number you are printing is roughly 4 digits, plus carriage return plus linefeed, you are sending 6 bytes for each time around the loop.</p>

<p>6 bytes of 1/960 (1042 µs) would be 6252 µs which completely dwarves your delays of 1915 µs.</p>

<p>Whenever you try to do things with timing loops you need to take account that <strong>everything</strong> in the loop takes time, not just your <code>delayMicroseconds</code> statements.</p>
 4143: <p>An <code>int</code> can't hold 1000000.  It is a <em>signed 16-bit variable</em>, which means it can only hold between -32768 and +32767.</p>

<p>To store larger numbers you need to use <code>long</code>, or <code>unsigned long</code> which are signed and unsigned <em>32-bit</em> variables.</p>
