user9995: This is the first code I wrote for my UNO R3 just to feel out the functions and I've already got some jankiness. Throughout this description I am referring to the first two if statements one inside the other one. 
I'm trying to increment i every time the signal is activated and then j is used to create a small space of time where the button being pressed will not increment i. My problem is my if statement involving j seem to be totally ignored. I've tried multiple configurations of the if statement structure to no avail. 
Here is where things get interesting. So upon setting up the serial connection back to my PC to debug this irritating problem and printing out the actual value of j (to see why it was triggering my if statement prematurely) it magically fixed the problem. It's like until the program literally acknowledges j's value in the print statement it doesn't exist but if it does it works. Any ideas on this? 
Note: the serial monitor in both picture depicts consecutive iterations with the signal activated the entire time. 
This picture shows the serial monitor after running with the button pressed but the print statement commented out

This picture shows the serial monitor at a space where the button was pressed and held (same time frame as image 1) but in this one it increments i to 3 and then continues incrementing j. How is this happening?


user1585: Its hard to see you code, but I think I can make a pretty good guess from your description what is going on.
It seems that you are using the time it takes to increment a value as a way to slow down your code. I think you are underestimating the speed of the arduino. The time it takes to count to 200 (if I'm reading your pictures right) is almost instant; However, you then start printing to with serial each time. Once the serial buffer is full, Serial.print() will wait until there is room in the buffer to place the new value. This time is still small, but making the code wait for it to pass 200 times will take much longer than just waiting for a number to increment to 200.
I think that having added a print statement is slowing down the code enough that your increment based delay is having a noticable effect. What you should do is use the void delay(unsigned long milliseconds); function to cause the code to stop for a bit after it first detects the button has been pressed, and then see if its still pressed afterward. You can also use delayMicroseconds(unsigned int) for shorter but more precise timing. Or better yet, use the unsigned long millis(); function to grab and store the time when the button was activated, and continue polling the current time until the desired delay has been acheived. That way is more code, It allows the code to do something productive while it waits or keep looping.

9995: <p>This is the first code I wrote for my UNO R3 just to feel out the functions and I've already got some jankiness. Throughout this description I am referring to the <strong>first two if statements</strong> one inside the other one. </p>

<p>I'm trying to increment <code>i</code> every time the signal is activated and then <code>j</code> is used to create a small space of time where the button being pressed will not increment <code>i</code>. My problem is my if statement involving <code>j</code> seem to be totally ignored. I've tried multiple configurations of the if statement structure to no avail. </p>

<p>Here is where things get interesting. So upon setting up the serial connection back to my PC to debug this irritating problem and printing out the actual value of <code>j</code> (to see why it was triggering my if statement prematurely) it magically fixed the problem. It's like until the program literally acknowledges <code>j</code>'s value in the print statement it doesn't exist but if it does it works. Any ideas on this? </p>

<p>Note: the serial monitor in both picture depicts consecutive iterations with the signal activated the entire time. </p>

<p>This picture shows the serial monitor after running with the button pressed but the print statement commented out</p>

<p><img src="https://i.stack.imgur.com/adizC.jpg" alt="This picture shows the serial monitor after running with the button pressed but the print statement commented out"></p>

<p>This picture shows the serial monitor at a space where the button was pressed and held (same time frame as image 1) but in this one it increments <code>i</code> to 3 and then continues incrementing <code>j</code>. How is this happening?</p>

<p><img src="https://i.stack.imgur.com/KcVYF.jpg" alt="This picture shows the serial monitor at a space where the button was pressed and held(same time frame as img 1) but in this one it incremements i to 3 and then continues incrementing j. How is this happening?"></p>
 1585: <p>Its hard to see you code, but I think I can make a pretty good guess from your description what is going on.</p>

<p>It seems that you are using the time it takes to increment a value as a way to slow down your code. I think you are underestimating the speed of the arduino. The time it takes to count to 200 (if I'm reading your pictures right) is almost instant; However, you then start printing to with serial each time. Once the serial buffer is full, <code>Serial.print()</code> will wait until there is room in the buffer to place the new value. This time is still small, but making the code wait for it to pass 200 times will take much longer than just waiting for a number to increment to 200.</p>

<p>I think that having added a print statement is slowing down the code enough that your increment based delay is having a noticable effect. What you should do is use the <a href="http://www.arduino.cc/en/Reference/Delay" rel="nofollow"><code>void delay(unsigned long milliseconds);</code></a> function to cause the code to stop for a bit after it first detects the button has been pressed, and then see if its still pressed afterward. You can also use <a href="http://www.arduino.cc/en/Reference/delayMicroseconds" rel="nofollow"><code>delayMicroseconds(unsigned int)</code></a> for shorter but more precise timing. Or better yet, use the <a href="http://www.arduino.cc/en/Reference/Millis" rel="nofollow"><code>unsigned long millis();</code></a> function to grab and store the time when the button was activated, and continue polling the current time until the desired delay has been acheived. That way is more code, It allows the code to do something productive while it waits or keep looping.</p>
