Define slave variables inside void loop

Hi,

I have a master-slave communication between two Arduino Mega boards (through i2C). The master sends a command to the slave in order to perform a given tasks. The system works when I open the Serial Monitor window from both Arduino boards (the variables are defined above void setup()). I mean, I open two Arduino IDE instances with their respective COM acquiring the data from USB cables. However, if I unplug the slave from the PC, while the master still is connected to the PC, the system does not work.

I could move most part of the initialization and void setup at the slave, inside the void loop(). However, the function which allows the reception of data from the master, gives me errors.

void setup() {


}

void loop() {
  

  const int STOP  = 13; //STOP of the RX module
  const int CLAX = 12; //CLAX of the RX module
  const int START = 11; //START of the RX module
  const int LAMP = 10; //LAMP of the RX module
  .....

#define ArrayElementCount(x) (sizeof(x) / sizeof(x[0]))
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
  hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

   Wire.begin (MY_ADDRESS);
  lcd.begin(20, 4);
  Serial.begin (9600);
  Serial1.begin (9600);
  Wire.onReceive(receiveEvent);
  lcd.setCursor(0, 0);
  pinMode(STOP, INPUT_PULLUP); digitalWrite(STOP, HIGH);
  .....

  if (flag == 1)
  {
   ...
  }
}

void receiveEvent(int howMany) 
{
  int y = Wire.read();
  int flag = 1;
  Serial.println(y);
}

My goal is to acquire all data just plugging the master to the pc uniquely (keeping the i2C communication with the slave). Which is the best approach to do that?

If you need additional information, I can add it.

Thanks

I can not imagine what putting all the stuff that is supposed to be before setup() or loop() into loop() is supposed to accomplish. Please explain WHY you are trying to do that.

When you unplug the slave from the PC, how is it then supposed to respond to the master? Where is it then getting power from, if it is?

REM14:
However, if I unplug the slave from the PC, while the master still is connected to the PC, the system does not work.

Try connecting 5V-point of MEGA to the 5V-point of UNO (though it is absolutely not recommended) as a quick test case!

The 5V pin from the master is connected to the + column of a breadboard and from that line to the 5V pin of the slave. Thus, both are fed. Is not correct?

REM14:
The 5V pin from the master is connected to the + column of a breadboard and from that line to the 5V pin of the slave. Thus, both are fed. Is not correct?

The connection is correct. Don't forget to keep the GND connection. Report the result. (Anyway, you need desperate 5V supply from your UNO.)

@GolamMostafa. Find the scheme here.

The system works if both slave and master Arduino board are connected to the USB port of the PC and, the serial monitor windows is open. I've removed the Serial lines in the slave (which it aims to link the serial monitor). However, the Serial2 is used to communicate with a device through serial.

I just need the Master be connected to the PC to collect data.

I've tried to: Master fed through USB, Slave connected to the electrical grid through jack, the LCD display connected directly to the 5V pin of the Slave. The master feeds the rest of elements from the breadboard: buttons and LEDs. It also feeds a device using the 3V pin.

I want the slave to work while it is connected to the grid and the master through USB.

Is it a problem of power? connections? code?

Thanks.