Advertisement
Guest User

Untitled

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