Advertisement
Guest User

Double throw switch characterization

a guest
Dec 13th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. const int NOPin = 2; //use interrupt capable pins, connect common contact to ground.
  2. const int NCPin = 3;
  3. uint8_t NObit;
  4. uint8_t NOport;
  5. volatile boolean buttonPressed = 0;
  6. volatile int bounceNumber = 0;
  7. volatile unsigned long a = 0;
  8. volatile unsigned long b = 0;
  9. volatile unsigned long c = 0;
  10. volatile unsigned long d = 0;
  11. volatile unsigned long e = 0;
  12. volatile unsigned long flightDown = 0;
  13. volatile unsigned long flightUp = 0;
  14. volatile unsigned long maxSingleBounceOnPress = 0; //NO pin
  15. volatile unsigned long maxSingleBounceOnRelease = 0;  //Also NO pin.
  16. volatile unsigned long minSingleContactDuration = 1000000;
  17. volatile unsigned long settleTime = 0;
  18. void setup() {
  19.   pinMode(NOPin, INPUT_PULLUP);//Connect to switch's normally open contact
  20.   pinMode(NCPin, INPUT_PULLUP);//Connect to switch's normally closed contact
  21.   attachInterrupt(digitalPinToInterrupt(NOPin),NO,CHANGE);
  22.   attachInterrupt(digitalPinToInterrupt(NCPin),NC,CHANGE);
  23.   NOport = digitalPinToPort(NOPin);
  24.   NObit = digitalPinToBitMask(NOPin);
  25.   Serial.begin(9600);
  26.   while(!Serial) delay(100);
  27.   Serial.print("flightDown");
  28.   Serial.print(",");
  29.   Serial.print("flightUp");
  30.   Serial.print(",");
  31.   Serial.print("maxSingleBounceOnPress");
  32.   Serial.print(",");
  33.   Serial.print("minSingleContactDuration");
  34.   Serial.print(",");
  35.   Serial.print("settleTime");
  36.   Serial.print(",");
  37.   Serial.print("maxSingleBounceOnRelease");
  38.   Serial.print(",");
  39.   Serial.println("bounceNumber");
  40. }
  41. void NO(){
  42.   a = micros();
  43.   if (buttonPressed == 0){
  44.     flightDown = a - b;
  45.     d = a;
  46.   }else{
  47.     if (!(*portInputRegister(NOport) & NObit)){ // if NO contact is closed
  48.       if (a-d < 10000){  
  49.         settleTime = a-d;
  50.         bounceNumber++;
  51.         if(a-c > maxSingleBounceOnPress){
  52.           maxSingleBounceOnPress = a-c;
  53.         }
  54.       }else{
  55.         if(a-c > maxSingleBounceOnRelease){
  56.           maxSingleBounceOnRelease = a-c;
  57.         }
  58.       }
  59.     }else{
  60.       e=a;
  61.       if (a-d < 10000){
  62.         if(a-c < minSingleContactDuration){
  63.          minSingleContactDuration = a-c;
  64.         }
  65.       }
  66.     }
  67.   }
  68.   c = a;
  69.   buttonPressed = 1;
  70. }
  71.  
  72. void NC(){
  73.   b = micros();
  74.   if (buttonPressed == 1){
  75.     flightUp = b - a;
  76.   }
  77.   buttonPressed = 0;
  78. }
  79.  
  80. void loop() {
  81.   if (buttonPressed == 0 && flightUp > 1){
  82.     noInterrupts();
  83.     Serial.print(flightDown);
  84.     Serial.print(",");
  85.     Serial.print(flightUp);
  86.     Serial.print(",");
  87.     Serial.print(maxSingleBounceOnPress);
  88.     Serial.print(",");
  89.     Serial.print(minSingleContactDuration);
  90.     Serial.print(",");
  91.     Serial.print(settleTime);
  92.     Serial.print(",");
  93.     Serial.print(maxSingleBounceOnRelease);
  94.     Serial.print(",");
  95.     Serial.println(bounceNumber);
  96.     a = 0;
  97.     c = 0;
  98.     d = 0;
  99.     e = 0;
  100.     flightDown = 0;
  101.     flightUp = 0;
  102.     maxSingleBounceOnPress = 0;
  103.     maxSingleBounceOnRelease = 0;
  104.     minSingleContactDuration = 1000000;
  105.     settleTime = 0;
  106.     bounceNumber = 0;
  107.     buttonPressed = 0;
  108.     interrupts();
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement