Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. #include <StringSplitter.h>
  2. #include <Wire.h>
  3. #include<string.h>
  4. #include "rgb_lcd.h"
  5.  
  6. #define LED 2
  7. #include<math.h>
  8. rgb_lcd lcd;
  9. const int B=4275;
  10. const int R0 = 100000;
  11. const int czujnikTemperatury = A0;
  12. const int czujnikDotyku=5;
  13.  
  14. struct
  15. {
  16. int stan;
  17. }czujnik_dotyku;
  18.  
  19. struct
  20. {
  21. int okres_wlaczenia;
  22. int okres_wylaczenia;
  23. int czas_ostatniej_zmiany;
  24. int stan;
  25. int czestotliwosc;
  26. int kaput;
  27. }dioda;
  28.  
  29. struct
  30. {
  31. int ton;
  32. int stan;
  33. int okres_wlaczenia;
  34. int okres_wylaczenia;
  35. int czas_ostatniej_zmiany;
  36. int razy;
  37. int m;
  38. } buzzer;
  39.  
  40. struct
  41. {
  42. int kelwiny;
  43. int celsjusze;
  44. int m;
  45.  
  46. }czujniktemp;
  47. struct
  48. {
  49. String tekst;
  50. int colorR;
  51. int colorG;
  52. int colorB;
  53. } ekran;
  54.  
  55. void setup() {
  56. pinMode(6, OUTPUT);
  57. pinMode(LED, OUTPUT);
  58. pinMode(czujnikDotyku, INPUT);
  59. dioda.okres_wlaczenia = 500 ;
  60. dioda.okres_wylaczenia =500 ;
  61. buzzer.okres_wlaczenia= 1000;
  62. buzzer.okres_wylaczenia= 1000;
  63. dioda.stan = LOW;
  64. dioda.czas_ostatniej_zmiany =millis();
  65. buzzer.czas_ostatniej_zmiany =millis();
  66. buzzer.stan = LOW;
  67. Serial.begin(9600);
  68. dioda.kaput=LOW;
  69. int kelwiny=LOW;
  70. int celsjusze=LOW;
  71. czujniktemp.m=0;
  72. lcd.begin(16,2);
  73. ekran.colorR=255;
  74. ekran.colorG=0;
  75. ekran.colorB=0;
  76. buzzer.m=0;
  77.  
  78.  
  79. }
  80.  
  81. void ekranik()
  82. {
  83. lcd.setRGB(ekran.colorR, ekran.colorG, ekran.colorB);
  84. }
  85.  
  86. void obsluga_diody()
  87. {
  88. int uplynelo = millis() - dioda.czas_ostatniej_zmiany;
  89. if (dioda.kaput==LOW)
  90. {
  91. if((dioda.stan==HIGH)&&(uplynelo>dioda.okres_wlaczenia))
  92. {
  93. digitalWrite(LED, LOW);
  94. dioda.stan=LOW;
  95. dioda.czas_ostatniej_zmiany =millis();
  96. }
  97. else
  98. if((dioda.stan==LOW)&&(uplynelo>dioda.okres_wlaczenia))
  99. {
  100. digitalWrite(LED, HIGH);
  101. dioda.stan=HIGH;
  102. dioda.czas_ostatniej_zmiany =millis();
  103. }
  104. }
  105.  
  106.  
  107. }
  108. void obsluga_buzzera()
  109. {int uplynelo = millis() - buzzer.czas_ostatniej_zmiany;
  110.  
  111. if (buzzer.stan==LOW)
  112. {
  113. digitalWrite(6,LOW);
  114. noTone(6);
  115. }
  116. else
  117. if((buzzer.razy!=0)&& (buzzer.stan==HIGH))
  118. {
  119. if((buzzer.stan==HIGH)&&(uplynelo>buzzer.okres_wlaczenia)&&(buzzer.m<buzzer.razy))
  120. {
  121. tone(6, buzzer.ton);
  122. buzzer.czas_ostatniej_zmiany =millis();
  123. buzzer.m++;
  124. Serial.println(buzzer.m);
  125. buzzer.stan==LOW;}
  126.  
  127. if(((buzzer.stan==LOW)&&(uplynelo>buzzer.okres_wylaczenia))||(buzzer.m>=buzzer.razy))
  128. {
  129. noTone(6);
  130. buzzer.czas_ostatniej_zmiany =millis();
  131. buzzer.stan==HIGH;
  132. }
  133.  
  134. }
  135. else
  136. if(buzzer.stan==HIGH)
  137. {
  138. digitalWrite(6,HIGH);
  139. tone(6, buzzer.ton);
  140. }
  141. }
  142.  
  143. void temperatura()
  144. {
  145. int a = analogRead(czujnikTemperatury);
  146. if ((czujniktemp.celsjusze==HIGH)&&(czujniktemp.m==0))
  147. {
  148. float R = 1023.0/a-1.0;
  149. R = R0*R;
  150. float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15;
  151. Serial.print("temperatura wynosi ");
  152. Serial.println(temperature);
  153. czujniktemp.m=1;
  154.  
  155. }
  156. else
  157. if((czujniktemp.kelwiny==HIGH)&&(czujniktemp.m==0))
  158. {
  159. Serial.print("temperatura wynosi ");
  160. Serial.println(a);
  161. czujniktemp.m=1;
  162. }
  163. }
  164. void dotyk()
  165. {
  166. czujnik_dotyku.stan = digitalRead(czujnikDotyku);
  167. if(czujnik_dotyku.stan == 1)
  168. {
  169. temperatura();
  170. }
  171. }
  172.  
  173. void loop()
  174. {
  175. obsluga_diody();
  176. obsluga_buzzera();
  177. dotyk();
  178. ekranik();
  179.  
  180. if(Serial.available())
  181. {
  182. String intxt = Serial.readString();
  183.  
  184. if((intxt.startsWith("buzzer "))&&(buzzer.stan== LOW))
  185. {buzzer.ton = intxt.substring(7).toInt();
  186. buzzer.stan=HIGH;
  187. Serial.print("buzzer włączony\n");
  188. int a;
  189.  
  190. if((a=intxt.indexOf("razy"))!=-1)
  191. {
  192. buzzer.razy=intxt.substring(a+4).toInt();
  193. Serial.println(buzzer.razy);
  194. Serial.println(a);
  195. }
  196. }
  197.  
  198. else
  199. if ((intxt == "buzzer stop\n")&&(buzzer.stan==HIGH))
  200. {buzzer.stan= LOW;
  201. Serial.print("buzzer wyłączony\n");
  202. }
  203.  
  204. else
  205. if(intxt == "info\n")
  206. {Serial.print("(C) 2020 Majkel Dzordan");}
  207.  
  208. else
  209. if(intxt==("dioda stop\n"))
  210. {dioda.kaput=HIGH;
  211. digitalWrite(LED, LOW);
  212. dioda.stan=LOW;
  213. }
  214.  
  215. else
  216. if(intxt.startsWith("dioda"))
  217. {dioda.czestotliwosc = intxt.substring(6).toInt();
  218. dioda.kaput=LOW;
  219. dioda.okres_wlaczenia=(1000/(2*dioda.czestotliwosc));
  220. dioda.okres_wylaczenia=(1000/(2*dioda.czestotliwosc));
  221. Serial.print("dioda miga ");
  222. Serial.print(dioda.czestotliwosc,DEC);
  223. Serial.print(" razy na sekundę\n");
  224. }
  225.  
  226. else
  227. if(intxt==("temp C\n"))
  228. {czujniktemp.celsjusze=HIGH;
  229. czujniktemp.kelwiny=LOW;
  230. czujniktemp.m=0;
  231. }
  232.  
  233. else
  234. if(intxt==("temp K\n"))
  235. {czujniktemp.kelwiny=HIGH;
  236. czujniktemp.celsjusze=LOW;
  237. czujniktemp.m=0;
  238. }
  239.  
  240. else
  241. if (intxt.startsWith("lcd ")&& intxt.indexOf("kolor")!=-1)
  242. {int lastStringLength = intxt.length();
  243.  
  244. ekran.tekst = intxt.substring(4,lastStringLength-19);
  245. lcd.clear();
  246. lcd.print(ekran.tekst);
  247. ekran.colorR= intxt.substring(lastStringLength-11,lastStringLength-8).toInt();
  248. ekran.colorG=intxt.substring(lastStringLength-7,lastStringLength-5).toInt();
  249. ekran.colorB=intxt.substring(lastStringLength-4,lastStringLength-1).toInt();
  250. Serial.print("lcd ok\n");
  251. }
  252. else
  253. if ((intxt.startsWith("lcd"))&& intxt.indexOf("kolor")==-1)
  254. {int lastStringLength = intxt.length();
  255. ekran.tekst=intxt.substring(4,lastStringLength-1);
  256. lcd.clear();
  257. lcd.print(ekran.tekst);
  258. Serial.print("lcd ok\n");
  259.  
  260. }
  261. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement