-1

我们不太擅长编写代码,因此对于解决我们遇到的传输和接收问题的任何帮助都会很棒。

目标是:

  • 将在 Nextion 显示屏上输入的数字发送到 Arduino Mega。
  • 将数字存储并保存到 Arduino 编码中(有点像在代码中自动填充数字,而不必每次都点击“开始”。)
  • 当红外中断光束被破坏时,Arduino 将数据发送到 Nextion 显示器以从“总数量”字段中减去。(<-- 在我们的编码中还没有走到这一步。)
  • Nextion 根据 Arduino 的反馈显示“总数量”字段更改

问题:

通过点击 Nextion 显示屏上的“开始”按钮,我们成功地让程序运行,但这只是当我们在 Arduino 代码中手动输入数量时。然而,我们的问题是无法将数据条目从 Nextion 显示器自动填充到 Arduino 代码中。关于如何实现这一目标的任何想法?

''' #include <Servo.h>

#include "EasyNextionLibrary.h"

EasyNex myNex(Serial);

#define Nextion Serial

'''

''' //"cycleOne" 是一个变量,用于了解电机循环了多少次。

int cycleOne = 0;

//"stopLooping" variable is used in cases where you want to skip parts of code in a loop.

int stopLooping = 0;

//Variables that deal with the buzzer and servo for hopper one.

int countedOne = 0;  //Initial value used by the program that will count up to the variable, "One".

int One = 0;         /* Enter in the number to be ejected.
                         Ex: I want 5 bolts ejected, so I'll enter 5 for the "One" variable*/

const float cycleOneSafety = 1.5;  //Used to help determine if the hopper is empty.

'''

'''

void setup() {

pinMode(startPin,INPUT);

digitalWrite(startPin,LOW);

//Indicates whether a pin is an input or output.  Also gives power to required pins.

pinMode (bbrdOne, INPUT);

digitalWrite(bbrdOne, HIGH);

pinMode (bbrpOne, OUTPUT);

digitalWrite (bbrpOne, HIGH);

pinMode (bbtOne,OUTPUT);

digitalWrite (bbtOne,HIGH);

'''

'''

Serial.begin(9600);

//Nextion.begin(9600);

myNex.begin (9600); 

delay (1000); //Gives Nextion time to intialize

'''

'''

void loop(){

  Serial.println("Trigger1 void Loop is running");

  myNex.NextionListen();

  delay (30);

}
void trigger1(){

  Serial.println("Void Trigger1 is called");

        if(Nextion.available()>2){             //Reads if more than 2 bytes are sent (which we should have more than 2 bytes every time with <#>, <len>, <cmd>, and other special callouts from touchscreen

          char start_char = Nextion.read();    // Created local variable (start_char) reads and stores the first byte

          if (start_char == '#'){              // When we find the # character

            uint8_t len = Nextion.read();      // Created local variable (len). Reads and stores 

第二个字节的值。是后面的字节数的长度

            unsigned long tmr_1 = millis();
            boolean cmd_found = true;

            while (Nextion.available()< len){ // Waiting for all bytes declared
             if ((millis()-tmr_1)>100){      // Will not wait forever
                cmd_found = false;            // tmr_1 is a timer to avoid the stack in the while loop is there are no bytes on Serial
                break;
              }
              if(cmd_found == true){ //command is found
                uint8_t cmd = Nextion.read(); // Create local variable (cmd). Reads and stores next byte of data
                switch (cmd){
                 case 'V':                   // or <case 0x56:> IF 'V' matches, we have the command group "Sent Variables"
                                         // the next byte according to our protocol is the value for the <One>
                   One = Nextion.read();   //Reads and stores Hopper One quantity to eject byte at the variable
       
                   Nextion.print("n0.val=");    // Lines are for Nextion debug
                   Nextion.print(One);     // they are sent to the variables
      //           NextionEndCommand();         // numeric component on Nextion display component n0

      //void NextionEndCommand()
                   Nextion.print("xFF\xFF\xFF");
            
                 break;
                }
              }
            }
          }
        }
      }

'''

'''

void trigger2(){

    Serial.println("Void Trigger2 is called");

    myNex.writeNum("bo.boc",2016);

//    One = //Value of button for quantity to eject

    startRead = 1;
    ejecting();

  }

  /*
   * The following if statement is for initializing variables from the Nextion
   * touch screen before running through the main loop.
   */

'''

''' 无效弹出(){

  Serial.println("Void Ejecting is called");

  if (startRead == 1){

    cycleOne = 0;
    error = 0;
    start = 1;
    Serial.println("Start Initiated...");
    delay (1000);
    Serial.println("Starting in:");
    Serial.println("3");
    delay (1000);
    Serial.println("2");
    delay (1000);
    Serial.println("1");
    delay (1000);
  }
  if (One > 0 and start == 1){
    hopperOne();
    }
  if (error == 0 and start == 1){
  delay(1000);                      //Delays the buzzer sounding for 1 second.
  for(int x = 0;x != 3; x++){       //Changing the value that x has to reach,
    tone(buzzer,200);               //  changes the number of times the buzzer will go off.
    delay(500);                     
    noTone(buzzer);                 
    delay(500);                     
    }
  }

  if (start == 1 and error == 0){

    countedOne = 0;
    error = 0;
    start = 0;
  }
  else if (start == 1){
    start = 0;
  }

'''

4

0 回答 0