Exercise 2 – Blink

Part 1

At first I was somewhat intimidated to get started, as the tutorial and schematic showed the use of a resistor. Despite comparing my materials to descriptions on the Electronics Club component list I was unsure whether I had the correct resistor. After reading the comments in the code from the repository, however, I realized I could simply connect a light directly to the #13 pin and ground pin and attached it that way. From there it was as simple as connecting the board via USB, selecting the com port, and uploading the code.

Part 2

Changing the timing for the light was incredibly simple. All that was required was changing the values for the onTime and offTime variables.

Part 3

Realizing that I now needed to use resistors to set up multiple lights, some further research revealed that I did in fact have the ones I needed. With this new knowledge I set to connecting my breadboard to the Arduino using cables from the 5V and ground power terminals on the Arduino to the positive and negative lines on the board. Two cables were also used to connect two digital pins on the Arduino to the long pins on each of the lights. The resistors were then used to connect the short pins to the ground/negative line on the breadboard.

As for the code, I created a second variable to reference the new pin being used for the second light. From here all that was required was adding a second instance of digitalWrite() high and low to the loop for that second light. I also removed any delay after the low write so that one of the lights would be on at all times.

Part 4

For adding a third light the process was the same as adding the second. The addition of the light, a third cable, and resistor completed the physical portion. The code also simply needed an additional variable to reference the pin, a new line to set the pinMode in setup(), and additional digital writes in the loop. Finally, reversing the sequence was as simple as swapping the value assigned to the first and third pin variables.

Part 5

The message which I chose to implement was the standard SOS pattern in Morse Code, which follows the pattern “… — …”. To do this, I used three lights with one light relaying each character. Code for this followed the same pattern for each light, turning the light on and off with digitalWrite() three times, with a delay after each write. As the first and last letters are the same, they shared the same, smaller delay value and the middle light used a longer delay.

I recorded a video of this but no matter what I tried WordPress refused to upload it.

Part 6

  1. In order to cause the light to blink without using delay() I worked based upon the Blink Without Delay tutorial. I added two new global variables to keep track of an elapsed time and hold a max delay time. On each iteration of the loop, the current time is saved using the millis() function. The elapsed time is subtracted from this value and if it is greater than the max delay time, the previous time will be set to the current and the state of the light is swapped to whatever it is not (high or low) using a nested if() statement. The light state is saved using another global variable. Finally digitalWrite() is used to turn the light on or off.
  2. As before, the only change required to make the light blink at a different rate was changing the delay value.
  3. Code require for the second light required the addition of global variables for the pin and its state. Making the lights alternate was as simple as setting the second light’s state to LOW when the first is set to HIGH within the nested if(). Another digitalWrite() for the second light is also included.
  4. Making the three lights blink in sequence required slight modifications to the if() statements of the loop. The first check of the state of the lights now checks if both the first and second lights are off, if so it will turn the first on and second and third off. The next now simply checks if the first light is on and if so turns it off and the second on. Finally an else block turns the second off and the third on.
  5. Following the same three light setup as I did for the delay() implementation, getting them to display a message required a variable to keep track of the max number of times I wanted each light to blink and one to track which light should be currently blinking. Two more were used to hold values for the time lights should remain for dots and dashes. From there I simply checked what the current light was, compared the current minus the previous time against either the value for a dot or dash, and if it was greater turned on the light and incremented the blink count. Once the light had blinked the needed number of times, the light count was incremented to move on to the next and the blink count reset.
Create your website at WordPress.com
Get started
%d bloggers like this: