Controlling Safe Actuators with Relays and Buttons


So this is my first Arduino project and I’ve had some help with the hardware setup HERE

I’m a bit better with the programming - as there are multiple pieces of code out there. So I’m posting the code here to see if anyone can see possible pitfalls or corrections / better ways of doing things.

The Hardware setup:

  • 2 Relays - controlling “UP” & “Down”
  • An Arduino UNO R3
  • Adafruit ILI9340C Display
  • Adafruit Fingerprint Scanner
  • A NO button.

So the function of this project is to use the Biometric FPS to release a hidden jewellery safe. At the moment the code checks a valid fingerprint and if it finds one activates the “Up” relay connected to the safe’s actuators. There is a possibility to retract the actuators either by another Biometric scan or by the local NO button which will be revealed as the safe comes up.

There’s a lot of pre-written code in this project. I’m just going to draw your attention to how I’m resetting the FPS after it’s done it’s scanning in the getFingerprintIDez() function:

if ((finger.fingerID < -1) && (fingerCounter == 0)) {
    extendActuator(); // method that turns "Up" relay on / "Down" relay off
    finger.fingerID = -1;  //resetting the FPS ID back below 0 to be used again
    fingerCounter = 1;  // setting variable ON to indicate the safe is raised

Another if statement retracts:

if ((finger.fingerID < -1) && (fingerCounter == 1)) { // FPS variable used from previous if statement.
    finger.fingerID = -1; //resetting the FPS ID back below 0 to be used again
    fingerCounter = 0;  //resetting the variable back to 0 to be used again

The other piece of code I’ve adapted is within the loop function. Basically checking the value of the Push Button and retracting the safe acutors if it changes value:

buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {    
        finger.fingerID = -1;
        fingerCounter = 0;

It would be nice if people could comment on these bits if they think they could be written better. I’ve applied this project to the actuators and both are raising and lowering correctly, so I must be doing something right :o

Any input most welcome

Thank you

Safe_Lift_1.ino (5.39 KB)

    fingerCounter = 1;  // setting variable ON to indicate the safe is raised

It is not at all obvious that that is what that code is doing. That suggests that fingerCounter is a lousy name for the variable, since it does not convey sufficient information about what the variable is for.

// Using software SPI is really not suggested, its incredibly slow
//Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _mosi, _sclk, _rst, _miso);

I know that these are commented out, but it’s obvious that the code came straight from Adafruit. It is also quite stupid to suggest, in the example code, that doing software SPI on the hardware SPI pins is somehow acceptable. It most certainly is NOT.

If the hardware SPI pins are available, use them to do hardware SPI, not software SPI. If they are not available,. because they are being used for hardware SPI, then suggesting that they can be used for software SPI, as this code does, is irresponsible.

Pushing the actuators out, or retracting them, for a fixed period of time is NOT a good idea. You need limit switches to tell you when they are fully extended or fully retracted.

  buttonState = digitalRead(buttonPin);

The ONLY thing we know, now, is that there is a button sewn onto the Arduino. There is NO clue in the name of the variable holding the pin number or the name of the variable holding the state exactly what the switch is for. In my mind, that borders on irresponsible.

int getFingerprintIDez() {

There is NOTHING in the name of this function that suggests it is also going to diddle with some actuators. Give the function meaningful name, that actually describes EVERYTHING it does, or stop diddling with the actuators in the function.

It is not clear why a valid fingerID would have a value of less than -1. I’d expect that 0 would mean that the fingerprint was unknown, while less than 0 would indicate some other error.

Hi PaulS

Thank you for your comments. I shall sort the naming issues momentarily. As to the other comments:

I will delete the SPI comments - this was already in the code I copied and I assume there to tell novices like me not to do the thing that pros like you already know??

In my project the relays are not "powering" the actuators. If you browse the linked post above there is a control box which does this. This has limit switches installed and they are pre-programmed to the correct max/min heights. All the relays have to do is create a switching signal for the amount of time the actuators need to get to the heights (multiples of 38mm/ps). The control box does the rest.

The fingerID resets below 0 as the FPS has a 20memory ID (0-19). Below 0 means that no ID is present. The if statements condition that an ID is present as well as the up/down status of the safe (fingerCounter - I will rename).

Thank you