Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EasyNextionLibrary.h"
- #include <MsTimer2.h>
- EasyNex myNex(Serial); // Create an object of EasyNex class with the name "myNex"
- /*---------------------------------------------*/ /*---------------------------------------------*/
- //byte buttonSwitch1 = A1; // номер входа, подключенный к кнопке
- //boolean buttonState1_1 = false;
- //boolean buttonState2_1 = false;
- //boolean buttonState3_1 = false;
- //boolean buttonSwitch1_State_old_1 = false;
- ///*---------------------------------------------*/
- //byte buttonSwitch2 = A2; // номер входа, подключенный к кнопке
- //boolean buttonState1_2 = false;
- //boolean buttonState2_2 = false;
- //boolean buttonState3_2= false;
- //boolean buttonSwitch2_State_old_2 = false;
- /*---------------------------------------------*/
- #define MEASURE_PERIOD 1000
- int timeCount; // счетчик времени
- long sum; // переменные для суммирования кодов АЦП
- long value; // сумма кодов АЦП (среднее значение * 500)
- boolean flagReady;
- /*---------------------------------------------*/
- volatile unsigned long start = 0; //переменная для хранения начального значения millis
- volatile unsigned long stop = 0; //переменная для хранения полученного промежутка времени
- volatile int once = 0; //переменная для игнорирования повторного нажатия кнопки
- unsigned long prev_ms = 0; //переменная для создания отрезков, чтобы не использовать delay
- volatile int state = LOW;
- /*---------------------------------------------*/
- int buttonState = 0;
- void setup() {
- Serial.begin(9600);
- myNex.begin(9600); // Begin the object with a baud rate of 9600
- //нужно настроить порты на чтение, читаем как использовать кнопки, как настраивать порты.
- // //myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
- // Serial.print("click btc,0");
- // Serial.write(0xff);
- // Serial.write(0xff);
- // Serial.write(0xff);
- // // myNex.writeNum("b2.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
- // Serial.print("click bt5,0");
- // Serial.write(0xff);
- // Serial.write(0xff);
- // Serial.write(0xff);
- /*---------------------------------------------*/
- pinMode(2, INPUT);
- digitalWrite(2, HIGH);
- pinMode(3, INPUT);
- digitalWrite(3, HIGH);
- // pinMode(12, INPUT);
- // digitalWrite(12, HIGH);
- // pinMode(A3, INPUT_PULLUP);
- // digitalWrite(A3, HIGH);
- // pinMode(A4, INPUT_PULLUP);
- // digitalWrite(A4, HIGH);
- pinMode(4, OUTPUT);
- digitalWrite(4, LOW); // The built-in LED is initialized as an output
- pinMode(5, OUTPUT); // The built-in LED is initialized as an output
- digitalWrite(5, LOW); // Setting the built-in LED to LOW = off
- pinMode(6, OUTPUT);
- digitalWrite(6, LOW); // The built-in LED is initialized as an output
- pinMode(7, OUTPUT); // The built-in LED is initialized as an output
- digitalWrite(7, LOW); // Setting the built-in LED to LOW = off
- pinMode(8, OUTPUT);
- digitalWrite(8, LOW); // The built-in LED is initialized as an output
- pinMode(9, OUTPUT); // The built-in LED is initialized as an output
- digitalWrite(9, LOW); // Setting the built-in LED to LOW = off
- pinMode(10, OUTPUT); // The built-in LED is initialized as an output
- digitalWrite(10, LOW); // Setting the built-in LED to LOW = off
- pinMode(11, OUTPUT); // The built-in LED is initialized as an output
- digitalWrite(11, LOW); // Setting the built-in LED to LOW = off
- pinMode(12,OUTPUT); // The built-in LED is initialized as an output
- digitalWrite(12, LOW); // Setting the built-in LED to LOW = off
- // pinMode(13,OUTPUT); // The built-in LED is initialized as an output
- // digitalWrite(13, LOW); // Setting the built-in LED to LOW = off
- /*---------------------------------------------*/
- MsTimer2::set(1, timerInterupt); // прерывания по таймеру, период 1 мс
- MsTimer2::start();
- attachInterrupt(digitalPinToInterrupt(2), begin, FALLING);
- attachInterrupt(digitalPinToInterrupt(3), end, FALLING);
- }
- /*---------------------------------------------*/
- void timerInterupt() {
- timeCount++; // +1 счетчик выборок усреднения
- sum += analogRead(A0); // суммирование кодов АЦП
- // проверка числа выборок усреднения
- if (timeCount >= MEASURE_PERIOD) {
- timeCount = 0;
- value = sum; // перегрузка среднего значения
- sum = 0;
- flagReady = true; // признак результат измерений готов
- }
- }
- void begin() {
- if (once == 0) { //проверяем, нажималась ли уже кнопка старт
- start = millis(); //получаем текущее значение millis
- once = 1; //ставим метку "старт был нажат"
- stop = 0; //обнуляем предыдущий замер, а можно и не обнулять :)
- }
- }
- ///*---------------------------------------------*/
- void end() {
- if (once == 1) { //проверяем, нажималась ли кнопка старт
- stop = millis() - start; //вычисляем промежуток
- once = 0; //убираем метку
- }
- }
- /*---------------------------------------------*/
- void loop() {
- //---------------------------------------------------------
- if ((micros() - prev_ms) > 500) { //вывод данных каждые 500мс
- prev_ms = millis();
- myNex.NextionListen();
- delay(100);
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.write(0xff);
- myNex.writeNum("x0.val", stop * 10);
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.write(0xff);
- }
- /*---------------------------------------------*/
- if (flagReady == true) {
- flagReady = false;
- Serial.print("n222.val=");
- Serial.print(value / 1000);
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.print("click n222,1");
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.write(0xff);
- /*---------------------------------------------*/
- }
- delay(100);
- /*---------------------------------------------*/
- // // simple debounce
- // buttonState1_1 = digitalRead(buttonSwitch1);
- // delay(1);
- // buttonState2_1 = digitalRead(buttonSwitch1);
- // delay(1);
- // buttonState3_1 = digitalRead(buttonSwitch1);
- // delay(1);
- // /*---------------------------------------------*/
- // buttonState1_2 = digitalRead(buttonSwitch2);
- // delay(1);
- // buttonState2_2 = digitalRead(buttonSwitch2);
- // delay(1);
- // buttonState3_2 = digitalRead(buttonSwitch2);
- // delay(1);
- /*---------------------------------------------*/
- // if ((buttonState1_1 == buttonState2_1) && (buttonState1_1 == buttonState3_1)) {
- // // has the button switch state changed?
- // if (buttonState1_1 != buttonSwitch1_State_old_1) {
- // buttonSwitch1_State_old_1 = buttonState1_1;
- // if (buttonSwitch1_State_old_1 == HIGH) {
- //
- //// Serial.print("click btc,0");
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- // } else {
- //
- //// Serial.print("click btc,1");
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- // }
- // }
- // }
- // /*---------------------------------------------*/
- // if ((buttonState1_2 == buttonState2_2) && (buttonState1_2 == buttonState3_2)) {
- // // has the button switch state changed?
- // if (buttonState1_2 != buttonSwitch2_State_old_2) {
- // buttonSwitch2_State_old_2 = buttonState1_2;
- // if (buttonSwitch2_State_old_2 == HIGH) {
- // // myNex.writeNum("b2.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
- //// Serial.print("click bt5,0");
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- // } else {
- // // myNex.writeNum("b2.bco", 63488); // Set button b0 background color to RED (color code: 63488)
- //// Serial.print("click bt5,1");
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- //// Serial.write(0xff);
- // }
- // }
- // }
- /*---------------------------------------------*/
- }
- /*---------------------------------------------*/
- void trigger1() {
- digitalWrite(4, !digitalRead(4));
- if (digitalRead(4) == HIGH) {
- } else if (digitalRead(4) == LOW) {
- }
- }
- /*---------------------------------------------*/
- void trigger2() {
- digitalWrite(5, !digitalRead(5));
- if (digitalRead(5) == HIGH) {
- } else if (digitalRead(5) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- void trigger3() {
- digitalWrite(12, !digitalRead(12));
- if (digitalRead(12) == HIGH) {
- } else if (digitalRead(12) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- void trigger4() {
- digitalWrite(7, !digitalRead(7));
- if (digitalRead(7) == HIGH) {
- } else if (digitalRead(7) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- void trigger5() {
- digitalWrite(8, !digitalRead(8));
- if (digitalRead(8) == HIGH) {
- } else if (digitalRead(8) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- void trigger6() {
- digitalWrite(9, !digitalRead(9));
- if (digitalRead(9) == HIGH) {
- } else if (digitalRead(9) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- void trigger7() {
- digitalWrite(10, !digitalRead(10));
- if (digitalRead(10) == HIGH) {
- } else if (digitalRead(10) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- void trigger8() {
- digitalWrite(11, !digitalRead(11));
- if (digitalRead(11) == HIGH) {
- } else if (digitalRead(11) == LOW) {
- }
- }
- // /*---------------------------------------------*/
- //void trigger9() {
- // digitalWrite(12, !digitalRead(12));
- // if (digitalRead(12) == HIGH) {
- //
- // } else if (digitalRead(12) == LOW) {
- // }
- //}
- // /*---------------------------------------------*/
- //void trigger10() {
- // digitalWrite(13, !digitalRead(13));
- // if (digitalRead(13) == HIGH) {
- //
- // } else if (digitalRead(13) == LOW) {
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment