How to do a program once

Hello i have problem. I don't know why my code repets itself when the rfid i holding rfid tag near antena whole time.

It should work:

  1. if hacking = 0 read rfid tag
  2. switch the hackinf to 1
  3. stop reading from rfid
  4. start the lcd code [line 81]

lines: 1176, 59-81

why this happens?

Post your code here to make it easy to help you.
And please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

if hacking = 0

If that line of code, or something like it, is in your program then it won't work.

Why wont it work? How to fix it?
(i removed most of code but not important one)

//Biblioteki
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include <Servo.h>

SoftwareSerial RFID = SoftwareSerial(13, 7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Servo servo1;
//Zmienne
char readString;
int lock = 0;
int hacking = 0;
int hip = 0;
char c;
String msg;
int step1 = 0;
int step2 = 0;

void servo_move(int angle) {
  servo1.attach(A5);
  servo1.write(angle);
  delay(500);
  servo1.detach();
}






void setup() {
  //Deklarowanie Pinów
  servo_move(165);
  Serial.begin(9600);
  Serial.println("Serial Ready");

  RFID.begin(9600);
  Serial.println("RFID Ready");
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);//serwo

  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("Keypad Disabled");
  lcd.setCursor(0, 1);
  lcd.print("System Blocked");

}


void loop() {
  digitalWrite(8, LOW);
  pinMode(8, OUTPUT);
  digitalWrite(A4, LOW);
  if (hacking == 0) {
    if (RFID.available() > 0) {
      digitalWrite(A1, HIGH);
      c = RFID.read();
      msg += c;
      Serial.println(msg);
      Serial.println(msg.length());
      delay (10);
    }



    if (msg.length() > 10) {
      msg = msg.substring(1, 12);
      if (msg == "310047BF3F") {
        hacking = 1;
        digitalWrite(A0, HIGH);

      }
      msg = "";


      if (hacking == 1) {

        digitalWrite(A2, HIGH);

        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Hacking.");
        lcd.setCursor(0, 1);
        lcd.print("K");
        lcd.blink();
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking..");
        lcd.setCursor(0, 1);
        lcd.print("U");
        lcd.blink();
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking...");
        lcd.setCursor(0, 1);
        lcd.print("8");
        lcd.blink();
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking..");
        lcd.setCursor(0, 1);
        lcd.print("1");
        lcd.blink();
        delay (150);


        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking.");
        lcd.setCursor(0, 1);
        lcd.print("S");
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking..");
        lcd.setCursor(0, 1);
        lcd.print("5");
        lcd.blink();
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking...");
        lcd.setCursor(0, 1);
        lcd.print("7");
        lcd.blink();
        delay (150);


        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking..");
        lcd.setCursor(0, 1);
        lcd.print("0");
        lcd.blink();
        delay (150);



        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking.");
        lcd.setCursor(0, 1);
        lcd.print("p");
        lcd.blink();
        delay (150);



        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking..");
        lcd.setCursor(0, 1);
        lcd.print("3");
        lcd.blink();
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking...");
        lcd.setCursor(0, 1);
        lcd.print("D");
        lcd.blink();
        delay (150);

        //////////////////////


        lcd.setCursor(0, 0);
        lcd.print("Hacking..");
    
        digitalWrite(A0, LOW);
        digitalWrite(A1, LOW);
        digitalWrite(A2, LOW);

        delay (350);

        digitalWrite(A0, HIGH);
        digitalWrite(A1, HIGH);
        digitalWrite(A2, HIGH);

        lcd.clear ();
        lcd.setCursor(0, 0);
        lcd.print("ACCESS GRANTED");
        lcd.setCursor(0, 1);
        lcd.print("SYSTEM DISABLED");
        /////////////////////////////////////////////////
        //digitalWrite(A5, HIGH);
        hacking = 2;
        servo_move(90);



      }
    }
  }

}

The answer to all such questions is usually, "because you are detecting a state, rather than a change of state". A state is tested every time through loop(), hence is repetitive.

char readString;

Shouldn't that variable be called readFloat? You can NOT store a string (or a String) in a char variable.

    if (RFID.available() > 0) {
      digitalWrite(A1, HIGH);
      c = RFID.read();
      msg += c;
      Serial.println(msg);
      Serial.println(msg.length());
      delay (10);
    }

There is no point in sticking your head in the sand after read each character.

        //////////////////////

A useless ass comment. Repeated many times. With NO useful comments anywhere.

UKHeliBob:

if hacking = 0

If that line of code, or something like it, is in your program then it won't work.

The "=" is an assignment operator while "==" is a test. The "if hacking = 0" statement assigns the value 0 to the variable "hacking" and will always be true. It is an easy mistake to make (and the compiler will not tell you about it). The way around that is to write your tests with the order reversed -- "if 0 == hacking" - that way if you get it wrong and use one equal sign instead of two, the compiler will throw it out because you are trying to assign a variable to a constant.