Introduction: Dorm Door Sign

The purpose of this Instructable is to act as a reference for how to code a basic circuit for the final sign for the Circuits and Microcontrollers Tools Mastery Module in Duke University's EGR 101 class, if you are new to Arduino coding. If you have experience with Arduino coding, we would like you to try to code your circuit by yourself; however, this Instructable can be used as a reference if you are stuck.

This Instructable is under the assumption that you have completed the Arduino and Circuitry Instructable from earlier in the Circuits and Microcontroller Tools Mastery Assignment, so it will not go into detail about how to configure the Arduino IDE or items like attributing code.

Instead, this Instructable will only contain detailed descriptions of example Arduino code files for an example of a dorm door sign that fulfills the requirements of a sign that has two user-controlled states. This dorm door sign has a green LED, a red LED, and a button. With the example codes in the following steps, a button press on the sign will alternate which LED is turned on. Also, unless the circuit loses power, the LED being turned on will stay on, even if the button is not being pressed. As a result, since this sign can remember and maintain two distinct states (green LED on or red LED on) when the button is not being pressed, this sign has two user-controlled states.

Please note that the example codes in the following steps are tailored specifically to this dorm door sign's circuit. Your code will most likely be rather different.

Step 1: Arduino Code Example #1

In this step, the code for the dorm door sign's circuit is given in the images above. This step will also explain how the code works to create a sign that has two user-controlled states.

The code begins with a block comment that outlines the attribution of the code and a description of the purpose of the code. Throughout the code, there are line comments that describe what that line of code is doing.

After the block comment, the code initializes several constant int variables for the pins of the button, red LED, and the green LED of the dorm door sign and a separate int variable named presscounter. The initialization of the three pin numbers is to be more explicit later on in the code in order to avoid confusion when using the digitalWrite() function. As for the int variable presscounter, the purpose of this variable is to keep track of button presses in the circuit. How this int variable is used will be explained in more detail when we explain the conditional statements.

After the initialization of the 4 variables, we reach the void setup() portion of the code. This part of the code is only run once when the Arduino is turned on, so we use it to set the pinMode of each of pins that we are using in this dorm door sign. In this example, we set the pins for the red and green LEDs as output pins (note that we use the variable names redledPin and greenledPin instead of the actual pin number so that it is easier to follow), and we set the pin for the button is an input pin. This setup allows us to output signals/voltages from the LED pins to the LEDs and input signals/voltages from the button to the button pin on the Arduino.

Moving forward, we reach the void loop() part of the code. This loop part is constantly being run as the Arduino is turned on, so we can put some if-else statements in this part to allow our sign to work.

The first part of the loop is an if statement that is entered if the button pin detects that the button is being pressed. When this if statement is entered, the presscounter variable (which is 0 when the Arduino is first started) increased by 1. By increasing the presscounter by 1, we can count the amount of times that the button has been pressed and use this value of the presscounter variable to create conditional statements for the LEDs later on. In addition to increasing the value of the presscounter variable by 1, a delay of 500 milliseconds is also engaged. This delay, also known as a debounce, is used because the Arduino is constantly looping through the loop() function; so, it recognizes holding down the button as the same as pushing the button multiple times. As a result, if you press the button for a few milliseconds too long, the Arduino can read that press as multiple button presses. So, we would use a delay or debounce to make the Arduino wait before it can detect more button presses. In ths code specifically, this will allow you to press and hold the button for up to 500 milliseconds before the Arduino registers it as more than one button press.

In the next part of the loop, we have an if statement and an else if statement. In the if statement, the conditional statement is "presscounter % 2 == 0" where the "%" is the modulo operator. The modulo operator in Arduino code finds the remainder after the division of the 2 numbers. For example, 9 % 2 is equal to 1, where 1 is the remainder. Thus, this if statement is saying that if the remainder of the presscounter value divided by 2 is equal to zero (meaning that the presscounter value is even), enter the if statement. In the if statement, the Arduino writes the red LED to be off or LOW and writes the green LED to be on or HIGH. As for the else if statement, we enter the else if statement if the presscounter value is odd ("presscounter % 2 == 1"), and in the else if statement, we turn the green LED on and turn the red LED off. Since we increment the presscounter value by 1 each time the button is pressed, we get a consistent way to switch between the two user-controlled states of the sign.

Step 2: Arduino Code Example #2

This step outlines a slightly different approach in coding the circuit for a dorm door sign.

In the beginning, the code is exactly the same as the first example with the modulo operator; variables were declared and pinModes for the different Arduino pins are set up. The main difference occurs in the void loop() portion of the Arduino code.

In the loop() function, the code starts by having an if statement that runs when the Arduino detects that the button has been pressed. In this if statement, there is a nested if and else-if statement that looks at the value of the int presscounter. When the value of presscounter is 0, the if statement will change the value of presscounter to 1. When the value of presscounter is 1, the else-if statement will change the value of presscounter to 0. Closing out of the parent if statement, there is a 500 millisecond delay that acts as a debounce for the same reasons described in the first example.

Then, the if and else-if statements following the button if statement is almost exactly the same as the first example. The only difference is that the conditional statements in each has changed. In the if statement, when presscounter equals 0, the red LED will be turned off and the green LED will be turned on. In the else-if statement, when presscounter equals 1, the green LED will be turned off and the red LED will turn on.

So, as we press the button, the value of the int presscounter switches between a 1 and a 0, which then satisfies the two different conditional statements in the loop() function. Since the int value holds even after the button is not pressed, the LEDs will stay in a given state until either the button is pressed or the power is turned off. This code thus still works in giving the dorm door sign two user-controlled states.

Step 3: Final Remarks

As you can see from the example code files to a dorm door sign. There are many different ways that you can code a the sign to have at least two user-controlled states. While these example codes may not work with your circuit without some modifications, these should provide a good idea on how you should approach coding your own sign. When creating the code for your sign, be sure to keep the following points in mind:

  • Use variables to switch between the states in your sign. In all the examples of the code, the int variable presscounter was initialized and used in conditional statements in the loop() function. By changing the value of the variable presscounter when the button is pressed, the different conditional statements in your loop() function can be met to change the state of your sign.
  • Use a delay/debounce in your code. As the loop() function is constantly being run when the Arduino is turned on, it is useful to have a delay/debounce in your code to avoid having the Arduino register too many button pressed. This will allow you to have more control over which state you want your sign to be in.

Using this Instructable as a reference, you should now work towards creating the code for your sign. If you have any questions about the example code or are confused about anything, please let a TA or Lab RAT know.