uuu000

shulhan_28_10_2025_002

Oct 28th, 2025
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1.  
  2. #include "EasyNextionLibrary.h"
  3. #include <MsTimer2.h>
  4. EasyNex myNex(Serial); // Create an object of EasyNex class with the name "myNex"
  5. /*---------------------------------------------*/ /*---------------------------------------------*/
  6. //byte buttonSwitch1 = A1; // номер входа, подключенный к кнопке
  7. //boolean buttonState1_1 = false;
  8. //boolean buttonState2_1 = false;
  9. //boolean buttonState3_1 = false;
  10. //boolean buttonSwitch1_State_old_1 = false;
  11. ///*---------------------------------------------*/
  12. //byte buttonSwitch2 = A2; // номер входа, подключенный к кнопке
  13. //boolean buttonState1_2 = false;
  14. //boolean buttonState2_2 = false;
  15. //boolean buttonState3_2= false;
  16. //boolean buttonSwitch2_State_old_2 = false;
  17. /*---------------------------------------------*/
  18. #define MEASURE_PERIOD 1000
  19. int timeCount; // счетчик времени
  20. long sum; // переменные для суммирования кодов АЦП
  21. long value; // сумма кодов АЦП (среднее значение * 500)
  22. boolean flagReady;
  23. /*---------------------------------------------*/
  24. volatile unsigned long start = 0; //переменная для хранения начального значения millis
  25. volatile unsigned long stop = 0; //переменная для хранения полученного промежутка времени
  26. volatile int once = 0; //переменная для игнорирования повторного нажатия кнопки
  27. unsigned long prev_ms = 0; //переменная для создания отрезков, чтобы не использовать delay
  28. volatile int state = LOW;
  29. /*---------------------------------------------*/
  30. int buttonState = 0;
  31. void setup() {
  32. Serial.begin(9600);
  33. myNex.begin(9600); // Begin the object with a baud rate of 9600
  34. //нужно настроить порты на чтение, читаем как использовать кнопки, как настраивать порты.
  35. // //myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
  36. // Serial.print("click btc,0");
  37. // Serial.write(0xff);
  38. // Serial.write(0xff);
  39. // Serial.write(0xff);
  40. // // myNex.writeNum("b2.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
  41. // Serial.print("click bt5,0");
  42. // Serial.write(0xff);
  43. // Serial.write(0xff);
  44. // Serial.write(0xff);
  45. /*---------------------------------------------*/
  46. pinMode(2, INPUT);
  47. digitalWrite(2, HIGH);
  48. pinMode(3, INPUT);
  49. digitalWrite(3, HIGH);
  50. // pinMode(12, INPUT);
  51. // digitalWrite(12, HIGH);
  52. // pinMode(A3, INPUT_PULLUP);
  53. // digitalWrite(A3, HIGH);
  54. // pinMode(A4, INPUT_PULLUP);
  55. // digitalWrite(A4, HIGH);
  56.  
  57. pinMode(4, OUTPUT);
  58. digitalWrite(4, LOW); // The built-in LED is initialized as an output
  59. pinMode(5, OUTPUT); // The built-in LED is initialized as an output
  60. digitalWrite(5, LOW); // Setting the built-in LED to LOW = off
  61. pinMode(6, OUTPUT);
  62. digitalWrite(6, LOW); // The built-in LED is initialized as an output
  63. pinMode(7, OUTPUT); // The built-in LED is initialized as an output
  64. digitalWrite(7, LOW); // Setting the built-in LED to LOW = off
  65. pinMode(8, OUTPUT);
  66. digitalWrite(8, LOW); // The built-in LED is initialized as an output
  67. pinMode(9, OUTPUT); // The built-in LED is initialized as an output
  68. digitalWrite(9, LOW); // Setting the built-in LED to LOW = off
  69. pinMode(10, OUTPUT); // The built-in LED is initialized as an output
  70. digitalWrite(10, LOW); // Setting the built-in LED to LOW = off
  71. pinMode(11, OUTPUT); // The built-in LED is initialized as an output
  72. digitalWrite(11, LOW); // Setting the built-in LED to LOW = off
  73. pinMode(12,OUTPUT); // The built-in LED is initialized as an output
  74. digitalWrite(12, LOW); // Setting the built-in LED to LOW = off
  75. // pinMode(13,OUTPUT); // The built-in LED is initialized as an output
  76. // digitalWrite(13, LOW); // Setting the built-in LED to LOW = off
  77. /*---------------------------------------------*/
  78.  
  79. MsTimer2::set(1, timerInterupt); // прерывания по таймеру, период 1 мс
  80. MsTimer2::start();
  81. attachInterrupt(digitalPinToInterrupt(2), begin, FALLING);
  82. attachInterrupt(digitalPinToInterrupt(3), end, FALLING);
  83. }
  84. /*---------------------------------------------*/
  85. void timerInterupt() {
  86. timeCount++; // +1 счетчик выборок усреднения
  87. sum += analogRead(A0); // суммирование кодов АЦП
  88. // проверка числа выборок усреднения
  89. if (timeCount >= MEASURE_PERIOD) {
  90. timeCount = 0;
  91. value = sum; // перегрузка среднего значения
  92. sum = 0;
  93. flagReady = true; // признак результат измерений готов
  94. }
  95. }
  96. void begin() {
  97. if (once == 0) { //проверяем, нажималась ли уже кнопка старт
  98. start = millis(); //получаем текущее значение millis
  99. once = 1; //ставим метку "старт был нажат"
  100. stop = 0; //обнуляем предыдущий замер, а можно и не обнулять :)
  101. }
  102. }
  103. ///*---------------------------------------------*/
  104. void end() {
  105. if (once == 1) { //проверяем, нажималась ли кнопка старт
  106. stop = millis() - start; //вычисляем промежуток
  107. once = 0; //убираем метку
  108. }
  109. }
  110.  
  111. /*---------------------------------------------*/
  112. void loop() {
  113. //---------------------------------------------------------
  114. if ((micros() - prev_ms) > 500) { //вывод данных каждые 500мс
  115. prev_ms = millis();
  116. myNex.NextionListen();
  117. delay(100);
  118. Serial.write(0xff);
  119. Serial.write(0xff);
  120. Serial.write(0xff);
  121. myNex.writeNum("x0.val", stop * 10);
  122. Serial.write(0xff);
  123. Serial.write(0xff);
  124. Serial.write(0xff);
  125. }
  126. /*---------------------------------------------*/
  127. if (flagReady == true) {
  128. flagReady = false;
  129. Serial.print("n222.val=");
  130. Serial.print(value / 1000);
  131. Serial.write(0xff);
  132. Serial.write(0xff);
  133. Serial.write(0xff);
  134. Serial.print("click n222,1");
  135. Serial.write(0xff);
  136. Serial.write(0xff);
  137. Serial.write(0xff);
  138. /*---------------------------------------------*/
  139. }
  140. delay(100);
  141. /*---------------------------------------------*/
  142. // // simple debounce
  143. // buttonState1_1 = digitalRead(buttonSwitch1);
  144. // delay(1);
  145. // buttonState2_1 = digitalRead(buttonSwitch1);
  146. // delay(1);
  147. // buttonState3_1 = digitalRead(buttonSwitch1);
  148. // delay(1);
  149. // /*---------------------------------------------*/
  150. // buttonState1_2 = digitalRead(buttonSwitch2);
  151. // delay(1);
  152. // buttonState2_2 = digitalRead(buttonSwitch2);
  153. // delay(1);
  154. // buttonState3_2 = digitalRead(buttonSwitch2);
  155. // delay(1);
  156. /*---------------------------------------------*/
  157. // if ((buttonState1_1 == buttonState2_1) && (buttonState1_1 == buttonState3_1)) {
  158. // // has the button switch state changed?
  159. // if (buttonState1_1 != buttonSwitch1_State_old_1) {
  160. // buttonSwitch1_State_old_1 = buttonState1_1;
  161. // if (buttonSwitch1_State_old_1 == HIGH) {
  162. //
  163. //// Serial.print("click btc,0");
  164. //// Serial.write(0xff);
  165. //// Serial.write(0xff);
  166. //// Serial.write(0xff);
  167. // } else {
  168. //
  169. //// Serial.print("click btc,1");
  170. //// Serial.write(0xff);
  171. //// Serial.write(0xff);
  172. //// Serial.write(0xff);
  173. // }
  174. // }
  175. // }
  176. // /*---------------------------------------------*/
  177. // if ((buttonState1_2 == buttonState2_2) && (buttonState1_2 == buttonState3_2)) {
  178. // // has the button switch state changed?
  179. // if (buttonState1_2 != buttonSwitch2_State_old_2) {
  180. // buttonSwitch2_State_old_2 = buttonState1_2;
  181. // if (buttonSwitch2_State_old_2 == HIGH) {
  182. // // myNex.writeNum("b2.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
  183. //// Serial.print("click bt5,0");
  184. //// Serial.write(0xff);
  185. //// Serial.write(0xff);
  186. //// Serial.write(0xff);
  187. // } else {
  188. // // myNex.writeNum("b2.bco", 63488); // Set button b0 background color to RED (color code: 63488)
  189. //// Serial.print("click bt5,1");
  190. //// Serial.write(0xff);
  191. //// Serial.write(0xff);
  192. //// Serial.write(0xff);
  193. // }
  194. // }
  195. // }
  196. /*---------------------------------------------*/
  197. }
  198. /*---------------------------------------------*/
  199. void trigger1() {
  200. digitalWrite(4, !digitalRead(4));
  201. if (digitalRead(4) == HIGH) {
  202.  
  203. } else if (digitalRead(4) == LOW) {
  204. }
  205. }
  206. /*---------------------------------------------*/
  207. void trigger2() {
  208. digitalWrite(5, !digitalRead(5));
  209. if (digitalRead(5) == HIGH) {
  210.  
  211. } else if (digitalRead(5) == LOW) {
  212. }
  213. }
  214. // /*---------------------------------------------*/
  215.  
  216. void trigger3() {
  217. digitalWrite(12, !digitalRead(12));
  218. if (digitalRead(12) == HIGH) {
  219.  
  220. } else if (digitalRead(12) == LOW) {
  221. }
  222. }
  223. // /*---------------------------------------------*/
  224. void trigger4() {
  225. digitalWrite(7, !digitalRead(7));
  226. if (digitalRead(7) == HIGH) {
  227.  
  228. } else if (digitalRead(7) == LOW) {
  229. }
  230. }
  231. // /*---------------------------------------------*/
  232. void trigger5() {
  233. digitalWrite(8, !digitalRead(8));
  234. if (digitalRead(8) == HIGH) {
  235.  
  236. } else if (digitalRead(8) == LOW) {
  237. }
  238. }
  239. // /*---------------------------------------------*/
  240. void trigger6() {
  241. digitalWrite(9, !digitalRead(9));
  242. if (digitalRead(9) == HIGH) {
  243.  
  244. } else if (digitalRead(9) == LOW) {
  245. }
  246. }
  247. // /*---------------------------------------------*/
  248. void trigger7() {
  249. digitalWrite(10, !digitalRead(10));
  250. if (digitalRead(10) == HIGH) {
  251.  
  252. } else if (digitalRead(10) == LOW) {
  253. }
  254. }
  255. // /*---------------------------------------------*/
  256. void trigger8() {
  257. digitalWrite(11, !digitalRead(11));
  258. if (digitalRead(11) == HIGH) {
  259.  
  260. } else if (digitalRead(11) == LOW) {
  261. }
  262. }
  263. // /*---------------------------------------------*/
  264. //void trigger9() {
  265. // digitalWrite(12, !digitalRead(12));
  266. // if (digitalRead(12) == HIGH) {
  267. //
  268. // } else if (digitalRead(12) == LOW) {
  269. // }
  270. //}
  271.  
  272. // /*---------------------------------------------*/
  273. //void trigger10() {
  274. // digitalWrite(13, !digitalRead(13));
  275. // if (digitalRead(13) == HIGH) {
  276. //
  277. // } else if (digitalRead(13) == LOW) {
  278. // }
  279. //}
Advertisement
Add Comment
Please, Sign In to add comment