2015-10-15 24 views
1

我曾那麼努力,但我只是不明白爲什麼這兩條線在我的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)*********** 

回答

1

我認爲你不明白爲什麼你必須初始化/設置的變量。 inputStringstringComplete是全局變量,所以當您在serialEvent()中更改它時,您可以在loop()中看到它,因此當您讀取serialEvent()中的'\ n'時。你知道寄件人已經完成了傳送,所以你必須對其進行分析。一旦完成相應的操作,您必須再次初始化全局變量才能重新開始。除非你這樣做,否則你會看到stringComplete == true,所以你開始在下一個週期loop()中再次分析它。

通常我們把這個布爾變量稱爲'flag',你可以在幾個代碼中看到它來'同步'它。在這種情況下,您在完成接收時設置爲true,並在完成分析時設置爲false。

+1

謝謝。非常清楚地解釋。我仍處於arduino的早期學習階段,但這很容易理解。 –