MartinSRB

[НРС] Вежбе 17 - Задатак 2

May 14th, 2023
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 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. extern serial Serial;
  10.  
  11. #define POTENCIOMETAR A0
  12. #define BTN           4
  13. #define SW            2
  14.  
  15. typedef void interruptFunction();
  16.  
  17. int br = 0,
  18.     btn_start_time,
  19.     btn_old_state,
  20.     btn_check_id,
  21.     sw_old_state;
  22.  
  23. interruptFunction* sw_rising_func  = NULL,
  24.                  * sw_falling_func = NULL;
  25.  
  26. void btn_check(int id, void* ptrt){
  27.     if(btn_old_state < digitalRead(BTN)){
  28.         btn_start_time = millis();
  29.         btn_old_state = digitalRead(BTN);
  30.     }else if(btn_old_state > digitalRead(BTN)){
  31.         if(millis() - btn_start_time >= 2000){
  32.             br += analogRead(POTENCIOMETAR);
  33.             Serial.print("Vrednost br je: ");
  34.             Serial.println(br);
  35.         }
  36.         btn_old_state = digitalRead(BTN);
  37.     }
  38. }
  39.  
  40. void turn_all_LED_off(){
  41.     for(int i = 26; i <= 33; ++i){
  42.         digitalWrite(i, LOW);
  43.     }
  44. }
  45.  
  46. void sw_attach_interrupt(short mode, interruptFunction func){
  47.     if(mode == RISING){
  48.         sw_rising_func = func;
  49.     }else if(mode == FALLING){
  50.         sw_falling_func = func;
  51.     }
  52. }
  53.  
  54. void sw_rising(){
  55.     turn_all_LED_off();
  56.     int dec = map(analogRead(POTENCIOMETAR), 0, 1023, 0, 255);
  57.     //Serial.println(dec); -> SAMO ZA TESTIRANJE <-
  58.     int i = 26;
  59.     while(dec / 2 != 0 || i <= 33){
  60.         digitalWrite(i++, dec % 2);
  61.         dec /= 2;
  62.     }
  63. }
  64.  
  65. void sw_check(int id, void* ptrt){
  66.     if(sw_old_state < digitalRead(SW)){
  67.         sw_rising_func();
  68.         sw_old_state = digitalRead(SW);
  69.     }else if(sw_old_state > digitalRead(SW)){
  70.         sw_old_state = digitalRead(SW);
  71.     }
  72. }
  73.  
  74. void serial_check(int id, void* ptrt){
  75.     if(Serial.available()){
  76.         int broj = atoi(Serial.readString());
  77.         if(broj < br){
  78.             setTaskState(btn_check_id, TASK_DISABLE);
  79.             Serial.println("Broj koji je unet je manji od br. (onemogucavam task za btn 4)");
  80.         }
  81.     }
  82. }
  83.  
  84. void setup()
  85. {
  86.     Serial.begin(9600);
  87.     for(int i = 26; i <= 33; ++i){
  88.         pinMode(i, OUTPUT);
  89.     }
  90.     btn_old_state = digitalRead(BTN);
  91.     sw_old_state  = digitalRead(SW);
  92.     sw_attach_interrupt(RISING, sw_rising);
  93.     btn_check_id = createTask(btn_check, 50, TASK_ENABLE, NULL);
  94.     createTask(sw_check, 50, TASK_ENABLE, NULL);
  95.     createTask(serial_check, 500, TASK_ENABLE, NULL);
  96. }
  97.  
  98. void loop()
  99. {
  100.  
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment