MartinSRB

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

May 16th, 2023
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 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. int x1 = 0,
  12.     x2 = 0;
  13.  
  14. const int POTENCIOMETAR = A0;
  15. int fun1(){
  16.     int p_value = analogRead(POTENCIOMETAR);
  17.     int m_value = map(p_value, 0, 1023, 0, 8);
  18.     return m_value;
  19. }
  20.  
  21. const int BTN1 = 4,
  22.           BTN2 = 34;
  23. int BTN1_old_state,
  24.     BTN2_old_state;
  25. void task1(int id, void* ptrt){
  26.     if(BTN1_old_state < digitalRead(BTN1)){
  27.         int br_lampica = x1 = fun1();
  28.         for(int i = 1, j = 26; i <= br_lampica || j <= 33; ++i, ++j){
  29.             if(i <= br_lampica){
  30.                 digitalWrite(j, HIGH);
  31.             }else{
  32.                 digitalWrite(j, LOW);
  33.             }
  34.         }
  35.         BTN1_old_state = digitalRead(BTN1);
  36.     }else if(BTN1_old_state > digitalRead(BTN1)){
  37.         BTN1_old_state = digitalRead(BTN1);
  38.     }
  39.     if(BTN2_old_state < digitalRead(BTN2)){
  40.         if(Serial.available()){
  41.             char c = Serial.read();
  42.             if(c >= '0' && c <= '9'){
  43.                 Serial.print(c);
  44.             }
  45.         }
  46.         BTN2_old_state = digitalRead(BTN2);
  47.     }else if(BTN2_old_state > digitalRead(BTN2)){
  48.         BTN2_old_state = digitalRead(BTN2);
  49.     }
  50. }
  51.  
  52. typedef void interruptFunction();
  53. const int SW1 = 2;
  54. int sw1_old_state,
  55.     task1_id;
  56. interruptFunction *sw1_rising_edge  = NULL,
  57.                   *sw1_falling_edge = NULL;
  58. void sw1_attach_interrupt(short mode, interruptFunction* func){
  59.     if(mode == RISING){
  60.         sw1_rising_edge = func;
  61.     }else if(mode == FALLING){
  62.         sw1_falling_edge = func;
  63.     }
  64. }
  65. void sw1_check(int id, void* ptrt){
  66.     if(sw1_old_state < digitalRead(SW1)){
  67.         if(sw1_rising_edge != NULL){
  68.             sw1_rising_edge();
  69.         }
  70.         sw1_old_state = digitalRead(SW1);
  71.     }else if(sw1_old_state > digitalRead(SW1)){
  72.         if(sw1_falling_edge != NULL){
  73.             sw1_falling_edge();
  74.         }
  75.         sw1_old_state = digitalRead(SW1);
  76.     }
  77. }
  78. void sw1_int_on(){ //uzlazna ivica
  79.     setTaskState(task1_id, TASK_DISABLE);
  80.     for(int i = 26; i <= 33; ++i){
  81.         digitalWrite(i, LOW);
  82.     }
  83.     for(int i = 26; i <= 33; ++i){
  84.         digitalWrite(i, HIGH);
  85.         delay(100 * x1);
  86.         digitalWrite(i, LOW);
  87.     }
  88. }
  89. void sw1_int_off(){
  90.     setTaskState(task1_id, TASK_ENABLE);
  91. }
  92.  
  93. const int BTN3 = 36;
  94. int BTN3_old_state,
  95.     BTN3_start_time;
  96. void task2(int id, void* ptrt){
  97.     if(BTN3_old_state < digitalRead(BTN3)){
  98.         BTN3_start_time = millis();
  99.         BTN3_old_state = digitalRead(BTN3);
  100.     }else if(BTN3_old_state > digitalRead(BTN3)){
  101.         if(millis() - BTN3_start_time >= 1500){
  102.             Serial.println("NRS");
  103.         }
  104.         BTN3_old_state = digitalRead(BTN3);
  105.     }
  106. }
  107. void setup()
  108. {
  109.     Serial.begin(9600);
  110.     for(int i = 26; i <= 33; ++i){
  111.         pinMode(i, OUTPUT);
  112.         digitalWrite(i, LOW);
  113.     }
  114.     BTN1_old_state = digitalRead(BTN1);
  115.     BTN2_old_state = digitalRead(BTN2);
  116.     task1_id = createTask(task1, 50, TASK_ENABLE, NULL);
  117.     sw1_old_state = digitalRead(SW1);
  118.     sw1_attach_interrupt(RISING, sw1_int_on);
  119.     sw1_attach_interrupt(FALLING, sw1_int_off);
  120.     createTask(sw1_check, 50, TASK_ENABLE, NULL);
  121.     BTN3_old_state = digitalRead(BTN3);
  122.     createTask(task2, 25, TASK_ENABLE, NULL);
  123. }
  124.  
  125. void loop()
  126. {
  127.  
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment