Hubert_M

Untitled

Jun 4th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. #include <Wire.h>
  2. //10
  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[17] = { 0, 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. bool stanOK = true;
  129. void implementacja()
  130. {
  131. //if (y[16] == 0){
  132. // while(true){};
  133. // }
  134. //else {
  135. //Serial.print("dzialam");
  136. //Przypisanie otrzymanych wartosci do poziomow wody
  137. T1x1 = y[0];
  138. T1x2 = y[1];
  139. T1x3 = y[2];
  140. T2x1 = y[3];
  141. T2x2 = y[4];
  142. T2x3 = y[5];
  143. T3x1 = y[6];
  144. T3x2 = y[7];
  145. T3x3 = y[8];
  146. T4x1 = y[9];
  147. T4x2 = y[10];
  148. T4x3 = y[11];
  149.  
  150. //Przypisanie otrzymanych wartosci do poziomu termometrow
  151. T1 = y[12];
  152. T2 = y[13];
  153. T3 = y[14];
  154. T4 = y[15];
  155. //Zbiornik 1
  156. for(int i = 0; i < sizeof(y); i++){
  157. Serial.print(y[i]);
  158. Serial.print(" | ");
  159. }
  160. Serial.println();
  161. {
  162. switch (stan1)
  163. {
  164. case 1:
  165. Z1 = 1;
  166. Z2 = 0;
  167. Z3 = 0;
  168. G1 = 0;
  169.  
  170. if (T1x2) {
  171. stan1 = 2;
  172. tim1= 20;
  173. }
  174. break;
  175.  
  176. case 2:
  177. Z1 = 0;
  178. Z2 = 0;
  179. Z3 = 0;
  180. G1 = 0;
  181.  
  182. if (T1 || !tim1)
  183. stan1 = 3;
  184. break;
  185.  
  186. case 3:
  187. Z1 = 0;
  188. Z2 = 1;
  189. Z3 = 1;
  190. G1 = 0;
  191. if (!T1x1)
  192. stan1 = 1;
  193. break;
  194.  
  195. }
  196. }
  197.  
  198. //Zbiornik 2
  199. {
  200. switch (stan2) {
  201. case 1:
  202. Z4 = 0;
  203. if (T2x2)
  204. stan2 = 2;
  205. break;
  206.  
  207.  
  208. case 2:
  209. Z4 = 1;
  210. if (!T2x1)
  211. stan2 = 1;
  212. break;
  213. }
  214.  
  215. }
  216.  
  217. //Zbiornik 3
  218. {
  219. switch (stan3) {
  220. case 1:
  221. Z5 = 0;
  222. if (T3x2)
  223. stan3 = 2;
  224. break;
  225.  
  226.  
  227. case 2:
  228. Z5 = 1;
  229. if (!T3x1)
  230. stan3 = 1;
  231. break;
  232. }
  233. }
  234.  
  235. //Zbiornik 4
  236. {
  237. switch (stan4) {
  238. case 1:
  239. Z6 = 0;
  240. G4 = 1;
  241. if (T4x1)
  242. stan4 = 2;
  243. break;
  244.  
  245.  
  246. case 2:
  247. Z6 = 1;
  248. if (!T4x1)
  249. stan4 = 1;
  250. break;
  251. }
  252. }
  253.  
  254. //Przekazanie danych do obiektu
  255. x[0] = Z1;
  256. x[1] = Z2;
  257. x[2] = Z3;
  258. x[3] = Z4;
  259. x[4] = Z5;
  260. x[5] = Z6;
  261.  
  262. x[6] = G1;
  263. x[7] = G2;
  264. x[8] = G3;
  265. x[9] = G4;
  266.  
  267. x[10] = M1;
  268. x[11] = M2;
  269. x[12] = M3;
  270. x[13] = M4;
  271.  
  272. //Obsluga timerow
  273. if (tim1) --tim1;
  274. if (tim2) --tim2;
  275. if (tim3) --tim3;
  276. if (tim4) --tim4;
  277. //}
  278. }
Add Comment
Please, Sign In to add comment