Car Automation Program

I'm writing a program to control the interior lights of a car. When I press a momentary button I want to be able to count the number of presses then send it to other locations of the program based on what number I stopped pressing at. The coding below allows me to detect the number of presses fine. The problem I have is how can I get it to wait to see if I press it for 2,3+ times instead of always activating on the first press? Here is the sample code:

whiteswitchstate=digitalRead(whiteswitch);
if (whiteswitchstate != whiteswitchlast)
{if (whiteswitchstate == LOW)
{whiteswitchcounter++;
Serial.println("White switch on");
Serial.print("White switch presses: ");
Serial.println(whiteswitchcounter);}
else
{Serial.println("White switch off");}
delay(50); }
whiteswitchlast=whiteswitchstate;

That is a pretty good specification of what you want it to do.

deanseavers:
I'm writing a program to control the interior lights of a car. When I press a momentary button I want to be able to count the number of presses then send it to other locations of the program based on what number I stopped pressing at. The coding below allows me to detect the number of presses fine. The problem I have is how can I get it to wait to see if I press it for 2,3+ times instead of always activating on the first press?

So which part is not not doing? It is not waiting to see if the button is pressed more than once. So you will need to write a section of code to wait (wait using millis() as delay() is not going to work). Then when the wait is over, then use the number of presses to do whatever you want.

It comes into that section of code, sees that the button was pressed the first time, then comes out with a counter value of 1 then does what I tell it to do for one press. It never has the chance to see another press.

I've tried different ways of delaying to count the button presses, but none have worked since I'm not that knowledgeable as to exactly where and how to code it. Is there a particular place in that code where I could place something like "after the button is pressed once, hang around here for five seconds and count the number of presses, then continue on with that total value"?

It never has the chance to see another press.

The point is if you want to deal with three presses in quick succession meaning something, you need to define what interval "in quick succession" means, and count the number of presses IN THAT INTERVAL before doing anything.

Is there a particular place in that code where I could place something like "after the button is pressed once, hang around here for five seconds and count the number of presses, then continue on with that total value"?

You need to record when the switch becomes pressed, if the variable to hold the time currently contains 0.

After the snippet you posted earlier, you measure the interval between now and then, if then is not 0. If then is not 0 and the interval is greater than "in quick succession", then look at the switch press count to determine what to do, reset the count to 0, reset then to 0, and do what needs doing.

Thanks so much for the quick responses and for helping me to get it working! I finally understand the concept after an all night frustration fest.

Modular programming rules! Break you requirement into pieces (modules). The first module to write is to count the number of pulses and determine when all pulses have been received (ie., there must be an amount of time from the first press to when you will stop counting and make that the final value). Then when the counting modules finishes (ie., times out), trigger the action module with the resultant count. This is a good place to call a function with the count as a parameter.