MartinSRB

[НРС] Вежбе 15 - Задатак 4

May 12th, 2023
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 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.  
  9. #include <math.h>
  10.  
  11. extern serial Serial;
  12.  
  13. #define POTENCIOMETAR A0
  14.  
  15. typedef struct{
  16.     int vrednosti[5] = {0, 0, 0, 0, 0};
  17. } Merenja;
  18.  
  19. Merenja g_Potenciometar; //global variable Potenciometar
  20. int counter_merenja     = 0; //vezani je za funkciju ispis_test()
  21. int skip_after_printed  = 0;
  22. bool just_printed       = false;
  23.  
  24.  
  25. void ispis_test(){
  26.     Serial.print("[TEST] [");
  27.     for(int i = 0; i < 4; ++i){
  28.         Serial.print(g_Potenciometar.vrednosti[i]);
  29.         Serial.print(",");
  30.     }
  31.     Serial.print(g_Potenciometar.vrednosti[4]);
  32.     Serial.println("]");
  33. }
  34.  
  35. void load_Potenciometar_vrednost(int id, void* ptrt){
  36.     int potenciometar_vrednost = analogRead(POTENCIOMETAR);
  37.     for(int i = 1; i < 5; ++i){ //idemo do 4 jer cemo na poslednji indeks posle for-a da ubacimo novu vrednost citanja
  38.         g_Potenciometar.vrednosti[i - 1] = g_Potenciometar.vrednosti[i]; //pomeranje clanova niza za jedno mesto u levo
  39.     }
  40.     g_Potenciometar.vrednosti[4] = potenciometar_vrednost;
  41.     if(abs(g_Potenciometar.vrednosti[0] - g_Potenciometar.vrednosti[4]) > 200){
  42.         if(!just_printed){ //pravimo pauzu da ne bi spamovao prilikom ispisa do 5 puta istu liniju za sekundu da ispise (moze maks 2 puta u sekundi da prikaze poruku)
  43.             skip_after_printed = 0;
  44.             Serial.println("U zadnjih 5 ocitavanja je doslo do promene vece od 200.");
  45.             just_printed = true;
  46.         }else if(just_printed){
  47.             if(++skip_after_printed == 5){
  48.                 just_printed       = false;
  49.                 skip_after_printed = 0;
  50.             }
  51.         }
  52.     }
  53.     if(++counter_merenja == 5){
  54.         ispis_test(); //moze se zakomentarisati kad se proveri kod, ovde je samo da se vidi promena u strukturi na svakih 500[ms]
  55.         counter_merenja = 0;
  56.     }
  57. }
  58.  
  59. void setup()
  60. {
  61.     Serial.begin(9600);
  62.     pinMode(POTENCIOMETAR, INPUT);
  63.     createTask(load_Potenciometar_vrednost, 100, TASK_ENABLE, NULL);
  64. }
  65.  
  66. void loop()
  67. {
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment