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



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

  inputString = "";
  stringComplete = false;

Stop my radio正在工作。如果我删除这段代码,它就会不断地发送值而不能停止它。

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

的处方:

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

我想你不明白为什么必须初始化/设置变量。inputStringstringComplete是全局变量,所以当你在serialEvent()中改变它时,你可以在loop()中看到它,所以当你在serialEvent()中读取'n'时。你知道发送者已经完成了传输,所以你必须分析它。完成相应的操作后,必须再次初始化全局变量才能重新开始。除非你已经这样做了,否则你会看到stringComplete == true,所以你会在loop()的下一个循环中再次开始分析它。

通常我们称这个布尔变量为"flag",你可以在代码的几个地方看到它来"同步"它。在本例中,当完成接收时设置为true,当完成分析时设置为false。

最新更新