Need help with a two input within allotted time sketch

So I have a vibration/shock sensor hooked up to my board throwing up values in the serial reader. All is good so far. The problem I am facing now is I do not know how to articulate my idea into a code file. Here is what I am looking to do.

It is quite simple. Really I just need the arduino board to only throw an input through a specified pin if it recieves two “knocks” (vibration values over normal idle) within one second of each other. Like “tap tap”. If the knocks are two far spread time wise then the board will not fire the pin.

Could anyone help me out getting this code into a sketch? I am at a loss right now.

Thank you!

Go to the Arduino reference page and check out the timer, communication, and digital I/O sections. They would basically cover all the functions that you would need to make this program.

  1. Set up a loop function (alternatively, you could use interrupts but I’d write it as an event checker if its this simple)

  2. If statement: if sensor high, enter if statement

  3. use one of the timer functions to get a time and put it into a variable

  4. another if statement: if the time between the time you just recorded and the one you recorded the previous time through the loop is smaller than a second, trigger your output pin. Otherwise do nothing.

  5. Possibly put in a delay depending on how often you want to sample the sensor.

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

^^^ Do what Mee_n_Mac suggests haha. It will let you expand the scope of your code/project much much better and more easily than my suggested method. ^^^

Tons of commercial programming is EventDriven/StateMachine software anyhow so it’s good to learn. Really useful for implementing basic AI and autonomy as well as higher level AI. I was shocked at how similarly structured my code from an autonomous bot for a class project was to some of the code I saw for autonomously landing a fricken F22 on an aircraft carrier (obviously a billion times longer and more reliable)…