Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Getting a button to latch its value until it is pressed again (http://www.chiefdelphi.com/forums/showthread.php?t=64449)

Ghandric 18-02-2008 10:01

Getting a button to latch its value until it is pressed again
 
Alright... when I press a button I need it to hold a value of 1 even if it is released. Then... when it is pressed again I need it to return its value to 0. To anyone who responds to this post you have all of my respect and thanks. Just to say it again to make sure I don't screw up in saying it... when I press a button I need it to hold a value of one even after it has been released. Then when i press it again i need it to return its value to 0. Thanks again.

psy_wombats 18-02-2008 10:09

Re: Getting a button to latch its value until it is pressed again
 
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.

ay2b 18-02-2008 12:08

Re: Getting a button to latch its value until it is pressed again
 
Depending on your usage, another option may be to by a pushbutton switch. Search for anything that's either "spst pushbutton" or "spdt pushbutton", but not "momentary". Here are two options from RadioShack: http://www.radioshack.com/product/in...ductId=2062510
http://www.radioshack.com/product/in...ductId=2062544


All times are GMT -5. The time now is 12:07.

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