Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int NOPin = 2; //use interrupt capable pins, connect common contact to ground.
- const int NCPin = 3;
- uint8_t NObit;
- uint8_t NOport;
- volatile boolean buttonPressed = 0;
- volatile int bounceNumber = 0;
- volatile unsigned long a = 0;
- volatile unsigned long b = 0;
- volatile unsigned long c = 0;
- volatile unsigned long d = 0;
- volatile unsigned long e = 0;
- volatile unsigned long flightDown = 0;
- volatile unsigned long flightUp = 0;
- volatile unsigned long maxSingleBounceOnPress = 0; //NO pin
- volatile unsigned long maxSingleBounceOnRelease = 0; //Also NO pin.
- volatile unsigned long minSingleContactDuration = 1000000;
- volatile unsigned long settleTime = 0;
- void setup() {
- pinMode(NOPin, INPUT_PULLUP);//Connect to switch's normally open contact
- pinMode(NCPin, INPUT_PULLUP);//Connect to switch's normally closed contact
- attachInterrupt(digitalPinToInterrupt(NOPin),NO,CHANGE);
- attachInterrupt(digitalPinToInterrupt(NCPin),NC,CHANGE);
- NOport = digitalPinToPort(NOPin);
- NObit = digitalPinToBitMask(NOPin);
- Serial.begin(9600);
- while(!Serial) delay(100);
- Serial.print("flightDown");
- Serial.print(",");
- Serial.print("flightUp");
- Serial.print(",");
- Serial.print("maxSingleBounceOnPress");
- Serial.print(",");
- Serial.print("minSingleContactDuration");
- Serial.print(",");
- Serial.print("settleTime");
- Serial.print(",");
- Serial.print("maxSingleBounceOnRelease");
- Serial.print(",");
- Serial.println("bounceNumber");
- }
- void NO(){
- a = micros();
- if (buttonPressed == 0){
- flightDown = a - b;
- d = a;
- }else{
- if (!(*portInputRegister(NOport) & NObit)){ // if NO contact is closed
- if (a-d < 10000){
- settleTime = a-d;
- bounceNumber++;
- if(a-c > maxSingleBounceOnPress){
- maxSingleBounceOnPress = a-c;
- }
- }else{
- if(a-c > maxSingleBounceOnRelease){
- maxSingleBounceOnRelease = a-c;
- }
- }
- }else{
- e=a;
- if (a-d < 10000){
- if(a-c < minSingleContactDuration){
- minSingleContactDuration = a-c;
- }
- }
- }
- }
- c = a;
- buttonPressed = 1;
- }
- void NC(){
- b = micros();
- if (buttonPressed == 1){
- flightUp = b - a;
- }
- buttonPressed = 0;
- }
- void loop() {
- if (buttonPressed == 0 && flightUp > 1){
- noInterrupts();
- Serial.print(flightDown);
- Serial.print(",");
- Serial.print(flightUp);
- Serial.print(",");
- Serial.print(maxSingleBounceOnPress);
- Serial.print(",");
- Serial.print(minSingleContactDuration);
- Serial.print(",");
- Serial.print(settleTime);
- Serial.print(",");
- Serial.print(maxSingleBounceOnRelease);
- Serial.print(",");
- Serial.println(bounceNumber);
- a = 0;
- c = 0;
- d = 0;
- e = 0;
- flightDown = 0;
- flightUp = 0;
- maxSingleBounceOnPress = 0;
- maxSingleBounceOnRelease = 0;
- minSingleContactDuration = 1000000;
- settleTime = 0;
- bounceNumber = 0;
- buttonPressed = 0;
- interrupts();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement