简体   繁体   English

Arduino-在无线电发送后删除变量不起作用

[英]Arduino - deleting variables after radio send doesn't work

I have tried so hard but i just can't understand why the two lines in my Tx code : 我已经尽力了,但是我不明白为什么我的Tx代码中的两行:

  inputString = "";
  stringComplete = false;

Stop my radio is working. Stop my radio工作。 If I delete this code, it just keeps sending the values over and over without being able to stop it. 如果删除此代码,它将不断重复发送值,而无法停止它。

Tx: TX:

/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
 - WHAT IT DOES: Reads Analog values on A0, A1 and transmits
   them over a nRF24L01 Radio Link to another transceiver.
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   - 
   Analog Joystick or two 10K potentiometers:
   GND to Arduino GND
   VCC to Arduino +5V
   X Pot to Arduino A0
   Y Pot to Arduino A1

 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/

String inputString = "";
boolean stringComplete = false;
int msg[1];  // 2 element array holding Joystick readings
int msgNum = 0;
void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  serialEvent();

  if (stringComplete) {
    inputString.trim();

    String on1 = "onone";
    on1.trim();
    String on2 = "ontwo";
    on2.trim();
    String off1 = "offone";
    off1.trim();
    String off2 = "offtwo";
    off2.trim();

    if (inputString.equals(on1)) {
      Serial.print("1 is On");
      msg[0] = 111;
      radio.write(msg, 1);
      inputString = "";
      stringComplete = false;
    } else if (inputString.equals(off1)) {
      Serial.print("1 Is Off");
      msg[0] = 112;
      radio.write(msg, 1);
      inputString = "";
      stringComplete = false;
    } else if (inputString.equals(on2)) {
      Serial.print("2 Is On");
      msg[0] = 113;
      radio.write(msg, 1);
      inputString = "";
      stringComplete = false;
    } else if (inputString.equals(off2)) {
      Serial.print("2 Is Off");
      msg[0] = 114;
      radio.write(msg, 1);
      inputString = "";
      stringComplete = false;
    } else {
      inputString = "";
      stringComplete = false;
    }

  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();

    delay(100);
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

//NONE
//*********( THE END )***********

Rx: 接收:

/* YourDuinoStarter Example: nRF24L01 Receive Joystick values

 - WHAT IT DOES: Receives data from another transceiver with
   2 Analog values from a Joystick or 2 Potentiometers
   Displays received values on Serial Monitor
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED

 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

int ledPin = 3;
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int msg[1];  // 2 element array holding Joystick readings
int lastMsgNum;
void setup()   /****** SETUP: RUNS ONCE ******/
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    while (!done)
    {
      // Fetch the data payload
      done = radio.read( msg, sizeof(msg) );
        Serial.print(msg[0]);
        if (msg[0] == 111) {
          digitalWrite(3, HIGH);
        } else if (msg[0] == 112) {
          digitalWrite(3, LOW);
        } else if (msg[0] == 113) {
          digitalWrite(5, HIGH);
        } else if (msg[0] == 114) {
          digitalWrite(5, LOW);
        }
    }
  }
  else
  {    
      //Serial.println("No radio available");
  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE
//*********( THE END )***********

I think that you don't understand why you must initialize/set your variables. 我认为您不明白为什么必须初始化/设置变量。 inputString and stringComplete are global variables, so when you changed it in serialEvent() you could see it in loop() so when you read a '\\n' in serialEvent() . inputStringstringComplete是全局变量,因此,当您在serialEvent()serialEvent()更改时,您可以在loop()看到它,因此当您在serialEvent()读取“ \\ n”时。 You understand that the sender has finished transmission so you must analize it. 您了解发件人已完成传输,因此您必须对其进行分析。 Once you finished the corresponding action, you must initialize the global variable again to start again. 完成相应的操作后,必须再次初始化全局变量才能重新开始。 Unless you have done that, you see stringComplete == true so you start to analize it again in the next cycle of loop() . 除非您这样做,否则您会看到stringComplete == true因此在下一个loop()循环中将其再次进行分析。

Usually we call this boolean variables as 'flag', you can see it in several places of code to 'sync' it. 通常我们将此布尔变量称为“标志”,您可以在多个代码位置看到它以“同步”它。 In this case, you set true when you have finished the reception and set false when you have finished the analisys. 在这种情况下,当您完成接收后将设置为true,而在完成analisys后将设置为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM