Photon code behaves differently when online [RESOLVED]

Hey guys,

Pretty new to the community and to the whole tinkering and programming thing. The code I have for this project of mine works perfectly while the photon is in AUTOMATIC mode, but it behaves slightly differently when I set the system mode to MANUAL.

Setup
I have 3 buttons and an RGB led setup. When I press the color’s button once, it turns on the corresponding led to 1/10th brightness, and when I press it again in increments brightness up to 10. When I hold it, it turns it off and resets brightness.

The issue:
If it’s not connected to the cloud (set to MANUAL mode), the led’s will be dim while I hold the button, and when I release the button it increments +1 brightness. While the photon is connected to the server (AUTOMATIC mode), it behaves normally and increments the brightness without ever dimming first.

This code also works fine on a standalone Arduino Uno. It’s only a problem while the Photon is offline. Any ideas?

Edit: This is the code http://pastebin.com/iZezUuVu

@escardel, reviewing your code, I suggest you look at the clickButton library in the web IDE which would make handling the buttons a lot simpler.

Without actually trying your code I can tell you that in MANUAL mode, loop() runs a LOT faster than in AUTOMATIC mode. The minor added delay of AUTOMATIC mode (up to 5ms), could be contributing to your code behaving the way it does. The fact that the codes runs on an UNO is not quite the same since the system clock for the Photon is 120MHz while the UNO’s is 16Mhz.

Here are some things to consider and try:

  • Use the clickButton library to simplify timing
  • Consider using Tone for your sound output instead of Buzz
  • Consider using millis() instead of delay() for timing and for creating non-blocking delays

:grinning:

2 Likes

Oh my god, you’re right! I added a 5 ms delay to the code and the problem went away. Obviously that’s a pretty hacky solution, but the from now on I’ll try to follow the advice you gave me. Thanks @peekay123!

1 Like