In almost all cases I would recommend charging money to do this work in addition to any shares you take in the company. If they pay for the software you write, it means they will value it in line with the cost. If you put the hours in and don't charge, they don't have anything to lose if they abandon the project and if you rely on their knowledge of the market and their contacts, you can't do anything with the application you have written.

I have noticed that customers who pay a reasonable amount value your contribution, be it code or advice, more than customers who don't pay or who pay a discounted rate.