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.
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.
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/index.jsp?productId=2062510
http://www.radioshack.com/product/index.jsp?productId=2062544