Start to Loop from input signal

Hi all,

I'm newbie in the arduino world and I will be happy to get some help...(i'm sorry about my english)

I have rfid code and I want that the loop for this code will start only if the arduino is get an input signal (from first arduino - the pin is configured to output).
if the input signal is lost, the loop have to stop.

or if it will be more easy that the Relay will work only if there is input signal from the first arduino

the input pin is 7
Code:

/*

  • Read a card using a mfrc522 reader on your SPI interface
  • Pin layout should be as follows (on Arduino Uno):
  • MOSI: Pin 11 / ICSP-4
  • MISO: Pin 12 / ICSP-1
  • SCK: Pin 13 / ISCP-3
  • SS: Pin 10
  • RST: Pin 9
  • This Code for the LAST reader - Open relay
    */

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN,RST_PIN);

int relay = A1;

int serNum[5];

int cards[][HEX] = {
{53,220,225,82,90},
{70,98,202,50,22}
};

bool access = false;
void setup(){

Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(relay, OUTPUT);
pinMode(7, INPUT); //signal from prev arduino
}

void loop(){
// while(digitalRead(7) == HIGH) ****** I try to use with while and if but the loop continue to run without the input signal condition even when the input wire is disconnected.

if(rfid.isCard()){

if(rfid.readCardSerial()){
Serial.print(rfid.serNum[0]);
Serial.print(" ");
Serial.print(rfid.serNum[1]);
Serial.print(" ");
Serial.print(rfid.serNum[2]);
Serial.print(" ");
Serial.print(rfid.serNum[3]);
Serial.print(" ");
Serial.print(rfid.serNum[4]);
Serial.println("");

for(int x = 0; x < sizeof(cards); x++){
for(int i = 0; i < sizeof(rfid.serNum); i++ ){
if(rfid.serNum != cards[x]) {
* access = false;*
* break;*
* } else {*
* access = true;*
* }*
* }*
* if(access) break;*
* }*

* }*

* if(access);{*
* Serial.println("Get In--->");*
// while(digitalRead(7) == HIGH);
* digitalWrite(relay, HIGH);*
* delay(100); *

* } else {*
* Serial.println("You Blocked(-)");*
* delay(100); *
* } *
* }*

* rfid.halt();*
* }*
Thanks for the makers and experts

And now the code without italics, please

Here is the code.
thank you

rfidtest.ino (1.95 KB)

Your code could simply have been pasted between code tags, like this:

 /*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 9
* 
* This Code for the LAST reader - Open relay
*/

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN,RST_PIN);

int relay = A1;

int serNum[5];

int cards[][HEX] = {
  {53,220,225,82,90},
  {70,98,202,50,22}
};

bool access = false;
void setup(){

    Serial.begin(9600);
    SPI.begin();
    rfid.init();
   pinMode(relay, OUTPUT);
    pinMode(7, INPUT);   //signal from prev arduino
}

void loop(){
//   while(digitalRead(7) == HIGH) // I try to use with while and if but the loop continue to run without the input signal condition even when the input wire is disconnected.
    if(rfid.isCard()){
    
        if(rfid.readCardSerial()){
            Serial.print(rfid.serNum[0]);
            Serial.print(" ");
            Serial.print(rfid.serNum[1]);
            Serial.print(" ");
            Serial.print(rfid.serNum[2]);
            Serial.print(" ");
            Serial.print(rfid.serNum[3]);
            Serial.print(" ");
            Serial.print(rfid.serNum[4]);
            Serial.println("");
            
            for(int x = 0; x < sizeof(cards); x++){
              for(int i = 0; i < sizeof(rfid.serNum); i++ ){
                  if(rfid.serNum[i] != cards[x][i]) {
                      access = false;
                      break;
                  } else {
                      access = true;
                  }
              }
              if(access) break;
            }
           
        }
        
       if(access);
       if(digitalRead(7) == HIGH){

          Serial.println("Get In--->"); 
          digitalWrite(relay, HIGH);
          delay(100);  
               
       } else {
           Serial.println("Blocked(-)");
           delay(100);      
       }        
    }
    
    
    rfid.halt();


    }

