Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 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.  
  110. {int currentMillis = millis();
  111.  
  112. if(buzzer.razy!=0)
  113. if((currentMillis-previousMillis>buzzer.okres_wlaczenia))
  114. { previousMillis = currentMillis;
  115. if(buzzer.stan==HIGH)
  116. {
  117. digitalWrite(6,HIGH);
  118. buzzer.stan=LOW;
  119. }
  120. else
  121. {
  122. digitalWrite(6,LOW);
  123. buzzer.stan=HIGH;
  124. }
  125. buzzer.razy--;
  126. Serial.println(buzzer.razy);
  127. }
  128.  
  129. else
  130. if (buzzer.stan==LOW)
  131. {
  132. noTone(6);
  133. }
  134. else
  135. if(buzzer.stan==HIGH)
  136. {
  137. tone(6,buzzer.ton);
  138. }
  139. }
  140.  
  141.  
  142. void temperatura()
  143. {
  144. int a = analogRead(czujnikTemperatury);
  145. if ((czujniktemp.celsjusze==HIGH)&&(czujniktemp.m==0))
  146. {
  147. float R = 1023.0/a-1.0;
  148. R = R0*R;
  149. float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15;
  150. Serial.print("temperatura wynosi ");
  151. Serial.println(temperature);
  152. czujniktemp.m=1;
  153.  
  154. }
  155. else
  156. if((czujniktemp.kelwiny==HIGH)&&(czujniktemp.m==0))
  157. {
  158. Serial.print("temperatura wynosi ");
  159. Serial.println(a);
  160. czujniktemp.m=1;
  161. }
  162. }
  163. void dotyk()
  164. {
  165. czujnik_dotyku.stan = digitalRead(czujnikDotyku);
  166. if(czujnik_dotyku.stan == 1)
  167. {
  168. temperatura();
  169. }
  170. }
  171.  
  172. void loop()
  173. {
  174. obsluga_diody();
  175. obsluga_buzzera();
  176. dotyk();
  177. ekranik();
  178.  
  179. if(Serial.available())
  180. {
  181. String intxt = Serial.readString();
  182.  
  183. if((intxt.startsWith("buzzer "))&&(buzzer.stan== LOW))
  184. {buzzer.ton = intxt.substring(7).toInt();
  185. buzzer.stan=HIGH;
  186. Serial.print("buzzer włączony\n");
  187. int a;
  188.  
  189. if((a=intxt.indexOf("razy"))!=-1)
  190. {
  191. buzzer.razy=intxt.substring(a+4).toInt();
  192. Serial.println(buzzer.razy);
  193. Serial.println(a);
  194. }
  195. }
  196.  
  197. else
  198. if ((intxt == "buzzer stop\n")&&(buzzer.stan==HIGH))
  199. {buzzer.stan= LOW;
  200. Serial.print("buzzer wyłączony\n");
  201. }
  202.  
  203. else
  204. if(intxt == "info\n")
  205. {Serial.print("(C) 2020 Majkel Dzordan");}
  206.  
  207. else
  208. if(intxt==("dioda stop\n"))
  209. {dioda.kaput=HIGH;
  210. digitalWrite(LED, LOW);
  211. dioda.stan=LOW;
  212. }
  213.  
  214. else
  215. if(intxt.startsWith("dioda"))
  216. {dioda.czestotliwosc = intxt.substring(6).toInt();
  217. dioda.kaput=LOW;
  218. dioda.okres_wlaczenia=(1000/(2*dioda.czestotliwosc));
  219. dioda.okres_wylaczenia=(1000/(2*dioda.czestotliwosc));
  220. Serial.print("dioda miga ");
  221. Serial.print(dioda.czestotliwosc,DEC);
  222. Serial.print(" razy na sekundę\n");
  223. }
  224.  
  225. else
  226. if(intxt==("temp C\n"))
  227. {czujniktemp.celsjusze=HIGH;
  228. czujniktemp.kelwiny=LOW;
  229. czujniktemp.m=0;
  230. }
  231.  
  232. else
  233. if(intxt==("temp K\n"))
  234. {czujniktemp.kelwiny=HIGH;
  235. czujniktemp.celsjusze=LOW;
  236. czujniktemp.m=0;
  237. }
  238.  
  239. else
  240. if (intxt.startsWith("lcd ")&& intxt.indexOf("kolor")!=-1)
  241. {int lastStringLength = intxt.length();
  242.  
  243. ekran.tekst = intxt.substring(4,lastStringLength-19);
  244. lcd.clear();
  245. lcd.print(ekran.tekst);
  246. ekran.colorR= intxt.substring(lastStringLength-11,lastStringLength-8).toInt();
  247. ekran.colorG=intxt.substring(lastStringLength-7,lastStringLength-5).toInt();
  248. ekran.colorB=intxt.substring(lastStringLength-4,lastStringLength-1).toInt();
  249. Serial.print("lcd ok\n");
  250. }
  251. else
  252. if ((intxt.startsWith("lcd"))&& intxt.indexOf("kolor")==-1)
  253. {int lastStringLength = intxt.length();
  254. ekran.tekst=intxt.substring(4,lastStringLength-1);
  255. lcd.clear();
  256. lcd.print(ekran.tekst);
  257. Serial.print("lcd ok\n");
  258.  
  259. }
  260. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement