/**************************************************** * Example of using analog input to control motor. * * This is just a continuous loop that reads the A/D * * value (this might come from an IR sensor) and * * changes the speed of a motor proportionally. We * * had to be careful about the (int) and (float) * * variables. Please use this as a very simple * * example, and not a very good one at that! * ****************************************************/ void main() { int voltage, duty_cycle; /* Some integer variables */ int motor_ch = 2; /* int motor channel set to 2 */ float drive; /* float var to contain duty cycle */ while(1) { /* loop forever */ voltage = analog(2); /* Read voltage input on ch 2 */ duty_cycle = (voltage-128); /* Bias to zero mean */ drive = (float)duty_cycle*(95./128.); /* Scale */ motor(motor_ch,(int)drive); /* Drive motor */ } }