I am getting error while calculating duty cycle for DC motor control
unsigned char dutycycle = (255 * 300)/3700;
Giving 2 instead of 20
And for (255 * 258)/3700 giving 0 instead of 17 and so on
Don't ask anything about above formula. I want to get things done correctly.
The default data type on 16bit arduino is int which has a maximum value of 32767 and 255*300=76500, so that is the problem.
unsigned char dc = (unsigned long)(255*300) / 3700;
Thank you for your quick reply
But it does not solved my problem I have checked the same thing is repeated
You need to cast the values to a data-type which is large enough to hold them. Try this then:
unsigned char dc = ((unsigned long)255 * (unsigned long)300) / 3700;
Or like this:
unsigned long ulv = 255;
ulv *= 300;
unsigned char dc = ulv / 3700;
Or like this
unsigned char dc = (255UL * 300UL) / 3700; // Spaces added because only a fool jams everything together in an unreadable mess
Sorry to that I had not put them in neet format but I am not a "FOOL".