Problem in coding push button

i was do the program for a key storage system. i was face the problem in push button. The push button is replace as sensor. The problem is in stage 12 i receive the value from python, i need to release the push button to declare the key has be taken and than will update to the database. if no release the push button there will no update the data to database.

keyss.ino (15.3 KB)

Functions can have parameters, so instead of:

void blinkR1(){ ... }
void blinkR2(){ ... }
void blinkR3(){ ... }
void blinkR4(){ ... }
void blinkR5(){ ... }
void blinkR6(){ ... }
void blinkR7(){ ... }
void blinkR8(){ ... }
void blinkR9(){ ... }

you can have something like:

void blinkLed(byte pin) {
  for (byte i = 6; i > 0; i--) {
    digitalWrite(pin, (i % 2) ? LOW : HIGH); // if i is odd then LOW, else HIGH
    delay(500);
  }
}

and call it with the number of the pin as a parameter.

Structuring your code as a state machine is a good idea, however, you need to make more use of language features to make your code descriptive. So instead of numeric constants for the states, you should create an enumeration:

enum KeyStorageStates { InputStaffId, ScanKeyChain, GetStaffId, ... };

KeyStorageStates state; // = assign initial state here

void loop() {
  switch (state) {
    case InputStaffId: inputStaffId();
      break;
    case ScanKeyChain: scanKeyChain();
      break;
    ...
  }
}

// rename void state0() { to something descriptive
void inputStaffId() {
  ...
  state = ScanKeyChain;
  ...
}

Now I don't know whether I have understood your actual question. Are you asking how to make the client/operator of your device release the physical button? That would be tricky.

I would say make the key dispense action depend on the press AND release of the button.
If there is an issue with doing that, then update the DB as soon as the key has been made available to the client. Then only return to the idle state once the device is in a consistent state - eg. all buttons in their default positions.

If I haven't understood your question, then please rephrase it with more detail.