Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Toggle Motor On & Off Help..... (http://www.chiefdelphi.com/forums/showthread.php?t=64243)

programmr 16-02-2008 07:59

Toggle Motor On & Off Help.....
 
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

Re: Toggle Motor On & Off Help.....
 
This involves storing the last state of your button, something like this:

Code:

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.


All times are GMT -5. The time now is 18:26.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi