Categories
TUTORIALS

Creating a Lock “Enabler” Button on Tinkercad

This tutorial is part of a project that is conceived as a Narcan first aid-kit stored on public transportation. Once the engineer of a train is notified of a prospective overdose, they can hit a button which will unlock the first aid-kit in the car. Once the kit is unlocked, the passenger needs to hit another button to open the box. In effect, the engineer is enabling the use of this button. 

Because Tinkercad does not include lock components, I will substitute them with two LEDs. A red LED will represent the locking mechanism and the blue LED will represent the opening button. The circuitry and programming logic should remain the same.

Introduction to Tinkercad

As the name suggests, Tinkercad is a derivative of Autocad for hackerspaces. It is a free software that can be accessed online at https://www.tinkercad.com/learn/circuits/learning. The wiring of the Arduino board as well as the programming environment can be simulated with Tinkercad. The circuit section will need to be chosen to use these features.

Components List

  • IR sensor
  • IR remote
  • 1 red LED
  • 1 blue LED
  • Button
  • 14 wires
  • 3 220 Ohm resistors

Setting Up Your Board and Code

The circuits environment gives us everything we need to complete the test project: an IR sensor, an IR remote control, two LEDs, a button, and all the necessary wires and resistors to complete the circuit. Our LEDs are powered by two separate pins so we can control them separately. In essence, one LED is controlled by the IR remote and another LED is controlled by the push button, when certain conditions are met. Each main component will use a separate pin.

As for the code, we will be using a modified version of IRrecvDemo, which is included with the IRremote custom library. The library is also available in Tinkercad. The code is quite short, and reproduced in its entirety below.

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

This code is fairly straightforward, and we will learn more about it as we add our own bits. The most important part of the code is the IF statement in the void loop, which will order the sensor to receive the signals sent by the remote, and record them in our serial monitor. Run the code and open up your monitor. Press the different buttons on the remote. You will notice that each button sends a different hex code. Knowing these codes will allow us to map different functions to each button. Below is a screenshot of the serial after hitting all of the number buttons.

For simplicity’s sake, I recorded the codes that I received when buttons #1 and #2 were pressed. Thinking ahead, I plan to have Button #1 open the lock and Button #2 to rearm it. In this example we substitute the locking mechanism with the red LED turning on and off. An on status for the red LED will then allow the blue LED to be controlled by the button. Overall, we want to use the the IR remote to control the red LED, and the button to control the blue LED ONLY if the red LED is on.

Before we move on let us declare the pins for the two LEDs and the button as well as set their modes in our code. We also want to create variables that record the status of the button and the status of our lock (the red LED). To do this I declare the variables buttonState and LOCKstatus and set them to zero. Later we will record a status change by trigging to variables to move to 1.

// Set the pin for the IR Sensor
int RECV_PIN = 11;

// Set the pin for the two LEDs
int Red = 13; //Substitute for Locking Mechanism
int Blue = 8; //Substitute for Opening Mechanism

// Set the pin for the button
const int buttonPin = 2;

// Create a variable to record the state of the button 
int buttonState = 0;

// Create a variable to record the state of the locking mechanism
int LOCKstatus = 0;

//Allowing the IR sensor to receive results from the IR Remote
IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  //Setting the modes on the pins
  pinMode(Red, OUTPUT);
  pinMode(Blue, OUTPUT);
  pinMode(buttonPin, INPUT);
  
  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

Now for the meat of our code. Time to use the hex codes from the serial monitor that we recorded earlier. Within the IF statement, we will create another IF statement which will be activated when the sensor receives the code for button #1.

 if (results.value == 0xFD08F7){
      digitalWrite(Red, HIGH);
    }

This task is accomplished by the segment of code above. In plain English, if the sensor receives a signal (results.value) and it is equal to button #1 (hex code 0xFD08F7) then the program will turn on the red LED ( digitalWrite(Red, HIGH) ). The 0x before the code is to declare the followings characters as a hexadecimal code.

Outside the original IF statement we will create another one for the off the button, which I have chosen as button #2. This code follows the same logic.

if (results.value == 0xFD8877){
      digitalWrite(Red, LOW);
      LOCKstatus = 0;
    }

Great! We finished the first step. We can now turn our red LED on and off using the IR remote.

Our next move is to record the status of the red LED. Is it on or off? We need this knowledge so we can command the button to only be operational when the red light is on. In this way, the holder of the IR remote controls the “permissions” on the button.

To record the status of the red LED, I simply add the LOCKstatus variable that we created earlier to the IF statement and change it to one. Now every time we turn the red LED on, the status will change to 1. We also will do the same for the off IF statement, except the status will be 0. Now 0 and 1 represent off and on respectively. I also told the serial monitor to print the status change to 1 for testing purposes, but this not necessary.

  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    
    //This loop unlocks the lock (lights the red LED)
    //when the #1 button is push
    if (results.value == 0xFD08F7){
      digitalWrite(Red, HIGH);
      LOCKstatus = 1;
      Serial.println(LOCKstatus); // Reads Lock Status to check if the signal worked
      Serial.println("Lock Disarmed");
    }
  }
  
   //This loop relocks the lock (un-lights the red LED)
    //when the #2 button is push
   if (results.value == 0xFD8877){
      digitalWrite(Red, LOW);
      LOCKstatus = 0;
    }
  

Now that we have the red LED’s status recorded, the final step is to turn on the blue LED through the button, but only when the red LED is on. We do this through another if statement, however first we should set the buttonState variable to read our button’s signal, at the beginning of our void loop. This will allow us to act on the signal sent by the button.

 buttonState = digitalRead(buttonPin);

The button has two states LOW & HIGH or pushed and not pushed. We want the blue LED to turn on ONLY if both the red LED is on and the button is pushed. The below IF statement renders this logic.

  if (buttonState == LOW & LOCKstatus == 1) {
     // turn LED on:
    digitalWrite(Blue, HIGH);
    } else { // Otherwise, turn off the Blue LED
    // turn LED off:
    digitalWrite(Blue, LOW);
   

There we have it, now we can turn on the blue light once we turn on the red light. Unfortunately, I can’t screenshot the button and press it at the same time, but you get the idea.

The final Tinkercad product can be found here. Try it out!

Leave a Reply

Your email address will not be published. Required fields are marked *