Advertisement
Hubert_M

Untitled

Jun 2nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define BUTTON1 2
  4. #define BUTTON2 3
  5. #define BUTTON3 4
  6. #define BUTTON4 5
  7.  
  8. #define LED1 7
  9. #define LED2 8
  10. #define LED3 9
  11. #define LED4 10
  12. #define LED5 11
  13. #define LED6 12
  14. #define LED7 13
  15.  
  16. char aK1;
  17. char aK2;
  18. char aK3;
  19. char aK4;
  20.  
  21. char L1;
  22. char L2;
  23. char L3;
  24. char L4;
  25. char L5;
  26. char L6;
  27. char L7;
  28.  
  29. byte x[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  30. byte y[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  31.  
  32. struct SerialV
  33. {
  34.   char inputs;
  35.   char outputs;
  36. };
  37.  
  38. union SerialValues
  39. {
  40.   SerialV s;
  41.   uint16_t v;
  42. };
  43.  
  44. void setup() {
  45.   // put your setup code here, to run once:
  46.   pinMode(BUTTON1, INPUT);
  47.   pinMode(BUTTON2, INPUT);
  48.   pinMode(BUTTON3, INPUT);
  49.   pinMode(BUTTON4, INPUT);
  50.  
  51.   pinMode(LED1, OUTPUT);
  52.   pinMode(LED2, OUTPUT);
  53.   pinMode(LED3, OUTPUT);
  54.   pinMode(LED4, OUTPUT);
  55.   pinMode(LED5, OUTPUT);
  56.   pinMode(LED6, OUTPUT);
  57.   pinMode(LED7, OUTPUT);
  58.  
  59.   Wire.begin(8);
  60.   Wire.onReceive(receiveEvent);  
  61.   Wire.onRequest(requestEvent);  
  62.  
  63.   noInterrupts();
  64.   TCCR1A = 0;
  65.   TCCR1B = 0;
  66.  
  67.   TCNT1 = 65536 - 6249;
  68.  
  69.   TCCR1B |= (1 << CS12);    // 256 prescaler
  70.   TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  71.   interrupts();
  72.  
  73.   Serial.begin(115200);
  74. }
  75.  
  76. void receiveEvent( int bytes ) {
  77.   for(int i = 0; i < sizeof(y); i++){
  78.     y[i] = Wire.read();
  79.   }
  80. }
  81.  
  82.  void requestEvent()  
  83. {
  84.   Wire.write(x,14);        
  85. }
  86.  
  87. ISR(TIMER1_OVF_vect)        // interrupt service routine
  88. {
  89.   TCNT1 = 65536 - 6249;   // preload timer
  90.   implementacja();
  91.  
  92.   char inputs = 0;
  93.   char outputs = 0;
  94.  
  95.   byte msg[2];
  96.  
  97.   outputs |= ((L7 && 1) << 6) | ((L6 && 1) << 5) | ((L5 && 1) << 4) | ((L4 && 1) << 3) | ((L3 && 1) << 2) |((L2 && 1) << 1) | (L1 && 1);
  98.   inputs |= ((aK4 && 1) << 3) | ((aK3 && 1) << 2) | ((aK2 && 1) << 1) | (aK1 && 1);
  99.  
  100.   msg[0] = inputs;
  101.   msg[1] = outputs;
  102. }
  103.  
  104. void loop() {
  105.  
  106.   aK1 = digitalRead(BUTTON1);
  107.   aK2 = digitalRead(BUTTON2);
  108.   aK3 = digitalRead(BUTTON3);
  109.   aK4 = digitalRead(BUTTON4);
  110.  
  111.   // put your main code here, to run repeatedly:
  112.   digitalWrite(LED1, L1);
  113.   digitalWrite(LED2, L2);
  114.   digitalWrite(LED3, L3);
  115.   digitalWrite(LED4, L4);
  116.   digitalWrite(LED5, L5);
  117.   digitalWrite(LED6, L6);
  118.   digitalWrite(LED7, L7);
  119.  
  120. }
  121. char T1x1 = 0, T1x2 = 0, T1x3 = 0, T2x1 = 0, T2x2 = 0, T2x3 = 0, T3x1 = 0, T3x2 = 0, T3x3 = 0, T4x1 = 0, T4x2 = 0, T4x3 = 0;
  122. char T1 = 0, T2 = 0, T3 = 0, T4 = 0;
  123. char Z1 = 0, Z2 = 0 , Z3 = 0, Z4 = 0, Z5 = 0, Z6 = 0;
  124. char G1 = 0, G2 = 0, G3 = 0, G4 = 0;
  125. char M1 = 0, M2 = 0, M3 = 0, M4 = 0;
  126. char stan1 = 1, stan2 = 1, stan3 = 1, stan4 = 1;
  127. char tim1 = 0, tim2 = 0, tim3 = 0, tim4 = 0;
  128. void implementacja()
  129. {
  130.   //Przypisanie otrzymanych wartosci do poziomow wody
  131.     T1x1 = y[0];
  132.     T1x2 = y[1];
  133.     T1x3 = y[2];
  134.     T2x1 = y[3];
  135.     T2x2 = y[4];
  136.     T2x3 = y[5];
  137.     T3x1 = y[6];
  138.     T3x2 = y[7];
  139.     T3x3 = y[8];
  140.     T4x1 = y[9];
  141.     T4x2 = y[10];
  142.     T4x3 = y[11];
  143.    
  144.     //Przypisanie otrzymanych wartosci do poziomu termometrow
  145.     T1 = y[12];
  146.     T2 = y[13];
  147.     T3 = y[14];
  148.     T4 = y[15];
  149.  
  150.     //Zbiornik 1
  151.     {      
  152.    
  153.     }
  154.     //Zbiornik 2
  155.     {
  156.      
  157.     }
  158.    
  159.     //Zbiornik 3
  160.     {
  161.      
  162.     }
  163.  
  164.     //Zbiornik 4
  165.     {
  166.      
  167.     }
  168.        
  169.     //Przekazanie danych do obiektu
  170.     x[0] = Z1;
  171.     x[1] = Z2;
  172.     x[2] = Z3;
  173.     x[3] = Z4;
  174.     x[4] = Z5;
  175.     x[5] = Z6;
  176.    
  177.     x[6] = G1;
  178.     x[7] = G2;
  179.     x[8] = G3;
  180.     x[9] = G4;
  181.    
  182.     x[10] = M1;
  183.     x[11] = M2;
  184.     x[12] = M3;
  185.     x[13] = M4;
  186.    
  187.     //Obsluga timerow
  188.     if(tim1) --tim1;
  189.     if(tim2) --tim2;
  190.     if(tim3) --tim3;
  191.     if(tim4) --tim4;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement