Guest User

Untitled

a guest
Dec 20th, 2019
547
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. long unsigned startMicros;
  2. volatile long unsigned duration;
  3. volatile long unsigned ISRCounter = 0;
  4.  
  5. int pinButton = 5; // Pulls button LOW
  6. int pinMister = 2; // Registers press (needs to be an interrupt pin)
  7.  
  8. int delayPress = 100; //Time between HIGH and LOW toggles
  9.  
  10. void setup() {
  11.  
  12.   pinMode(pinButton,INPUT);
  13.  
  14.   pinMode(pinMister,INPUT);
  15.  
  16.   // External interrupt
  17.   attachInterrupt(digitalPinToInterrupt(pinMister), timeArrival, FALLING);
  18.  
  19.   // Waits for serial monitor before starting
  20.   while (!Serial);
  21.   Serial.begin(115200);
  22.   Serial.println("Starting!");
  23. }
  24.  
  25.  
  26. void loop() {
  27.  
  28.   // Random button presses
  29.   // delayPress = random(50,75);
  30.  
  31.   // Start timer
  32.   startMicros = micros();
  33.  
  34.   // Controller button to ground
  35.   pinMode(pinButton,OUTPUT);
  36.  
  37.   // Pause before and after button press
  38.   delay(delayPress);
  39.  
  40.   // Reset pinButton to floating
  41.   pinMode(pinButton,INPUT);
  42.  
  43.   delay(delayPress);
  44.  
  45. }
  46.  
  47.  
  48. // Interrupt function, just timestamps and prints :)
  49. void timeArrival(){
  50.  
  51.   ISRCounter += 1;
  52.   duration = micros() - startMicros;
  53.  
  54. //  Uncomment this block for running stats
  55.   clearScreen();
  56.  
  57.   Serial.print("Samples: ");
  58.   Serial.println(ISRCounter);
  59.  
  60.   Serial.print("Average: ");
  61.   Serial.print(getAverage(duration)/1000,4);  
  62.   Serial.println(" ms");
  63.  
  64.   Serial.print("Max:     ");
  65.   Serial.print(getMax(duration)/1000,3);
  66.   Serial.println(" ms");
  67.  
  68.   Serial.print("Min:     ");
  69.   Serial.print(getMin(duration)/1000,3);
  70.   Serial.println(" ms");
  71.  
  72.   Serial.print("Std Dev: ");
  73.   Serial.print(getStdDev(duration)/1000,3);
  74.   Serial.println(" ms");
  75.  
  76. // Comment out this bit if you use running stats, otherwise print latency  
  77. //  Serial.println(duration/float(1000));
  78.  
  79. }
  80.  
  81. // Clears screen between stats outputs
  82. void clearScreen(){
  83.   Serial.write(27);      
  84.   Serial.print("[2J");
  85.   Serial.write(27);
  86.   Serial.print("[H");
  87. }
  88.  
  89.  
  90. // Functions to calculate Average, Min, Max, and Std Dev
  91.  
  92. float getAverage(unsigned long int newNum){
  93.   static unsigned long int numSamples = 1;
  94.   static float curAvg;
  95.  
  96.   curAvg = curAvg + (newNum - curAvg) / numSamples;
  97.   numSamples++;
  98.  
  99.   return curAvg;
  100. }
  101.  
  102. float getMax(unsigned long int newNum){
  103.   static unsigned long int maxVal = 0;
  104.  
  105.   if(newNum > maxVal){
  106.     maxVal = newNum;
  107.   }
  108.  
  109.   return maxVal;
  110. }
  111.  
  112. float getMin(unsigned long int newNum){
  113.   static unsigned long int minVal = 4294967295;
  114.  
  115.   if(newNum < minVal){
  116.     minVal = newNum;
  117.   }
  118.  
  119.   return minVal;
  120. }
  121.  
  122.  
  123. float getStdDev(unsigned long int newNum){
  124.  
  125.     static float M = 0.0;
  126.     static float S = 0.0;
  127.     static unsigned long int i = 1;
  128.  
  129.     float tmpM = M;
  130.     M += (newNum - tmpM) / i;
  131.     S += (newNum - tmpM) * (newNum - M);
  132.     i++;
  133.  
  134.     return sqrt(S/(i-2));
  135. }
RAW Paste Data