0

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 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?

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?

  • 10
    There is such a thing as "print screen" you know - makes an image that is actually readable. Also, how about posting your code, rather than a photograph of part of your code? – Majenko May 11 '15 at 22:27
  • The BIGGEST first thing to realise is that Te Jankiness is probably in your thinking and not in the micro controller. Once yo start from that assumption you have greater chance of sucess, – Russell McMahon May 12 '15 at 6:07
  • I suggest that you use {} with every if statement and also don't put two statements on the same line. This practice may seem boring but it is very important to establish good practices early – portforwardpodcast May 12 '15 at 12:02
  • 1
    It would be far, far more helpful to copy and paste the actual code into your post. Please do this. A photograph of your computer monitor, with the code extremely blurry just doesn't cut it. You can also copy and paste the serial monitor output. When pasting your code select it and hit Ctrl+K to indent it four spaces. That turns it into a code block. – Nick Gammon Jul 19 '15 at 5:14
1

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.

  • Sorry about the sub-par pictures I was in a hurry at the time. Yes I am aware of the importance of proper syntax/style, the weird structuring of the if statements is the result of muc time spent trying different things(including maximum bracketing) but they all function precisely the same. This code runs perfectly as long as j is printed. As for the answer from BrettAM this was my initial thought as well. But upon further investigation I do not believe this is the case. Because the code still malfunctions in the same way even with other serial printing if you dont specifically print j. – Josh Wiens May 13 '15 at 2:01
  • I have since written other code where this same problem persists. I will write something to specifically depict this problem since it seems to be consistent. This time I will take proper screen shots and there will be no unorthodox syntax. – Josh Wiens May 13 '15 at 2:03
  • @JoshWiens Try making j a volatile variable. It seems like a rather extreme thing to optimize out but that might be whats happening. Also, switching to using a time based function instead of a loop like I suggest should still fix the problem. FYI posting code would be prefered to screen shots. – BrettAM May 13 '15 at 2:57
-1

Yes, and I do appreciate the original answer as it is effective(something just bugs me about losing sensory capability for any amount of time), I just was not really looking for a work around but rather the engineer in me was simply interested in why the code would be responding the way it was. Anyways though I got around to writing my tester and now it's working like a charm. Maybe I was being sloppy before? Obviously I just need to do a bit more testing. I'll post the code just in case anyone is interested in seeing it(also will keep in mind the preference to actual code rather than screen shot in the future) . Thanks for the support!enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.