Sketch won't load after I replug the USB cable.

I’ve spent close to 3 hours troubleshooting this, and I’m getting really stress out :sleeping:

So this is the code in my arduino,

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

RF24 radio(9, 8);
RF24Network network(radio);

//Addresses for the different nodes
const uint16_t master = 00;
const uint16_t slave1 = 01;
const uint16_t slave2 = 02;

//  VARIABLES FOR COMMUNICATION
char tosend_off = '0';
char tosend_on = '1';
char blank = 'r';

// VARIABLES FOR CODE STRUCTURE               
int lamp_state = 0;

void setup() {
  pinMode(5, INPUT);
  Serial.begin(9600);
  radio.begin();
  network.begin(90, slave1);
}

void loop()
{
  network.update();

  if(digitalRead(5) == HIGH)
  {
    if(lamp_state == 0)
    {
      RF24NetworkHeader master(master);
      network.write(master, &tosend_on, sizeof(tosend_on));
      lamp_state = 1;
    }
  }
  else
  {
    if(lamp_state == 1)
    {
      RF24NetworkHeader master(master);
      network.write(master, &tosend_off, sizeof(tosend_off));
      lamp_state = 0;
    }
  }
}

Notice the nested If statements I’ve got there. Also, notice the main function i’m doing which is:

RF24NetworkHeader master(master);
network.write(master, &tosend_on, sizeof(tosend_on));

For some reason, with the nested If, after I unplug and replug the USB cable, the whole thing doesn’t work… But without the nested If, it will still work after i unplug and replug the USB cable.

So this the code that works:

void loop()
{
  network.update();
  RF24NetworkHeader master(master);
  network.write(master, &tosend_on, sizeof(tosend_on));
}

I’ve no clue… Seriously. Can someone help me? Is there any mistake in my code?

Also, I’m using the Arduino UNO R3, NRF24L01+ Module and the RF24 & RF24Network Libraries

You have some radio connected here , is it using the same pins as the USB ? ( Rx/Thx)

hammy: You have some radio connected here , is it using the same pins as the USB ? ( Rx/Thx)

Nope. The pins that are used are, Digital(9,8,11,12,13)

I also have a button on Digital 5.

That's it

What do you mean by "load" in "Sketch won't load after I replug the USB cable."

Run or upload?

Do you power from USB or differently?

Anyway, you will have to close serial monitor after disconnecting; that might be your problem.