下一步双重状态按钮不起作用

时间:2018-07-10 11:20:00

标签: arduino arduino-ide nextion

下面是我从YouTube示例中下载的代码,并简化为仅使用双重状态按钮。

CODE

/*
This sketch shows examples on how to send data from the Nextion display to Arduino and vice versa.

I didn't find a reliable way to receive data from the nextion display without using the library 
so I am going to use the official library to receive data, but I am not going to use it to send data to the display
because it can create problems with touch events when we send data in the loop.
I think it's easier to send data to the display without the library, anyway.

Connection with Arduino Uno/Nano:
* +5V = 5V
* TX  = pin 0 (RX)
* RX  = pin 1 (TX)
* GND = GND

If you are going to use an Arduino Mega, you have to edit everything on this sketch that says "Serial"
and replace it with "Serial1" (or whatever number you are using). Also, define the Serial port on NexConfig.h
inside the nextion library.


Nextion library: https://github.com/itead/ITEADLIB_Arduino_Nextion


This sketch was made for my 2nd video tutorial shown here: https://www.youtube.com/watch?v=mdkUBB60HoI&t=26s

Made by InterlinkKnight
Last update: 02/22/2018
*/


#include <Nextion.h>  // Include the nextion library (the official one) https://github.com/itead/ITEADLIB_Arduino_Nextion
                      // Make sure you edit the NexConfig.h file on the library folder to set the correct serial port for the display.
                      // By default, it's set to Serial1, which most Arduino boards don't have.
                      // Change "#define nexSerial Serial1" to "#define nexSerial Serial" if you are using Arduino UNO, nano, etc.



int CurrentPage = 0;  // Create a variable to store which page is currently loaded





// Declare objects that we are going to read from the display. This includes buttons, sliders, text boxes, etc:
// Format: <type of object> <object name> = <type of object>(<page id>, <object id>, "<object name>");
/* ***** Types of objects:
 * NexButton - Button
 * NexDSButton - Dual-state Button
 * NexHotspot - Hotspot, that is like an invisible button
 * NexCheckbox - Checkbox
 * NexRadio - "Radio" checkbox, that it's exactly like the checkbox but with a rounded shape
 * NexSlider - Slider
 * NexGauge - Gauge
 * NexProgressBar - Progress Bar
 * NexText - Text box
 * NexScrolltext - Scroll text box
 * NexNumber - Number box
 * NexVariable - Variable inside the nextion display
 * NexPage - Page touch event
 * NexGpio - To use the Expansion Board add-on for Enhanced Nextion displays
 * NexRtc - To use the real time clock for Enhanced Nextion displays
 * *****
 */
NexDSButton bt0 = NexDSButton(0, 9, "bt0");  // Dual state button added
NexText t0 = NexText(0, 16, "t0");  // Text box added, so we can read it

// Declare pages:
// Sending data to the display to nonexistent objects on the current page creates an error code sent by the display.
// Any error sent by the display creates lag on the Arduino loop because Arduino tries to read it, thinking it's a touch event.
// So to avoid this, I am only going to send data depending on the page the display is on.
// That's the reason I want the Arduino to know which page is loaded on the display.
// To let Arduino know what page is currently loaded, we are creating a touch event for each page.
// On the nextion project, each page most send a simulated "Touch Press Event" in the "Preinitialize Event" section so
// we can register that a new page was loaded.
NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event
NexPage page2 = NexPage(2, 0, "page2");  // Page added as a touch event

// End of declaring objects



char buffer[100] = {0};  // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
                         // Further on this sketch, I do receive text so that's why I created this buffer.



// Declare touch event objects to the touch event list: 
// You just need to add the names of the objects that send a touch event.
// Format: &<object name>,

NexTouch *nex_listen_list[] = 
{
  &bt0,  // Dual state button added
  &page0,  // Page added as a touch event
  &page1,  // Page added as a touch event
  &page2,  // Page added as a touch event
  NULL  // String terminated
};  // End of touch event list



////////////////////////// Touch events:
// Each of the following sections are going to run everytime the touch event happens:
// Is going to run the code inside each section only ones for each touch event.



