Advertisement
MartinSRB

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

May 16th, 2023
920
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.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. extern serial Serial;
  10.  
  11. /*---------- PRVA TACKA -------------*/
  12. int z1 = 0,
  13.     z2 = 0;
  14. /*----------------------------------*/
  15.  
  16. /*============= DRUGA TACKA =========*/
  17. const int POTENCIOMETAR = A0;
  18. int fun1(){
  19.     int p_value = analogRead(POTENCIOMETAR);
  20.     int map_value = map(p_value, 0, 1023, 0, 255);
  21.     return map_value;
  22. }
  23. /*====================================*/
  24. /*~~~~~~~~~~~~ TRECA TACKA ~~~~~~~~~~~~~*/
  25. const int BTN1 =  4,
  26.           BTN2 = 34;
  27. int BTN1_old_state,
  28.     BTN2_old_state;
  29. void task1(int id, void* ptrt){
  30.     if(BTN1_old_state < digitalRead(BTN1)){
  31.         int dec = fun1(),
  32.             i   = 26;
  33.         z1 = dec;
  34.         while(dec / 2 > 0 || i <= 33){    //iz decimalnog broja u binarni broj
  35.             digitalWrite(i++, dec % 2);
  36.             dec /= 2;
  37.         }
  38.         Serial.println("BTN1 je upravo pritisnut.");
  39.         BTN1_old_state = digitalRead(BTN1);
  40.     }else if(BTN1_old_state > digitalRead(BTN1)){
  41.         BTN1_old_state = digitalRead(BTN1);
  42.     }
  43.     if(BTN2_old_state < digitalRead(BTN2)){
  44.         if(Serial.available()){
  45.             char *s = Serial.readString();
  46.             Serial.println(s);
  47.         }
  48.         BTN2_old_state = digitalRead(BTN2);
  49.     }else if(BTN2_old_state > digitalRead(BTN2)){
  50.         BTN2_old_state = digitalRead(BTN2);
  51.     }
  52. }
  53. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  54. /*********** CETVRTA TACKA **************/
  55. typedef void interruptFunction();
  56. const int SW1 = 2;
  57. int task1_id,
  58.     sw1_old_state;
  59. interruptFunction* sw1_rising_edge  = NULL,
  60.                  * sw1_falling_edge = NULL;
  61. void sw1_attach_interrupt(int mode, interruptFunction* func){
  62.     if(mode == RISING){
  63.         sw1_rising_edge  = func;
  64.     }else if(mode == FALLING){
  65.         sw1_falling_edge = func;
  66.     }
  67. }
  68. void sw1_rising_edge_func(){
  69.     setTaskState(task1_id, TASK_DISABLE);
  70.     for(int i = 26; i <= 33; ++i){
  71.         digitalWrite(i, LOW);
  72.     }
  73.     for(int i = 33; i >= 26; --i){
  74.         digitalWrite(i, HIGH);
  75.         delay(z1 * 100);
  76.         digitalWrite(i, LOW);
  77.     }
  78. }
  79. void sw1_falling_edge_func(){
  80.     for(int i = 26; i <= 33; ++i){
  81.         digitalWrite(i, LOW);
  82.     }
  83.     setTaskState(task1_id, TASK_ENABLE);
  84. }
  85. void sw1_check(int id, void* ptrt){
  86.     if(sw1_old_state < digitalRead(SW1)){
  87.         sw1_rising_edge();
  88.         sw1_old_state = digitalRead(SW1);
  89.     }else if(sw1_old_state > digitalRead(SW1)){
  90.         sw1_falling_edge();
  91.         sw1_old_state = digitalRead(SW1);
  92.     }
  93. }
  94. /**************************************/
  95. /*-=-=-=-=-=-=-= PETA TACKA =-=-=-=-=-=*/
  96. const int BTN3 = 36;
  97. int BTN3_start_time,
  98.     BTN3_old_state;
  99. void task2(int id, void* ptrt){
  100.     if(BTN3_old_state < digitalRead(BTN3)){
  101.         BTN3_start_time = millis();
  102.         BTN3_old_state = digitalRead(BTN3);
  103.     }else if(BTN3_old_state > digitalRead(BTN3)){
  104.         if(millis() - BTN3_start_time >= 1500){
  105.             ++z2;
  106.             Serial.print("z2=");
  107.             Serial.println(z2);
  108.         }
  109.         BTN3_old_state = digitalRead(BTN3);
  110.     }
  111. }
  112. /*=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  113. void setup()
  114. {
  115. /*---------- PRVA TACKA -------------*/
  116. Serial.begin(9600);
  117. for(int i = 26; i <= 33; ++i){
  118.     pinMode(i, OUTPUT);
  119.     digitalWrite(i, LOW);
  120. }
  121. Serial.println("Martin Muharemovic");
  122. /*----------------------------------*/
  123. /*~~~~~~~~~~~~ TRECA TACKA ~~~~~~~~~*/
  124. BTN1_old_state = digitalRead(BTN1);
  125. BTN2_old_state = digitalRead(BTN2);
  126. task1_id = createTask(task1, 50, TASK_ENABLE, NULL);
  127. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  128. /*********** CETVRTA TACKA **************/
  129. sw1_old_state = digitalRead(SW1);
  130. sw1_attach_interrupt(RISING, sw1_rising_edge_func);
  131. sw1_attach_interrupt(FALLING, sw1_falling_edge_func);
  132. createTask(sw1_check, 50, TASK_ENABLE, NULL);
  133. /***************************************/
  134. /*-=-=-=-=-=-=-= PETA TACKA =-=-=-=-=-=*/
  135. BTN3_old_state = digitalRead(BTN3);
  136. createTask(task2, 30, TASK_ENABLE, NULL);
  137. /*=-=-=-=-=-=-=-=-==-=-=-=-=-=--=-==-=-=-=*/
  138. }
  139.  
  140. void loop()
  141. {
  142.  
  143. }
  144.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement