Serial monitor not responding in nRF24L01

I was trying to control LED with serial monitor via nRF24L01. If you type character 'a' in serial monitor, the integer 111 is transmitted to receiver, and the integer triggers the LED to be lit. However, the serial monitor doesn't seem to be responding.

My arduino and serial monitor works quite well in other codes, so it doesn't seem to be a hardware issue. I also checked whether the code was stuck in setup(), but it wasn't either. It seems that something is wrong in my loop() code. Any clues?

This is a code for both transmitter and receiver.

//Code for Transmitter
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int SW1 = 7;

void setup(){
 Serial.begin(9600);
 radio.begin();
 radio.openWritingPipe(pipe);
 pinMode(SW1, INPUT);
 digitalRead(SW1) == LOW;
 }

void loop(){
 char c = toupper(Serial.read());
 if(!radio.available()) 
    Serial.println("radio unavailable!");
 while(radio.available()){if (c == 'a'){
 Serial.println("Serial Transmitted");
 msg[0] = 111;
 radio.write(msg, 1);
 }
 }
}
//Code for receiver
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int LED1 = 1;

void setup(){
 Serial.begin(9600);
 radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();
 pinMode(LED1, OUTPUT);
 digitalWrite(LED1, LOW);}

void loop(){
if(radio.available()){

  while(radio.available()){
    radio.read(msg, 1);
  }
  Serial.println(msg[0]);
  if(msg[0] == 111){delay(10);digitalWrite(LED1, HIGH);Serial.println("Data successfully received");
  else digitalWrite(LED1, LOW);
  delay(10);
  }
  }else {Serial.println("No radio available");while(1);}
}

digitalRead(SW1) == LOW;

I doubt it's your problem but this line of code does nothing.

char c = toupper(Serial.read());

you are reading the serial port without checking if there is anything to read.

if (c == 'a')

How can this ever be true when you use toupper() on c ?

if(!radio.available())
Serial.println("radio unavailable!");

this will print that string EVERY time loop() executes and the radio it not available, that will bog things down a lot, may even hang the program.

BTW, does that first code actually compile? It seems to have an odd number of {}s

There are two major problems/misunderstandings in the send part.

  char c = toupper(Serial.read());   // nothing there most of the time , so c will be -1

  while (radio.available()) {        // something received?

Dont assume that Serial.read() always returns a byte, if nothing is there (most of the time) it will return -1.

radio.available() returns the number of received bytes.

I think it is not very clever to wait for a reception before starting to transmit.
As you did not call startListening, it will just never happen.

But even if the while could be entered (probably with c==-1) it could never be left.

In the receiving code all available packets are read to the same buffer, then the last is processed.

It seems I've misunderstood the radio.available() function. I thought it was about just checking whether the module was working or not. It needs something to be received. I don't necessarily need the transmitter to receive some data, so the while could be omitted.

But I'm not sure about checking serial port if there's anything to read. Do I use if(Serial.available()>0) ?

Sorry. It's been a long time I've never used this kind of thing.

Okay. I changed the loop part into this, and it prints "Serial Transmitted", which means it works.

void loop(){
        if(Serial.avaliable()>0){
           char c = Serial.read();

           if(c == 'a'){
             Serial.println("Serial Transmitted");
             msg[0] = 111;
             radio.write(msg, 1);
             }
        }
}

However, it successfully sends the data at the first time, but after the second time, the serial monitor doesn't respond again. Is it natural or does something has to be initialized?

ghjung:
it prints "Serial Transmitted", which means it works.

No, it only shows that the if was entered (so the detection of the 'a' works).
Add more debug, or comment out the radio.write(),
so you can verify that your input methode is working more than once.

radio.write() blocks (without timeout?) AFAIK...