void bt0PopCallback(void *ptr)  // Release event for dual state button bt0
{
  uint32_t number5 = 0;  // Create variable to store value we are going to get
  bt0.getValue(&number5);  // Read value of dual state button to know the state (0 or 1)

  if(number5 == 1){  // If dual state button is equal to 1 (meaning is ON)...
    digitalWrite(7, LOW);  // Turn ON internal LED
  }else{  // Since the dual state button is OFF...
    digitalWrite(7, HIGH);  // Turn OFF internal LED
  }
}  // End of release event



// Page change event:
void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event


// Page change event:
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
}  // End of press event


// Page change event:
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;  // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
}  // End of press event



////////////////////////// End of touch events



void setup() {  // Put your setup code here, to run once:

  Serial.begin(9600);  // Start serial comunication at baud=9600


  // I am going to change the Serial baud to a faster rate.
  // The reason is that the slider has a glitch when we try to read it's value.
  // One way to solve it was to increase the speed of the serial port.
  delay(500);  // This delay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
  Serial.print("baud=115200");  // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
                                // it will retear to default baud of 9600.
                                // To take effect, make sure to reboot the Arduino (resetting Arduino is not enough).
                                // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
                                // If you change the default baud, every time the nextion is power ON is going to have that baud rate, and
                                // would not be necessery to set the baud on the setup anymore.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to nextion.
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.end();  // End the serial comunication of baud=9600

  Serial.begin(115200);  // Start serial comunication at baud=115200





  // Register the event callback functions of each touch event:
  // You need to register press events and release events seperatly.
  // Format for press events: <object name>.attachPush(<object name>PushCallback);
  // Format for release events: <object name>.attachPop(<object name>PopCallback);
  bt0.attachPop(bt0PopCallback);  // Dual state button bt0 release
  page0.attachPush(page0PushCallback);  // Page press event
  page1.attachPush(page1PushCallback);  // Page press event
  page2.attachPush(page2PushCallback);  // Page press event

  // End of registering the event callback functions

  pinMode(7, OUTPUT);

}  // End of setup




void loop() {  // Put your main code here, to run repeatedly:


  delay(30);  // This is the only delay on this loop.
              // I put this delay because, without it, the timer on the display would stop running.
              // The timer I am talking about is the one called tm0 on page 0 (of my example nextion project).
              // Apparently, we shouldn't send data to the display too often.



  // Send page number to the object called up:
  // This object (np) exist on every page so at this point we don't need to check which page is loaded on the display.
  Serial.print("np.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  Serial.print(CurrentPage);  // This is the value you want to send to that object and atribute mentioned before.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial.write(0xff);
  Serial.write(0xff);



  // We are going to check the list of touch events we enter previously to
  // know if any touch event just happened, and execute the corresponding instructions:

  nexLoop(nex_listen_list);  // Check for any touch event



}  // End of loop

我在使Nextion激活引脚7(连接到继电器模块)上的按钮时遇到问题。 MEGA2560正在记录来自Nextion显示屏的传输(因为TX LED在按钮触摸时闪烁),但是它没有任何功能。该按钮(通过Nextion Editor)位于page0id9上,并命名为bt0。我以为我已经设法理解了代码并纠正了我遇到的所有问题,但是,它没有用。

如果有人可以帮助我理解这段代码,将不胜感激!

1 个答案:

答案 0 :(得分:0)

您好,如果您从库中查看nextion.h文件。 那里也应该包括nextDualStateButton。在某些库中,缺少该行。

#ifndef __NEXTION_H__
#define __NEXTION_H__

#include "Arduino.h"
#include "NexConfig.h"
#include "NexTouch.h"
#include "NexHardware.h"

#include "NexButton.h"
#include "NexDualStateButton.h"  //add this one
#include "NexCrop.h"
#include "NexGauge.h"
#include "NexHotspot.h"
#include "NexPage.h"
#include "NexPicture.h"
#include "NexProgressBar.h"
#include "NexSlider.h"
#include "NexText.h"
#include "NexWaveform.h"

#endif /* #ifndef __NEXTION_H__ */