Re: Single Click On/Off Using the Trigger in EasyC
Quote:
Originally Posted by IraJason
(Post 629980)
Ok, I got the code to work, bit it took a little bit of tweaking. It may be how I set it up though. Either way, I'm able to have the cathodes toggle on and off with the trigger. This is the code I used:
Code:
if(p1_sw_trig && toggle_state == 0)
{
toggle = !toggle;
}
delay(100);
toggle_state = p1_sw_trig;
if(toggle == 1)
{
OItoRelay(1,1,1,1)
}
For some reason, without the delay, the program would have the light stay on only when the trigger was held, then not do anything on the second click. On the third click however, the cathodes would stay on after the trigger was pressed. I think it has to do with the amount of time you hold the button down, but I'm not entirely sure. If someone does have an answer to that, it would be greatly apprciated to know for future projects. And again, thank you to all who contributed to this! Doing beginners programming on a computer is a bit different than programming for a real world object.
|
It is my belief that the syntax of your IF statement when compiled is causing the comparisons at execution time to be something like this....
IF (1 && 0 == 0)
IF (0 && 0 == 0)
IF (0 && 1 == 0)
IF (1 && 1 == 0)
Instead of what you really want which is more like this
IF (0 = 1) && (0 = 0) False
IF (1 = 1) && (0 = 0) True
IF (0 = 1) && (1 = 0) False
IF (1 = 1) && (1 = 0) False
Try this syntax instead and take out the delay and see what happens.
if((p1_sw_trig == 1 ) && (toggle_state == 0))
|