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

Arduino - deleting variables after radio send doesn't work

我已经很努力了,但我就是不明白为什么我的 Tx 代码中有两行:

  inputString = "";
  stringComplete = false;

Stop my radio 正在工作。 如果我删除这段代码,它只会一遍又一遍地发送值而无法停止。

发送:

/* 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 )***********

接收:

/* 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 )***********

我认为您不明白为什么必须 initialize/set 您的变量。 inputStringstringComplete 是全局变量,所以当你在 serialEvent() 中更改它时,你可以在 loop() 中看到它,所以当你在 [=12= 中读取 '\n' ].您了解发送方已完成传输,因此您必须对其进行分析。完成相应的动作后,必须重新初始化全局变量才能重新开始。除非你已经这样做了,否则你会看到 stringComplete == true,所以你会在 loop() 的下一个循环中再次开始分析它。

通常我们称这个布尔变量为'flag',你可以在代码的几个地方看到它'sync'。在这种情况下,您在完成接收时设置为 true,在完成分析后设置为 false。