MartinSRB

[НРС] Колоквијум - Група 3А

Jun 4th, 2023
1,136
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 1 0
  1. #include<arduinoPlatform.h>
  2. #include<tasks.h>
  3. #include<interrupts.h>
  4. #include<stdio.h>
  5. #include<serial.h>
  6. #include <pwm.h>
  7. #include<data.h>
  8. #include <ctime>
  9.  
  10. extern serial Serial;
  11.  
  12. const int pin          = 26,
  13.           measureCount = 20;
  14. int oldState,
  15.     startTime,
  16.     elapsedTime = 0,
  17.     periodTime  = 0,
  18.     tempSum     = 0,
  19.     tempCnt     = 0,
  20.     lastAvrg    = -1;
  21.  
  22. void tracking(int id, void* ptrt){
  23.     static int first = true;
  24.     bool error = false;
  25.     int newState = digitalRead(pin);
  26.     if(oldState > newState){
  27.         elapsedTime = millis() - startTime;
  28.     }else if(oldState < newState){
  29.         int timeLocal   = millis();
  30.         periodTime = timeLocal - startTime;
  31.         startTime  = timeLocal;
  32.         if(!first){
  33.             Serial.print("(");
  34.             Serial.print(tempCnt + 1);
  35.             Serial.print(".) ");
  36.             Serial.print(elapsedTime);
  37.             Serial.print(' ');
  38.             Serial.println(periodTime);
  39.             int temperature = map(elapsedTime, 0, 1000, 0, 45);
  40.             if(lastAvrg != -1){
  41.                 if((temperature > (lastAvrg + (lastAvrg / 2))) ||
  42.                     (temperature < (lastAvrg - (lastAvrg / 2)))){
  43.                     time_t now = time(0);
  44.                     char* date = ctime(&now);
  45.                     Serial.print("[ERROR] Temperature is 50% diff from last avrg (");
  46.                     Serial.print(temperature);
  47.                     Serial.print(") [");
  48.                     Serial.print(date);
  49.                     Serial.println("]");
  50.                     error = true;
  51.                 }
  52.             }
  53.             if(!error){
  54.                 tempSum += temperature;
  55.             }else{
  56.                 tempSum += lastAvrg;
  57.             }
  58.             if(tempCnt++ == 19){
  59.                 int avrg = tempSum / measureCount;
  60.                 tempCnt = 0;
  61.                 Serial.print("[AVERAGE] Average temperature last 20 measurings is ");
  62.                 Serial.println(avrg);
  63.                 if(lastAvrg == avrg){
  64.                     Serial.println("[INFO] This average is the same as the one before.");
  65.                 }
  66.                 lastAvrg = avrg;
  67.                 tempSum = 0;
  68.                 tempCnt = 0;
  69.             }
  70.         }else{
  71.             first = false;
  72.         }
  73.     }
  74.     oldState = newState;
  75. }
  76.  
  77. void setup()
  78. {
  79.     Serial.begin(9600);
  80.     oldState = digitalRead(pin);
  81.     startTime = millis();
  82.     pwmSin(pin, 1000, 0.1);
  83.     createTask(tracking, 1, TASK_ENABLE, NULL);
  84. }
  85.  
  86. void loop()
  87. {
  88.  
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment