Well, the whole thing is too horrible to contemplate so we might as well look at he minutiae
This.
@little_carlos:
[patronizing graybeard mode=on]I have seen some questions posted by you, and the efforts you put in is a good thing.
Still, it seems to me that you're often programming in a not well structured way and you have a fuzzy grasp of the language.
There are some steps, I would think, you could take to improve your results to effort ratio:
1. Learn to properly indent your code, it helps a lot in understanding the relationships between the various parts and the logic behind them, both for you and for the others reading it: consider that many, me included, are put off by a "wall of code" and less inclined to spend time cleaning it up to answer your doubts.
Indentation styles are very personal, like the choice of editors, but any is better than none (I happen to like and use Allman, and can't stand GNU).
All the modern editors/IDEs support indentation and can do the work for you (Eclipse, emacs, Visual Studio, you name it, there is an ample choice of free but excellent programming environments).
2. Even more important: learn the C language from the basics. Write simple programs working from the command line (they are nearly the same in Windows, Linux and OS X as long as you remain inside standard C), work your way through the examples on a
good C programming book.
When you have mastered the control flow, the different datatypes and the memory handling (scoping, variables life, dynamic memory, pointers etc.) you'll be able to tackle your embedded programs with better understanding, and not as a series of trials and errors.
If this is your first programming language, a general algorithm and data structure book can also be useful (I would not know what to advise, though, Knuth is the definitive bible but a bit "heavy").
3. A side note (very personal opinion): PICs are fine and all, but the odd language (as far as I can see: #pragmas, void main()* etc) is not helping a learner towards good style. Other systems let you use a language much closer to standard C.
* Yes,
I know, void main() is perfectly fine and contemplated by the standard in an embedded environment, but let an old(ish) man some pet peeves.
[patronizing graybeard mode=off, but I'll keep my beard, thank you very much]
That said, have fun!
the ? operator is sometimes useful but it can make code harder to read.
I like the ? operator
I really love it, as I love the value returning rust or python ifs, but, as with a lot of what we love, care is needed.