Counting a loop

bullroarer:
I understand because my servo will be changing a value the whole time it is rotating and I might have made a mistake when I chose to write 'pos' once a card was read. Should I write the variable once it goes into position or as the card is read?

I think that you will get yourself into trouble if you change the value until after you change the state of the servo.

Read Card
Locked != Locked // Invert Locked

If Locked == True
  Unlock
Else
  Lock

You should see that if you invert the value of Locked immediately after reading the card, then Locked will not reflect the state of the deadbolt and the if statement will perform the wrong action.

Here is a better way of doing it.

Read Card

If Locked == True
  Unlock
Else
  Lock

Locked != Locked // Invert Locked

If there is any ambiguity about what the if statement is going to do, such as cases where there might be more than two states, or cases where the logic might not always alternate monotonically between the states (e.g. sometimes you will read the card, but will NOT unlock or lock), then the state-tracking variable should be update inside the if statement, like this:

Read Card

If Locked == True
  Unlock
  Locked = False
Else
  Lock
  Locked = True