Advertisement
Guest User

cvratovic_projekt

a guest
Feb 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. int NN;
  2. int LD;
  3.  
  4. int NaprijedD = 3;
  5. int NazadD = 5;
  6. const int xos = A0; // x os
  7. const int yos = A3; // y os
  8.  
  9. const int NaprijedL = 6;
  10. const int NazadL = 9;
  11.  
  12. int SrednjaVrijednostNN = 0;
  13. int SrednjaVrijednostLD = 0;
  14. int SrednjaVrijednostL = 0;
  15. int SrednjaVrijednostD = 0;
  16.  
  17. int maxSpeed = 180;
  18. int skretanje = 100;
  19.  
  20. const int numReadings = 20;
  21. const int numReadings1 = 20;
  22.  
  23. int readings[numReadings];
  24. int readIndex = 0;
  25. int total = 0;
  26. int average = 0;
  27.  
  28. int readings1[numReadings1];
  29. int readIndex1 = 0;
  30. int total1 = 0;
  31. int average1 = 0;
  32.  
  33.  
  34. void setup() {
  35.  
  36. pinMode(NaprijedD, OUTPUT);
  37. pinMode(NazadD, OUTPUT);
  38. pinMode(NaprijedL, OUTPUT);
  39. pinMode(NazadL, OUTPUT);
  40.  
  41. //Serial.begin(9600);
  42. for (int thisReading = 0; thisReading < numReadings; thisReading++) {
  43. readings[thisReading] = 0;
  44. }
  45. for (int thisReading1 = 0; thisReading1 < numReadings1; thisReading1++) {
  46. readings1[thisReading1] = 0;
  47. }
  48. }
  49.  
  50. void loop() {
  51.  
  52. NN = analogRead(xos);
  53. LD = analogRead(yos);
  54.  
  55. SrednjaVrijednostNN = gladenje(NN);
  56. SrednjaVrijednostLD = gladenje1(LD);
  57.  
  58. // IspisNN();
  59. // IspisLD();
  60. SkalirajNN();
  61. SkalirajLD();
  62.  
  63. Kretanje();
  64. }
  65.  
  66. void SkalirajNN() {
  67. if (SrednjaVrijednostNN >= 360 && SrednjaVrijednostNN < 375) {
  68. SrednjaVrijednostNN = 0;
  69. Serial.print( " Nakon " );
  70. Serial.println(SrednjaVrijednostNN);
  71.  
  72. } else if (SrednjaVrijednostNN >= 375 && SrednjaVrijednostNN < 530) {
  73. SrednjaVrijednostNN = map(SrednjaVrijednostNN, 375, 530, 0, maxSpeed * 1.25);
  74. Serial.print( " Nakon " );
  75. Serial.println(SrednjaVrijednostNN);
  76.  
  77. } else if (SrednjaVrijednostNN >= 530 ) {
  78. SrednjaVrijednostNN = maxSpeed * 1.25;
  79. Serial.print( " Nakon " );
  80. Serial.println(SrednjaVrijednostNN);
  81.  
  82. } else if (SrednjaVrijednostNN >= 250 && SrednjaVrijednostNN < 360) {
  83. SrednjaVrijednostNN = map(SrednjaVrijednostNN, 250, 360, maxSpeed * (-1), 0);
  84. Serial.print( " Nakon " );
  85. Serial.println(SrednjaVrijednostNN);
  86.  
  87. } else if (SrednjaVrijednostNN < 250 ) {
  88. SrednjaVrijednostNN = maxSpeed * (-1);
  89. Serial.print( " Nakon " );
  90. Serial.println(SrednjaVrijednostNN);
  91.  
  92. } else Serial.println();
  93. }
  94.  
  95. void SkalirajLD() {
  96. if (SrednjaVrijednostLD >= 380 && SrednjaVrijednostLD < 420) {
  97. SrednjaVrijednostLD = 0;
  98. Serial.print( " Nakon " );
  99. Serial.println(SrednjaVrijednostL);
  100.  
  101. } else if (SrednjaVrijednostLD >= 420 && SrednjaVrijednostLD < 510) {
  102. SrednjaVrijednostLD = map(SrednjaVrijednostLD, 420, 510, 0, skretanje);
  103. Serial.print( " Nakon " );
  104. Serial.println(SrednjaVrijednostL);
  105.  
  106. } else if (SrednjaVrijednostLD >= 510 ) {
  107. SrednjaVrijednostLD = skretanje;
  108. Serial.print( " Nakon " );
  109. Serial.println(SrednjaVrijednostL);
  110.  
  111. } else if (SrednjaVrijednostLD >= 220 && SrednjaVrijednostLD < 380) {
  112. SrednjaVrijednostLD = map(SrednjaVrijednostLD, 220, 380, skretanje * (-1), 0);
  113. Serial.print( " Nakon " );
  114. Serial.println(SrednjaVrijednostD);
  115.  
  116. } else if (SrednjaVrijednostLD < 220 ) {
  117. SrednjaVrijednostLD = skretanje * (-1);
  118. Serial.print( " Nakon " );
  119. Serial.println(SrednjaVrijednostD);
  120.  
  121. } else Serial.println();
  122. }
  123.  
  124. void Kretanje() {
  125.  
  126. if (SrednjaVrijednostNN >= 0) {
  127. int calc1 = SrednjaVrijednostNN + SrednjaVrijednostLD;
  128. if (calc1 < 0) {
  129. calc1 = 0;
  130. }
  131. analogWrite(NaprijedL, calc1);
  132. analogWrite(NazadL, 0);
  133.  
  134. int calc2 = SrednjaVrijednostNN - SrednjaVrijednostLD;
  135. if (calc2 < 0) {
  136. calc2 = 0;
  137. }
  138. analogWrite(NaprijedD, calc2);
  139. analogWrite(NazadD, 0);
  140.  
  141. } else if (SrednjaVrijednostNN < 0) {
  142. int calc3 = SrednjaVrijednostNN * (-1) + SrednjaVrijednostLD;
  143. if (calc3 < 0) {
  144. calc3 = 0;
  145. }
  146. analogWrite(NaprijedL, 0);
  147. analogWrite(NazadL, calc3);
  148.  
  149. int calc4 = SrednjaVrijednostNN * (-1) - SrednjaVrijednostLD;
  150. if (calc4 < 0) {
  151. calc4 = 0;
  152. }
  153. analogWrite(NaprijedD, 0);
  154. analogWrite(NazadD, calc4);
  155. }
  156. }
  157.  
  158. int gladenje(int ulaz) {
  159. // subtract the last reading:
  160. total = total - readings[readIndex];
  161. // read from the sensor:
  162. readings[readIndex] = ulaz;
  163. // add the reading to the total:
  164. total = total + readings[readIndex];
  165. // advance to the next position in the array:
  166. readIndex = readIndex + 1;
  167.  
  168. // if we're at the end of the array...
  169. if (readIndex >= numReadings) {
  170. // ...wrap around to the beginning:
  171. readIndex = 0;
  172. }
  173.  
  174. // calculate the average:
  175. average = total / numReadings;
  176. return average;
  177. }
  178.  
  179. int gladenje1(int ulaz1) {
  180. // subtract the last reading:
  181. total1 = total1 - readings1[readIndex1];
  182. // read from the sensor:
  183. readings1[readIndex1] = ulaz1;
  184. // add the reading to the total:
  185. total1 = total1 + readings1[readIndex1];
  186. // advance to the next position in the array:
  187. readIndex1 = readIndex1 + 1;
  188.  
  189. // if we're at the end of the array...
  190. if (readIndex1 >= numReadings1) {
  191. // ...wrap around to the beginning:
  192. readIndex1 = 0;
  193. }
  194.  
  195. // calculate the average:
  196. average1 = total1 / numReadings1;
  197. return average1;
  198. }
  199.  
  200. void IspisNN() {
  201. Serial.print( " PV = " ); // prvobitna vrijednost
  202.  
  203. Serial.print(NN);
  204.  
  205. Serial.print( " IV = " ); // izgladjena vrijednost
  206.  
  207. Serial.print(SrednjaVrijednostNN);
  208.  
  209. Serial.print( " PV = " );
  210. }
  211.  
  212. void IspisLD() {
  213. Serial.print( " PV = " );
  214.  
  215. Serial.print(LD);
  216.  
  217. Serial.print( " IV = " );
  218.  
  219. Serial.print(SrednjaVrijednostLD);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement