What I generally do is if I loose motivation is.
1) Stay of the computer all together for a least a couple of days to get my
head clear. Then most times the solution to the problem comes to mind
a lot easier.
2) Take a break and go watch TV and have a coffee.
3) Ensure I have comments in my code so that tracking errors is easier.
I have a good family and they keep my motivation going by compliementing me
on what I have already done and they inspires me to do better.
Anyways, I think you get the gist. The thing is, wether your a newbie or an
experianced devoloper, you will always run into problems. I reckon you have to
take a break evry now and then to avoid fatigue which can lead to being
discorouged to finish.
Hope this helps and keep at it. The rewards in the end can be worth it.