user29127: I am using Arduino UNO and a bluetooth module.  I have used an if condition so that Serial.println() happens only once but it keeps on printing.  Please help. Below is the code.
//Begin CODE
char str;
String fnal;
int i;

void setup()
{
  Serial.begin(9600);
  fnal="";
  i=0;
}

void loop()
{
  i++;
  if(i<2)
  {
    Serial.print('#');
    Serial.print('1');
    delay(10);
  }
}

According to this code # and 1 should be printed only once but in serial monitor the printing continues infinitely.

user10794: If you had put a Serial.print inside loop you would have seen the problem. Debugging displays are very helpful. Like this:
void loop()
{
  i++;
  Serial.println (i);   // <---- debugging
  if(i<2)
  {
    Serial.print('#');
    Serial.print('1');
    delay(10);
  }
}

Since you are adding 1 to i each time around the loop, how long do you think it will go before i overflows? It will go up and up, get to 32767 and then become -32768, and then count back to 0. During that time it will be less than 2, and it will do the printing of "#1".

user29143: It is definitely an overflow issue.
You declared i as int which ranges from -32768 to 32767.
I wrote this code as an experiment:
void loop() {
  unsigned long now = millis();
  i++;
  if(i==0){
    Serial.println(now);
  }
}

The result is i overflows every 264 ms, so if(i<2) will be true many times per second.
A simple solution is to put i++; inside the if instead, like this:
void loop()
{
  if(i<2)
  {
    i++;
    Serial.print('#');
    Serial.print('1');
    delay(10);
  }
}

i will stop incrementing when the value is 2.
No more overflow.

29127: <p>I am using Arduino UNO and a bluetooth module.  I have used an <code>if</code> condition so that <code>Serial.println()</code> happens only once but it keeps on printing.  Please help. Below is the code.</p>

<pre><code>//Begin CODE
char str;
String fnal;
int i;

void setup()
{
  Serial.begin(9600);
  fnal="";
  i=0;
}

void loop()
{
  i++;
  if(i&lt;2)
  {
    Serial.print('#');
    Serial.print('1');
    delay(10);
  }
}
</code></pre>

<p>According to this code <code>#</code> and <code>1</code> should be printed only once but in serial monitor the printing continues infinitely.</p>
 10794: <p>If you had put a Serial.print inside <code>loop</code> you would have seen the problem. Debugging displays are very helpful. Like this:</p>

<pre><code>void loop()
{
  i++;
  Serial.println (i);   // &lt;---- debugging
  if(i&lt;2)
  {
    Serial.print('#');
    Serial.print('1');
    delay(10);
  }
}
</code></pre>

<p>Since you are adding 1 to <code>i</code> each time around the loop, how long do you think it will go before <code>i</code> overflows? It will go up and up, get to 32767 and then become -32768, and then count back to 0. During that time it will be less than 2, and it will do the printing of "#1".</p>
 29143: <p>It is definitely an overflow issue.</p>

<p>You declared <code>i</code> as <code>int</code> which ranges from -32768 to 32767.
I wrote this code as an experiment:</p>

<pre><code>void loop() {
  unsigned long now = millis();
  i++;
  if(i==0){
    Serial.println(now);
  }
}
</code></pre>

<p>The result is <code>i</code> overflows every 264 ms, so <code>if(i&lt;2)</code> will be <code>true</code> many times per second.</p>

<p>A simple solution is to put <code>i++;</code> inside the <code>if</code> instead, like this:</p>

<pre><code>void loop()
{
  if(i&lt;2)
  {
    i++;
    Serial.print('#');
    Serial.print('1');
    delay(10);
  }
}
</code></pre>

<p><code>i</code> will stop incrementing when the value is 2.
No more overflow.</p>
