Joes:
ok
do we have an example of this?
What you're looking for is called edge detection. Here is an example of such using a button:
/*
- Example for "Edge Detection"
- Edge detection refers to determining the point at which a digital signal moves
- from low to high or high to low. Low to high is refered to as the 'positive' or
- rising edge. High to low is the 'negative or falling' edge. This example uses a
- button to run a commands at each edge, identifying the edge. Edges occur on a
- a button when it is either pushed or released. If the input pin is pulled high,
- pushing the button down wil be the falling edge, while releasing it will be th
- rising edge. If the input pin is pulled high, the opposite will be true.
- NOTE: This example does not utilize a debounce, so it's entirely possible that
- you'll see multiple prints if using a device, such as a button, that bounces
- during signal change.
*/// Pin used for our digital signal
const short inputPin = 5;void setup() {
Serial.begin(57600);
Serial.println("[EdgeDetection]");pinMode(inputPin, INPUT); // Set the pin as input
digitalWrite(inputPin, HIGH); // enable pull-up resistor. (Pulled high)}
void loop() {
/* Keep track of the last state of the button. A static variable will
- only be initialized the first time and will hold it's value through
- each loop. */
static short lastState = HIGH;
// Get the current state of the pin
short currentState = digitalRead(inputPin);// Check for the falling edge
if ( (currentState==HIGH) && (lastState==LOW) ) {
fallingEdge();
}
// Check for rising edge
if ( (currentState==LOW) && (lastState==HIGH) ) {
risingEdge();
}lastState = currentState;
}void fallingEdge() {
Serial.println("Falling edge");
}void risingEdge() {
Serial.println("Rising edge");
}