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

问题描述:

我曾那么努力,但我只是不明白为什么这两条线在我的Tx代码:的Arduino - 无线电发送后删除变量不起作用

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: [email protected] */ 

/*-----(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: [email protected] */ 

/*-----(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。

+1

谢谢。非常清楚地解释。我仍处于arduino的早期学习阶段,但这很容易理解。 –