Hello all,
I've been having a very bizarre issue with a project of mine that for the life of me I cannot figure out.
I'm using an Arduino Uno R3 on IDE 1.6.7 to read from a photocell and manipulate mini servos based on the readouts from the photocell (essentially, it automates shiny Pokémon hunting on a 3DS). In an effort to keep this accessible for those who don't play Pokémon, I will try to keep this limited to just the functions and circuitry related to the arduino and not what it is making the game do.
The servos are utilized in sequence, attaching one servo, writing positions to it to press a button on the 3DS, and then detaching the servo (this was originally for the purpose of not drawing too high mA at once from the board, but now the servos are powered via 5v wall wart, meaning that the only connections the servos have to the board are through the connected grounds of the wall wart and the arduino and the PWM pins used to control them).
The sketch manipulates two servos (alternating pressing the up and down buttons on 3DS) in sequence until the bottom screen goes dark (indicating a battle is starting in-game), in which case it measures starting from when the screen goes dark to when it becomes light again and, based on how long the screen was dark for (using the values given from the photocell), either terminates the sequence, or presses a few buttons using other servos to end the battle in-game and restart the sequence.
The issue I'm having is that for literally no discernible reason whatsoever, the sequence will just randomly stop- the cycle will run anywhere from 3-25 times without any issue whatsoever, then will simply just bail on it.
I very highly doubt it is a memory issue, as I have similar sketches with way more variables (both globally and locally) and overall sketch size that run just fine (in fact, this sketch is just an isolated part of a much larger master sketch containing other similar modes that also happen to run just fine).
Serial is still active and connected when this happens, as I am able to run other functions as well as restart this one, and see the serial readouts that happen when said functions are run (though when this sequence randomly decides to end, the serial readouts from that point in the cycle are not shown).
I thought it must have been a power issue even though I was only running one mini servo at a time, but now that I've been powering the servos via 5v (0.7 A) wall wart and am still only actually powering one servo at any given time, I'm absolutely clueless as to what this might be.
Previous to the addition of the wall wart, I found that disabling the functions I use to hold (as opposed to simply pressing) the buttons down increased the life of the sketch somewhat, but I would still eventually come back to find the same crash later.
Link to my code:
Hopefully it isn't too messy or too much, and I will try to trim it down further to make it more readable, but I perfectly understand if it is too much of a pain to look through. I just don't have a clue what's going on or where else to turn for answers :-[
Edit: If it would be helpful, I can post a video of it in action (which hopefully would help pinpoint where the issue is).