The 3 key ingredients to motivation are: autonomy, mastery, and purpose.
Nearly every time I've been unhappy writing software it has been because of a deprivation of autonomy, mastery, or purpose.
I've been ordered (deprivation of autonomy) to work on things I didn't think mattered (deprivation of purpose) with a language I wasn't familiar with in a context I wasn't familiar with, in a way I thought was wrong (deprivation of mastery and autonomy) for political rather than technical reasons (deprivation of purpose). I have never been unhappier and less motivated than that.