So something is wrong - but what? Maybe your fan has to much speed or some bug in your code. At first: check the input and provide some logging with a fix.
I would search a good place in the code and write it this way by providing some output
if( speed > 100 ) {
printf("speed %ld is too high. Settting max to 100", speed );
speed = 100;
}
Nevertheless you should find the
real cause of your issue.