Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SensorInput {
- public:
- byte inputPin;
- int sensorState; // the current reading from the input pin
- int lastSensorState; // the previous reading from the input pin
- // the following variables are unsigned longs because the time, measured in
- // milliseconds, will quickly become a bigger number than can be stored in an int.
- unsigned long lastDebounceTimeSensor; // the last time the output pin was toggled
- unsigned long debounceDelaySensor; // the debounce time; increase if the output flickers
- SensorInput(byte inputPin) {
- lastSensorState = HIGH;
- lastDebounceTimeSensor = 0;
- debounceDelaySensor = 50;
- this->inputPin = inputPin;
- pinMode(inputPin, INPUT_PULLUP); // sets the pin as input
- };
- bool checkState() {
- int reading = digitalRead(inputPin);
- DBGV("Sensor read: ", reading);
- // check to see if you just pressed the button
- // (i.e. the input went from LOW to HIGH), and you've waited long enough
- // since the last press to ignore any noise:
- // If the switch changed, due to noise or pressing:
- if (reading != lastSensorState) {
- // reset the debouncing timer
- lastDebounceTimeSensor = millis();
- }
- if ((millis() - lastDebounceTimeSensor) > debounceDelaySensor) {
- // whatever the reading is at, it's been there for longer than the debounce
- // delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != sensorState) {
- sensorState = reading;
- // only toggle the LED if the new button state is HIGH
- if (sensorState == LOW) {
- return 1;
- }
- }
- }
- // set the LED:
- //digitalWrite(ledPin, ledState);
- // save the reading. Next time through the loop, it'll be the lastButtonState:
- lastSensorState = reading;
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement