MartinSRB

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

May 16th, 2023
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 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 TAVCKA ------------*/
  12. int x1 = 0,
  13.     x2 = 0;
  14. /*-----------------------------------*/
  15. /*=========== DRUGA TACKA ===========*/
  16. const int sw[] = {2, 7, 8, 35};
  17. int fun1(){
  18.     int dec = 0;
  19.     for(int i = 0; i < 4; ++i){
  20.         dec += digitalRead(sw[i]) * pow(2, i);
  21.     }
  22.     return dec;
  23. }
  24. /*===================================*/
  25. /*~~~~~~~~~~~~ TRECA TACKA ~~~~~~~~~~*/
  26. const int BTN1 = 4,
  27.           BTN2 = 34;
  28. int BTN1_old_state,
  29.     BTN2_old_state;
  30.  
  31. void print_x1_x2(){
  32.     Serial.print("task1: x1=");
  33.     Serial.print(x1);
  34.     Serial.print(" x2=");
  35.     Serial.println(x2);
  36. }
  37.  
  38. void task1(int id, void* ptrt){
  39.     if(BTN1_old_state < digitalRead(BTN1)){
  40.         x1 = fun1();
  41.         print_x1_x2();
  42.         BTN1_old_state = digitalRead(BTN1);
  43.     }else if(BTN1_old_state > digitalRead(BTN2)){
  44.         BTN1_old_state = digitalRead(BTN1);
  45.     }
  46.     if(BTN2_old_state < digitalRead(BTN2)){
  47.         x2 = fun1();
  48.         print_x1_x2();
  49.         BTN2_old_state = digitalRead(BTN2);
  50.     }else if(BTN2_old_state > digitalRead(BTN2)){
  51.         BTN2_old_state = digitalRead(BTN2);
  52.     }
  53. }
  54. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  55. /*=============== CETVRTA TACKA ==========*/
  56. typedef void interruptFunction();
  57. interruptFunction *sw1_rising_edge, *sw2_rising_edge, *sw3_rising_edge, *sw4_rising_edge,
  58.                   *sw1_falling_edge, *sw2_falling_edge, *sw3_falling_edge, *sw4_falling_edge;
  59. int sw1_old_state,
  60.     sw2_old_state,
  61.     sw3_old_state,
  62.     sw4_old_state;
  63. void sw1_attach_interrupt(interruptFunction rise, interruptFunction fall){
  64.     sw1_rising_edge = rise;
  65.     sw1_falling_edge = fall;
  66. }
  67. void sw2_attach_interrupt(interruptFunction rise, interruptFunction fall){
  68.     sw2_rising_edge = rise;
  69.     sw2_falling_edge = fall;
  70. }
  71. void sw3_attach_interrupt(interruptFunction rise, interruptFunction fall){
  72.     sw3_rising_edge = rise;
  73.     sw3_falling_edge = fall;
  74. }
  75. void sw4_attach_interrupt(interruptFunction rise, interruptFunction fall){
  76.     sw4_rising_edge = rise;
  77.     sw4_falling_edge = fall;
  78. }
  79. void sw1_int_on(){
  80.     digitalWrite(26, HIGH);
  81. }
  82. void sw2_int_on(){
  83.     digitalWrite(27, HIGH);
  84. }
  85. void sw3_int_on(){
  86.     digitalWrite(28, HIGH);
  87. }
  88. void sw4_int_on(){
  89.     digitalWrite(29, HIGH);
  90. }
  91. void sw1_int_off(){
  92.     digitalWrite(26, LOW);
  93. }
  94. void sw2_int_off(){
  95.     digitalWrite(27, LOW);
  96. }
  97. void sw3_int_off(){
  98.     digitalWrite(28, LOW);
  99. }
  100. void sw4_int_off(){
  101.     digitalWrite(29, LOW);
  102. }
  103. void sw1_check(int id, void* ptrt){
  104.     if(sw1_old_state < digitalRead(sw[0])){
  105.         sw1_rising_edge();
  106.         sw1_old_state = digitalRead(sw[0]);
  107.     }else if(sw1_old_state > digitalRead(sw[0])){
  108.         sw1_falling_edge();
  109.         sw1_old_state = digitalRead(sw[0]);
  110.     }
  111. }
  112. void sw2_check(int id, void* ptrt){
  113.     if(sw2_old_state < digitalRead(sw[1])){
  114.         sw2_rising_edge();
  115.         sw2_old_state = digitalRead(sw[1]);
  116.     }else if(sw2_old_state > digitalRead(sw[1])){
  117.         sw2_falling_edge();
  118.         sw2_old_state = digitalRead(sw[1]);
  119.     }
  120. }
  121. void sw3_check(int id, void* ptrt){
  122.     if(sw3_old_state < digitalRead(sw[2])){
  123.         sw3_rising_edge();
  124.         sw3_old_state = digitalRead(sw[2]);
  125.     }else if(sw3_old_state > digitalRead(sw[2])){
  126.         sw3_falling_edge();
  127.         sw3_old_state = digitalRead(sw[2]);
  128.     }
  129. }
  130. void sw4_check(int id, void* ptrt){
  131.     if(sw4_old_state < digitalRead(sw[3])){
  132.         sw4_rising_edge();
  133.         sw4_old_state = digitalRead(sw[3]);
  134.     }else if(sw4_old_state > digitalRead(sw[3])){
  135.         sw4_falling_edge();
  136.         sw4_old_state = digitalRead(sw[3]);
  137.     }
  138. }
  139. /*=======================================*/
  140. /*-=-=-=-=-=-=- PETA TACKA =-=-=-=-=-=-=-=*/
  141. const int BTN3 = 36;
  142. int BTN3_old_state,
  143.     BTN3_start_time;
  144. void task2(int id, void* ptrt){
  145.     if(BTN3_old_state < digitalRead(BTN3)){
  146.         BTN3_start_time = millis();
  147.         BTN3_old_state = digitalRead(BTN3);
  148.     }else if(BTN3_old_state > digitalRead(BTN3)){
  149.         if(millis() - BTN3_start_time >= 2000){
  150.             Serial.print("Proizvod brojeva x1 i x2 je: ");
  151.             Serial.println(x1 * x2);
  152.         }
  153.         BTN3_old_state = digitalRead(BTN3);
  154.     }
  155. }
  156. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  157. void setup()
  158. {
  159. /*----------- PRVA TAVCKA ------------*/
  160.     Serial.begin(9600);
  161.     for(int i = 26; i <= 33; ++i){
  162.         pinMode(i, OUTPUT);
  163.         digitalWrite(i, LOW);
  164.     }
  165.     Serial.println("Martin Muharemovic - PR130/2021 Grupa 7");
  166. /*-----------------------------------------*/
  167. /*~~~~~~~~~~~~ TRECA TACKA ~~~~~~~~~~*/
  168. BTN1_old_state = digitalRead(BTN1);
  169. BTN2_old_state = digitalRead(BTN2);
  170. createTask(task1, 25, TASK_ENABLE, NULL);
  171. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  172. /*=============== CETVRTA TACKA ==========*/
  173. sw1_old_state = digitalRead(sw[0]);
  174. sw2_old_state = digitalRead(sw[1]);
  175. sw3_old_state = digitalRead(sw[2]);
  176. sw4_old_state = digitalRead(sw[3]);
  177. sw1_attach_interrupt(sw1_int_on,sw1_int_off);
  178. sw2_attach_interrupt(sw2_int_on,sw2_int_off);
  179. sw3_attach_interrupt(sw3_int_on,sw3_int_off);
  180. sw4_attach_interrupt(sw4_int_on,sw4_int_off);
  181. createTask(sw1_check, 50, TASK_ENABLE, NULL);
  182. createTask(sw2_check, 50, TASK_ENABLE, NULL);
  183. createTask(sw3_check, 50, TASK_ENABLE, NULL);
  184. createTask(sw4_check, 50, TASK_ENABLE, NULL);
  185. /*========================================*/
  186. /*-=-=-=-=-=-=- PETA TACKA =-=-=-=-=-=-=-=*/
  187. BTN3_old_state = digitalRead(BTN3);
  188. createTask(task2, 25, TASK_ENABLE, NULL);
  189. /*=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-*/
  190. }
  191.  
  192. void loop()
  193. {
  194.  
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment