Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <TID12.h>
  2.  
  3. TID MyTid(7,5,6);
  4.  
  5. const int sdv = 8;
  6. const int rpm = 9;
  7.  
  8. int sdvvalue;
  9. int rpmvalue;
  10.  
  11. float sdvfloat;
  12. float rpmfloat;
  13.  
  14. int sdvstate = 0;
  15. int rpmstate = 0;
  16. int sdvstateold = 0;
  17. int rpmstateold = 0;
  18. int rpmstatestart = 0;
  19.  
  20. int rpmstart = 0;
  21. int sdvstart = 0;
  22.  
  23. int sdvhcount = 0;
  24. float sdvhmax = 0;
  25. int sdvhimax = 0;
  26. int rpmhcount = 0;
  27.  
  28. int sdvbreak = 0;
  29. int rpmbreak = 0;
  30.  
  31. int rpm2s = 0;
  32. int rpm3s = 0;
  33.  
  34. int rpmh = 0;
  35. int rpmh2 = 0;
  36.  
  37. int sdv2s = 0;
  38.  
  39. int sdvh = 0;
  40.  
  41. unsigned long time;
  42. unsigned long timeold;
  43. unsigned long timediff;
  44. unsigned long timediff2;
  45.  
  46. char rpmdisp[8];
  47. char sdvdisp[8];
  48. char voltdisp[3];
  49. char przebiegdisp[1];
  50.  
  51. int buttonState = 0;
  52. int xd = 0;
  53. int impuls = 0;
  54. float przebieg1 = 0.0;
  55. int odczytvolt;
  56. float voltage;
  57. String show;
  58.  
  59. void setup()
  60. {
  61. MyTid.space1_init(0,7);
  62. pinMode(sdv, INPUT);
  63. pinMode(rpm, INPUT);
  64. pinMode(4,INPUT_PULLUP);
  65. pinMode(A0,INPUT);
  66. }
  67.  
  68. void loop()
  69. {
  70. buttonState = digitalRead(4);
  71. timeold = micros();
  72. if(xd == 0)
  73. {
  74. MyTid.display_message("CORSA B ",5,1);
  75.  
  76. }
  77. if (buttonState == LOW) {
  78.  
  79. if(xd <= 4)
  80. {
  81. xd += 1;
  82. }
  83. MyTid.clear_space(1);
  84. delay(2000);
  85. }
  86.  
  87. while (rpmstart == LOW)
  88. {
  89. time = micros(); //Engines RPM
  90. timediff2 = time - timeold;
  91. if (timediff2 > 40000)
  92. rpmstart = 1;
  93. rpmstateold = rpmstate;
  94. rpmstate = digitalRead(rpm);
  95. delayMicroseconds(150);
  96. if (rpmstate == HIGH && rpmstateold == LOW) //wait for rising edge
  97. rpmstart = 1;
  98. }
  99.  
  100.  
  101.  
  102. timeold = micros();
  103. while(rpmhcount < 5)
  104. {
  105. rpmstateold = rpmstate;
  106. rpmstate = digitalRead(rpm);
  107. delayMicroseconds(150);
  108. if (rpmstate == HIGH && rpmstateold == LOW) //wait for rising edge of signal
  109. rpmhcount++;
  110. time = micros(); //caching old time value...
  111. timediff2 = time - timeold; //
  112. if (timediff2 > 187500) //--> RPM < 800/min
  113. {
  114. rpmbreak = 1;
  115. break;
  116. }
  117. }
  118. time = micros();
  119.  
  120. rpmstart = 0;
  121.  
  122. rpmhcount = 0;
  123.  
  124. timediff = time - timeold;
  125.  
  126. rpmfloat = float(timediff);
  127.  
  128. rpmfloat = (1 / (rpmfloat / 1000000)) * 150; //calculate
  129.  
  130. rpmvalue = int(rpmfloat);
  131.  
  132. rpmh = rpmvalue / 1000;
  133.  
  134. rpmh2 = rpmvalue / 100;
  135.  
  136. rpm2s = rpmh2 - (rpmh * 10);
  137.  
  138. rpm3s = (rpmvalue / 10) - (rpmh2 * 10);
  139.  
  140. rpmdisp[0] = '0' + rpmh;
  141. rpmdisp[1] = '0' + rpm2s;
  142. rpmdisp[2] = '0' + rpm3s;
  143. rpmdisp[3] = '0' + (rpmvalue % 10);
  144.  
  145.  
  146. if (rpmbreak == HIGH) //display 0000 if rpm < 800
  147. {
  148. rpmdisp[0] = '0';
  149. rpmdisp[1] = '0';
  150. rpmdisp[2] = '0';
  151. rpmdisp[3] = '0';
  152.  
  153. }
  154.  
  155. rpmbreak = 0;
  156. //end rpm
  157. sdvhmax = (7 * sdvfloat) / 10; //calculate speed value based on previous measurement for increased accuaracy
  158.  
  159. sdvhimax = int(sdvhmax); //round...
  160.  
  161. sdvhmax = float(sdvhimax);
  162.  
  163. if (sdvhmax < 5) //minimum speed is about 3 km/h
  164. sdvhmax = 5;
  165.  
  166.  
  167.  
  168. timeold = micros(); //start
  169. while (sdvstart == LOW)
  170. {
  171. time = micros();
  172. timediff2 = time - timeold;
  173. if (timediff2 > 26000)
  174. sdvstart = 1;
  175. sdvstateold = sdvstate;
  176. sdvstate = digitalRead(rpm);
  177. delayMicroseconds(150);
  178. if (sdvstate == HIGH && sdvstateold == LOW) //wait for rising edge
  179. sdvstart = 1;
  180. }
  181.  
  182.  
  183. timeold = micros();
  184. while(sdvhcount < sdvhmax)
  185. {
  186. sdvstateold = sdvstate;
  187. sdvstate = digitalRead(sdv);
  188. delayMicroseconds(150);
  189. if (sdvstate == HIGH && sdvstateold == LOW)
  190. {
  191. sdvhcount++;
  192. impuls++;
  193. }
  194. //ab hier
  195. time = micros(); //cache old time
  196. timediff2 = time - timeold;
  197. if (timediff2 > 247500) //if car too slow --> 000
  198. {
  199. sdvbreak = 1;
  200. break;
  201. }
  202.  
  203. }
  204. time = micros();
  205.  
  206. sdvhcount = 0;
  207.  
  208. sdvstart = 0;
  209.  
  210. timediff = time - timeold;
  211.  
  212. sdvfloat = float(timediff);
  213.  
  214. sdvfloat = ((1 / (sdvfloat / 1000000)) * 1.96875) * (sdvhmax/10) * 1.05; //(1.750 / 32) * 10 = 0.546875 , 3.6*0.546875 , (* 1.05 to increase accuaracy) , accuarate for 165/70 R13 tyre dimensions
  215.  
  216. sdvvalue = int(sdvfloat);
  217.  
  218. sdvh = sdvvalue / 100;
  219.  
  220. sdv2s = (sdvvalue / 10) - sdvh * 10;
  221.  
  222. sdvdisp[0] = '0' + sdvh;
  223. sdvdisp[1] = '0' + sdv2s;
  224. sdvdisp[2] = '0' + (sdvvalue % 10);
  225. if (sdvbreak == HIGH) //display 000 if car is too slow
  226. {
  227. sdvdisp[0] = '0';
  228. sdvdisp[1] = '0';
  229. sdvdisp[2] = '0';
  230. }
  231.  
  232. sdvbreak = 0;
  233.  
  234. odczytvolt = analogRead(A0);
  235. voltage = odczytvolt * (15.0/285.0) + 0.6;
  236.  
  237. if(impuls >= 920)
  238. {
  239. przebieg1 += 0.1;
  240. impuls = 0;
  241. }
  242. if(xd == 1)
  243. {
  244. MyTid.display_message(String(rpmdisp+String("RPM")),1,1); //send to display...
  245. }else if(xd == 2)
  246. {
  247. MyTid.display_message(String(sdvdisp+String("KM/H")),1,1); //send to display...
  248. }else if(xd == 3)
  249. {
  250. //show = String(voltage+String("V"));
  251. //dtostrf(voltage, 6, 2, voltdisp);
  252.  
  253. show = String(voltage);
  254. show.replace('.',',');
  255. MyTid.display_message(show+String("V"),1,1);
  256. }else if(xd == 4)
  257. {
  258. show = String(przebieg1,1);
  259. show.replace('.',',');
  260. MyTid.display_message(show+String("KM"),1,1);
  261. }
  262. else if(xd == 5)
  263. {
  264. xd = 0;
  265. }
  266.  
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement