Log in

View Full Version : Toggle Motor On & Off Help.....


programmr
16-02-2008, 07:59
Hello, does anyone know how to program in MPLab this thing... We want pwm02 to turn on if p3_sw_trig was hit. Then, if it's hit again, to shut pwm02 off. Basically like an on or off button. Any help would be greatly appreciated. Thank You.

psy_wombats
16-02-2008, 11:15
This involves storing the last state of your button, something like this:

void Run_Motor_Button (void)
static unsigned char engage_motor = 0; //Should the motor be on?
static unsigned char last_button_state = 0; //This will be the last position of p3_sw_trig
if (p3_sw_trig && !last_button_state){ //The button is pushed but wasn't last time
engage_motor = !engage_motor //Change what should be happening to the motor
}
last_button_state = p3_sw_trig; //Update the state of the button
if (engage_motor){ //Use the motor if needed
pwm02 = 255;
} else {
pwm02 = 127;
}

This is a good thing to know how to do, as it is used fairly often. I advise that instead of copy/pasting the code that you read the comments and write a similar routine yourself.