aaron994:
So I have a vibration/shock sensor hooked up to my board throwing up values in the serial reader.
So right now you have an Arduino that reads some knock sensor (? analog or digital, link if you have it ?) and sends what it reads over the USB to the serial monitor window. Can you post that code ?
What bsagan said is basically correct, I would add that depending on the sensor you might need some code that waits for the 1’st knock to be finished before looking for the 2’nd knock. So you need to define a min and a max time between knocks. This might be a good case for learning about a simple state machine. The Arduino runs certain portions of the code depending on the “state” of the software, the “state” being held in some variable … let’s call it state.
So when state = 0 (let’s call this state “detect #1 knock”) the code runs a loop looking for the 1’st knock. What that code is will depend on the sensor. When the 1’st knock is definitely detected the “Arduino time” when that happened is stored in some variable (timeKnock1) and the state is changed to 1 (the “end #1 knock” state).
When in this state (= 1) the code tries to find when the first knock is done. This might just be a simple timer, doing nothing for XX secs or it might be looking for the knock sensor to stop indicating a knock or more likely a combo of the two ideas. When the code decides the 1’st knock is done, it goes to state = 2 (“detect #2 knock” state).
This code (state = 2) is a lot like the “detect #1 knock” state code except that there are 2 possible outcomes. Again a loop runs looking for a 2’nd knock and also asking how much “Arduino time” has elapsed since the 1’st knock was detected. Either a 2’nd knock is detected within Y secs of the 1’st knock or it won’t be. If the 2’nd knock is detected then state is changed to a 3 (the “fire pin” state). If it isn’t then state is jumped to a 4 (the “reset” state).
In the “fire pin” state (= 3) the code writes either a HIGH or a LOW to the pin you want “fired”. I might assume that there’s some timeout for that pin, that is that it eventually returns to it’s original “off” state. If so then this state will also wait for some time to pass and then go the the “reset” state. If the pin is never reset then the Arduino can just be stuck in this state until the Arduino is reset or power is removed.
In the “reset” state (= 4) the output pin is reset back to it’s initial HIGH or LOW, any detection or state transition times are reset to zero and the state is then reset back to a 0. This allows the Arduino to go back to looking for a knock knock again. The code gets to this state after timing out after “firing” the pin or timing out after not detecting a 2’nd knock.
As bsagan pointed out this could be done with a set of if…else…else… statements. In simple cases that’s often the way to go, this might be one of those cases. But since that was already suggested, I offered up a somewhat more complex concept. When thought about as a state machine you can see (I hope) how you are forced to think logically about what must happen and why.
To know “Arduino time” you might want to look at this:
http://arduino.cc/en/Reference/Millis
To see how you might get the “bones” of a state machine:
http://arduino.cc/en/Reference/SwitchCase
You would also want to know about:
http://arduino.cc/en/Reference/Else