You need to use Tools + Auto Format, and post the code again. Do that AFTER putting every { and every } (AND I DO MEAN EVERY) on a line BY ITSELF.

You need to define how the switch is wired. I'm going to guess "incorrectly" for now.

You need to explain some stuff about your code.

       if(access);

If access is true, do nothing (;). Otherwise, do nothing. Why bother?

    rfid.halt();

Where is the corresponding code to restart the rfid instance? Why is there a need to halt it on every pass through loop()?

Thanks for the explanation on how to post!
this is the correct code:

/*
  Read a card using a mfrc522 reader on your SPI interface
  Pin layout should be as follows (on Arduino Uno):
  MOSI: Pin 11 / ICSP-4
  MISO: Pin 12 / ICSP-1
  SCK: Pin 13 / ISCP-3
  SS: Pin 10
  RST: Pin 9

  This Code for the LAST reader - Open relay
*/

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN, RST_PIN);

int relay = A1;

int serNum[5];

int cards[][HEX] = {
  {53, 220, 225, 82, 90},
  {70, 98, 202, 50, 22}
};

bool access = false;
void setup() {

  Serial.begin(9600);
  SPI.begin();
  rfid.init();
  pinMode(relay, OUTPUT);
  pinMode(7, INPUT);   //signal from prev arduino
}

void loop() {
  //   while(digitalRead(7) == HIGH) // I try to use with while and if but the loop continue to run without the input signal condition even when the input wire is disconnected.
  if (rfid.isCard()) {

    if (rfid.readCardSerial()) {
      Serial.print(rfid.serNum[0]);
      Serial.print(" ");
      Serial.print(rfid.serNum[1]);
      Serial.print(" ");
      Serial.print(rfid.serNum[2]);
      Serial.print(" ");
      Serial.print(rfid.serNum[3]);
      Serial.print(" ");
      Serial.print(rfid.serNum[4]);
      Serial.println("");

      for (int x = 0; x < sizeof(cards); x++) {
        for (int i = 0; i < sizeof(rfid.serNum); i++ ) {
          if (rfid.serNum[i] != cards[x][i]) {
            access = false;
            break;
          } else {
            access = true;
          }
        }
        if (access) break;
      }

    }

    if (access);
    Serial.println("Get In--->");
    digitalWrite(relay, HIGH);
    delay(100);

  } else {
    Serial.println("Blocked(-)");
    delay(100);
  }
}


rfid.halt();


}

the wiring is this: from 1st arduino pin 8 is configure to output that connected to pin 7 (configured as input) on this arduino.
the wire is connected with 10k resistor between the arduino

about the rest of the code I just use with the example in the mfrc522 library so I actually to know what is mean about

    if(access);

and

    rfid.halt();

I will be happy if you can advise me what to change to get this work.
basically the condition to open the Relay is that the arduino is get high input from pin 7 and reading the correct card together

the wiring is this: from 1st arduino pin 8 is configure to output that connected to pin 7 (configured as input) on this arduino.
the wire is connected with 10k resistor between the arduino

A picture (schematic) is worth a thousand words. I am not even going to guess what this mess of words means, but it does not seem to describe how a switch is wired. A switch involves ONE Arduino.

Make a simple pencil drawing showing how everything is connected and post a photo of the drawing. Please do NOT use Fritzing.

It will be much easier to plan your program if it is organized into separate functions - one of which could be to read the rfid tags. Then your code in loop() might be like this

void loop() {
   checkInputFromArduino();
   readRFID();
   otherStuff();
}

and the code in readRFID() might be like

void readRFID() {
   if (arduinoInput == true) {
       // code to read RFID tag
   }
}

Have a look at Planning and Implementing a Program

...R