My U421’s have been working well for me but as my solutions got more sophisticated they got less reliable. In particular, I was outputting to an LCD, outputting to an LED, and checking (inputting from) a switch at the same time (using multiple timers). But at seemingly random times my application would crash. It makes sense that I should only do one thing with the device at a time, but all my solutions failed.
I tried suspending the conflicting timers as operations were done, but that only partially helped. Setting a global variable didn’t help at all. Toggling only the relevant bits (“WriteABit”) didn’t help either.
Of course I didn’t expect I was the first person to hit such an issue so I did some research and found that the solution for C# (and similar languages, I assume) is to use locks – an old concept for anyone using database servers, clusters, or other complex systems.
In particular, I changed my code to create a generic object:
object U421_lock = new object();
and then later whenever doing anything U421 related I wrapped the code with:
lock( U421_lock )
{
// do stuff
}