Communication between 2 Arduino and weird behaviour

Hello

I got a project where I need to make communications with one other Arduino. I don’t have any special protocol. I just have a pulse into another pin of the other Arduino.

This is the code of the first Arduino:

void setup() {
        // Init the pin
        pinMode(7, OUTPUT);

        // Send only one impulse
        digitalWrite(7, HIGH);
        delay(1);
        digitalWrite(7, LOW);

        // Print something in the serial print
        Serial.begin(9600);
        Serial.println("Message sent");
}

void loop() {
}

And this is the code of the second one:

boolean stop = false;

//Adding the lights
const char red = 11;
const char yellow = 10;
const char green = 9;

//Adding the relays
const char relay1 = 6;
const char relay2 = 5;
const char relay3 = 4;

//Adding the sensors
const char breakSensor = 3;
const char outSensor = 2;

//Adding the COM
const char COMin = 8;
const char COMout = 7;

//Define detect
int detect = true;

void setup() {
  //Define the outputs
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

  //Define the inputs
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(8, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  //When receive a signal:
  detect = digitalRead(COMin);
  if(detect == LOW) {
    //Set stop to false
    stop = false;
    Serial.println("The zone will start");

    //Set the lights to green
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);

    //Powering the rails
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
  }

  //When the train goes out the block:
  detect = digitalRead(outSensor);
  if(detect == LOW) {
    Serial.println("The zone will stop");
    //Send start to the precedent
    digitalWrite(COMout, HIGH);

    //Set lights to red
    digitalWrite(green, LOW);
    digitalWrite(red, HIGH);
    digitalWrite(yellow, LOW);

    //Set stop to true
    stop = true;

    //Stop to power the COMout
    delay(500);
    digitalWrite(COMout, LOW);
  }

  //If the train enter the breaking zone and needs to stop
  detect = digitalRead(breakSensor);
  if(detect == LOW && stop == true) {
    Serial.println("The zone is stopped");
    //Power off the tracks.
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
  }
}

But the problem is with this part:

//When receive a signal:
  detect = digitalRead(COMin);
  if(detect == LOW) {
    //Set stop to false
    stop = false;
    Serial.println("The zone will start");

    //Set the lights to green
    digitalWrite(green, HIGH);
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);

    //Powering the rails
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
  }

Is always true. So the light is always green and the serial port is spammed with the message “The zone will start”.

What I intended to make is that, only when the first Arduino send something, then it will return the following stuff. I tried to switch from “if HIGH” to “if LOW” but the same problem occurs.

I don’t know what’s happening, It worked a few time ago, then I tried to connect it to the power supply that was too low in intensity, and this is where the problem started. From that time the problem occurs what ever happens, even when connected to the computer.

I really have no clue on this one. Can you help?

Thanks in advance!

You gave the pins names. Why are you not consistently using the names?

Do you have the grounds connected between the two Arduinos?