Digital Electronics Lecture Homework #2

Part #1: Find the Errors:

In this code, there are three errors that prevent the circuit, particularly the second button and corresponding LED, from working properly.

  1. “int buttonpin= 7” needs semicolon, so, “int button 7;”
    The first error is a simple syntax problem. In establishing the integer for the first button, there needs to be semicolon after the variable.
  2. set pinMode for button 2 and led2
    The next error regards the function “pinMode”. In order for the code to function properly”recognize” the components and set them as inputs or outputs, we need to make sure the pinMode is set. In this situation, pinMode is not set for the buttonPin2 or its corresponding LED, ledPin2. In order to change this, we should add “pinMode(buttonPin2, INPUT);” and pinMode “(ledPin2, OUTPUT)” under “void setup ()”. This allow the code to receive information about the button state as an input, and the LED to be influenced by other functions and components as an output, which means blinking or not blinking, in this case.
  3. add “checkbutton2”
    At the bottom of the code, we see that the code for the second button is correct, but it is lacking an important piece of information prior to this code. We see that the code uses the new function “void checkButton2() {“, however, this is not established as a function before its use in programming the second button. In order for it to work, right beneath “void loop() {“, we must add “checkButton2();” after “checkButton1();” Otherwise, the program will not recognize it as a function that can be used within the code.

With these changes, the code and circuit will be able to function perfectly. Here is a link to the adjusted circuit:

https://www.tinkercad.com/things/eH2cryqYmus

Part #2: Explain the Code

These three functions seem very similar, however, their functionalities vary greatly based on seemingly minute changes in the structure and content of the code. Here is an explanation of how all three of them behave and why.

function1()

When this function is utilized in “loop,” we see that when multiple are on, the LEDs will blink in order from left to right. If just an individual switch is on, it will blink independently. Each LED and corresponding switch has its own respective “if” statement, or conditional. Each conditional says that if a switch is turned on, then its corresponding LED will blink on and off at a speed of 500 milliseconds, or half a second. If the switch is turned on, the LED will remain off. The reason why the LEDs light up from left to right, rather than synchronously lies in the order of the code. The code will always move from top to bottom. When all of the switches are on, the code will perform the function of blinking the first LED on and off once, and then moving to the second LED and it on and off, and so on. In this way, when the function repeats, it looks as if it is always moving in a pattern from left to right.

function2()

The second function behaves in a slightly different way, primarily because of its utilization of “else if” statements. Purely from just playing with the circuit, we notice that the LEDs will only light independently, and only if the LEDs preceding it are off. This concept is expressed in code in a similar way using the “else if” statement. “Else if” sort of translates to “if the prior “condition(s)” are not true, the following will happen if this new statement is true.” It sounds a little complex, but this how it best makes sense in my mind.

So, in this case, the first “if” statement implies that if the first switch is turned on, then the first LED will turn on. We’ll notice that none of the other LEDs will turn on even if we flip the switches, because “else if” statements imply that the preceding statements must be untrue, which is impossible since the first lit LED represents that the first conditional that is true. Let’s take a look at the first “else if” statement that affects the second LED:

else if(digitalRead(switchPin2) == HIGH) {
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);

In non-coding language, this means that if the first switch is not turned on, and the second switch is turned on, then the second LED will turn on. If the second switch is not, the code will skip over the first two lights, and move onto the third. Again, if the two preceding statements are untrue (the first switch is on or the second switch is on) and if the third switch is on, then the third LED will turn on. The preceding conditionals must be untrue for this to happen. This same pattern continues for the fourth LED. Given the nature of this structure, only one LED can be on at a time, and the preceding LEDs and switches must be off in order for this to happen.

function3()

This code appears very similar to that of function1(), however, when we play with the circuit, we immediately notice a big difference. While the first code for function1 was written in a way that caused all of the LEDs to flash asynchronously from left to right, this circuit appears to allow multiple LEDs to flash together in sync. This is because in the first code each switch and corresponding LED revolved around their own independent delay time of 500 milliseconds, and the code moved through in order from top to bottom (the first LED to the fourth LED), causing each one to light separately. However, this code is written differently.

if(digitalRead(switchPin1) == HIGH) {
digitalWrite(ledPin1, HIGH);
}

if(digitalRead(switchPin2) == HIGH) {
digitalWrite(ledPin2, HIGH);
}

if(digitalRead(switchPin3) == HIGH) {
digitalWrite(ledPin3, HIGH);
}

if(digitalRead(switchPin4) == HIGH) {
digitalWrite(ledPin4, HIGH);
}

delay(500);

digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);

delay(500);

While each of the LEDs maintains their own respective if statements, we can see that none of the conditionals contain a delay time. The if statements simply say, “if this switch is on, then this LED will turn on,” rather then, “if this switch is, then this LED will turn on for 500 milliseconds and turn off for 500 milliseconds.” This causes all of them to light up in sync. Following the if statements, all of the LEDs will remain on for a universal value of 500 milliseconds. After that, they will all turn off at the same time for another period of 500 milliseconds. Because none of the if statements have their own delay times that the code must independently run through from top to bottom, they all operate on the same singular delay time, causing them to blink synchronously.

Part #3: Simplify the Code

The following is a link to my TinkerCad simplified circuit:

https://www.tinkercad.com/things/fHL0PQlfbDQ

Leave a comment

Design a site like this with WordPress.com
Get started