Download as pdf or txt
Download as pdf or txt
You are on page 1of 235

Microcontrollers for Steam-AI Students Kamil Bala

1.Digital Outputs (Part 1)

1.Digital Outputs (Part 1) ........................................................................................................... 1


1.1.Led Outputs .......................................................................................................................... 3
1.1.0 Basic Arduino board connection ............................................................................... 3
1.1.1. External LED Blinking Program .............................................................................. 9
1.1.2. External LED Blinking Program: Using Breadboard............................................. 10
1.1.3. Alternating Two LEDs Blinking ............................................................................ 11
1.1.4. Sequential Blinking of Five LEDs ......................................................................... 18
1.1.5. Sequential Blinking of Eight LEDs ........................................................................ 25
1.2.Led Patterns and Output Multiplexing ............................................................................... 33
1.2.1. Giving the Desired Value Using a Variable ........................................................... 33
1.2.2. Simplifying Tasks Using Functions (6-13) ............................................................ 40
1.2.3. Sequentially turns on and off the correct LEDs from 13 to 6 ................................ 46
1.2.4. Sequentially turns on and off LEDs (13-6, 6-13) ................................................... 52
1.2.5. Sequential Lighting up of LEDs 6-13 and Turning Them All Off Simultaneously
.......................................................................................................................................... 60
1.2.6. 6-13 Sequentially lighting and then extinguishing LEDs 6-13 .............................. 66
1.2.7. Program that sequentially adds and removes LEDs 6-13 ...................................... 73
1.2.8. Program that sequentially collects LEDs 13-6 and turns them off all at once ...... 80
1.2.9. Program that sequentially lights up LEDs from 13-6 and extinguishes them from
6-13................................................................................................................................... 87
1.2.10.Program accumulating and then subtracting LEDs in sequence 13-6 ................... 94
1.2.11. Program to add and subtract 6-13, 13-6 LEDs ..................................................... 96
1.2.12. Outward-Blinking Lights Program..................................................................... 102
1.2.13. sequentially lights up from the inside out, then extinguishes simultaneously ... 108
1.2.14. sequentially lights up from the inside out, then extinguishes from the inside out
........................................................................................................................................ 115
1.2.15. lights up LEDs from the inside out, then extinguishes them from the outside in
........................................................................................................................................ 121
1.2.16. Outward to Inward Blinking Program ................................................................ 128
1.2.17. LEDs blinking from the outside to inside, simultaneously ................................ 133
1.2.18. LEDs that gather from outside to inside and fade from outside to inside .......... 139
1.2.19. LEDs that gather from outside to inside and fade from inside to outside .......... 145
1.2.20. Randomly Blinking LEDs............................................................................ 151
1.2.21. Traffic Light Simulation Program ...................................................................... 157
1.2.22. Intersecting LEDs ............................................................................................... 163
1.2.23. 3-part snake roaming LEDs 2-13 sequentially ................................................... 170

1
Microcontrollers for Steam-AI Students Kamil Bala

1.2.24. 3-part snake roaming LEDs 13-2 sequentially ................................................... 176


1.2.25. LED Array 1 Animation ..................................................................................... 178
1.2.26. LED Array 2 Animation ..................................................................................... 184
1.2.27. Multiplexing 1_8 Output .................................................................................... 191
1.2.28. Port Multiplexing_2_8 Output_Test .................................................................. 197
1.2.29. Port Multiplexing_3_8 Output_Test .................................................................. 202
1.2.30. Port Multiplexing_16 Output ............................................................................. 208
1.2.31. Port Çoğullama_16 Çıkış _test ........................................................................... 214
1.2.32. Port Multiplexing_24 Outputs ............................................................................ 220
1.2.33. Dynamic LED Snake Animation with Arduino 2-13 ......................................... 226
1.2.34. Dynamic LED Snake Animation with Arduino 13-2 ......................................... 233

2
Microcontrollers for Steam-AI Students Kamil Bala

1.1.Led Outputs
1.1.0 Basic Arduino board connection

https://www.tinkercad.com/things/4nrp20nHrzJ-110-basic-arduino-board-connection

/***************************************************************************
**
Program Name: Program that blinks an internal LED
Program Objective: We are making the built-in LED blink

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
lnk:https://www.linkedin.com/in/kamil-bala-b4a100b6/
Yalova/2023
***************************************************************************
**/
void setup()
{
pinMode(13, OUTPUT); // Pin 13 is set as output
}

void loop()
{
digitalWrite(13, HIGH); // Pin 13 is made high, meaning it turns the LED on
delay(1000); // Waits for 1000 ms, 1000ms = 1s
digitalWrite(13, LOW); // Pin 13 is set to low, meaning it turns the LED off
delay(1000); // Waits for 1000 ms, 1000ms = 1s
}

3
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:


Program B is a typical “Hello, World!” program for Arduino and is often one of the first
projects undertaken when starting with a microcontroller. The code is a simple example
demonstrating how to control an LED.
1. void setup(): This function runs once when the program starts. It is usually used for
setting pin modes, initiating serial communication, etc. o pinMode(13, OUTPUT);
here, it specifies that pin number 13 on the Arduino is converted to an output pin (i.e.,
a pin that can provide power). This is typically used for controlling an LED because
power is needed to turn on the LED.
2. void loop(): This function continually runs over and over again after the setup()
function completes. So, the codes written here are executed continuously.
• digitalWrite(13, HIGH); this command causes power (typically 5V or 3.3V) to
be supplied to pin number 13, thereby allowing current to flow through this
pin. If this pin is connected to an LED, this command lights up (turns on) the
LED.
• delay(1000); this function pauses the program’s operation for the specified
period (in this case, 1000 milliseconds or 1 second). This allows the LED to
remain lit for a while.
• digitalWrite(13, LOW); this command cuts the power to pin number 13, so no
current flows through the pin anymore. If this pin is connected to an LED, this
command turns off the LED.
• The subsequent delay(1000); command ensures the LED stays off for a while.
The general purpose of this code is to create a continuous blinking effect by
turning the LED on for one second and off for one second. This simple
structure is often used to test whether the system is working correctly and
serves as an introduction to the world of microcontrollers.

4
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:
1. The Program Starts:
• When the Arduino receives power (for example, when connected to a
computer via USB or connected to another power source), the program begins.
2. Setup Function Executes:
• The void setup() function is executed. This function runs only once and does
the initial configurations of the Arduino.
• At this stage, with the pinMode(13, OUTPUT); command, pin number 13 is set
to output mode. This means we can send an electrical signal to the LED
connected to this pin.
3. Loop Function Begins:
• The void loop() function starts. This function is repeated continuously as long
as the Arduino is operating (thus forming a loop).
4. LED Turns On:
• The digitalWrite(13, HIGH); command is executed. This sends a “HIGH”
(high) voltage (typically 5V or 3.3V) to pin number 13. In this case, when the
pin is in the “HIGH” state, the LED connected to the pin turns on (i.e., receives
electric current and emits light).
5. One Second Pause:
• The delay(1000); function makes the program wait here for 1000 milliseconds
(i.e., 1 second). During this time, the LED remains lit because the voltage of
the pin has not yet been lowered.
6. LED Turns Off:
• The digitalWrite(13, LOW); command is executed. This brings the voltage of
pin number 13 to “LOW” (low), or 0V. As a result, the LED no longer receives
power and turns off.
7. Another One Second Pause:
• The second delay(1000); function makes the program wait again for 1000
milliseconds (i.e., 1 second). During this period, the LED is off because the pin
is in the “LOW” state, and the electric current is cut off.
8. Repetition of the Cycle:
• When the end of the void loop() function is reached, it returns to the beginning,
and steps 4 through 7 are repeated. This process continues until the power to
the Arduino is cut or another stop command is issued. In conclusion, this
program provides simple visual feedback by sequentially blinking an LED.
This is a simple test that often helps determine whether the hardware is
functioning correctly.

5
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. Using Different Pins: If you want to connect the LED in the program to another pin,
what changes would you need to make, and what would be the impact of these
changes on the system?
2. Optimizing Delay Times: How can the delay periods (delay) in the program be
optimized, and what is the impact of this optimization on the blinking behavior of the
LED?
3. Controlling Multiple LEDs: If you want to add more than one LED to the program,
what approach would you take, and what are the necessary code and circuit
modifications?
4. Debugging Strategies: If the LED is not working as expected, how would you
diagnose this situation, and what debugging steps would you apply?
5. Energy Consumption: How can the energy consumption of such a blinking LED
setup be calculated, and what strategies would you suggest to reduce energy
consumption?
6. Interacting with User Input: If you want to control the blinking duration of the LED
with user input, such as a button or potentiometer, what are the required code and
hardware changes?
7. Expanding the Program: How would you modify this basic blinking LED program
to make it more complex, for instance, producing different light patterns in response to
a specific signal or trigger?
8. Safety Measures: What safety precautions should you take when setting up such a
circuit, and why are these precautions important?
9. Alternative Methods: What alternative methods can be used to control the LED,
other than the digitalWrite() and delay() functions, and what are the advantages and
disadvantages of these methods?
10. Real-World Applications: How could you apply this simple blinking LED example
to real-world scenarios? For example, what are your thoughts on using it as an alert
system or user interface signal?

6
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Using Different Pins:

o To connect to a different pin, you would need to change the pin number used
in the pinMode() and digitalWrite() functions. This is often done to avoid pin
conflicts with other hardware or to use pins with specific features.

2. Optimizing Delay Durations:

o Optimizing delay durations depends on the application. For instance, shorter


delays might be preferred if user interaction is required. Altering delay
durations directly affects the blinking frequency of the LED.

3. Controlling Multiple LEDs:

o To add multiple LEDs, you need to define separate output pins for each LED
and use the digitalWrite() function for each. It’s also important to design your
circuit to meet the power requirements of each LED.

4. Debugging Strategies:

o Troubleshooting usually starts with checking a simple electrical circuit (e.g.,


verifying connections, checking for burned-out LEDs, etc.), followed by
reviewing the code (pin assignments, function calls, etc.), and finally
inspecting the physical state of the hardware (cable connections, solder joints,
etc.).

5. Energy Consumption:

o To calculate energy consumption, you need to know the current drawn by the
LED and its operating voltage. These values are multiplied to find the power
consumption (in Watts). Ways to reduce energy consumption include using
lower-power LEDs or decreasing the LED’s on-time.

6. Interacting with User Input:

o For user interaction, you’ll need to connect an input device (like a button or
potentiometer) and make additions to your code to read the state of this device.
You can control the blinking speed of the LED by adjusting the delay function
according to input values.

7. Expanding the Program:

o To create more complex light patterns, you could employ different timing
options, use multiple LEDs, incorporate colored LEDs (such as RGB LEDs),
and program them to generate various patterns and sequences.

8. Safety Precautions:

7
Microcontrollers for Steam-AI Students Kamil Bala

o Safety measures include operating at low voltage, correctly connecting circuit


components (e.g., using the right resistors), taking precautions to prevent
circuit overheating, and keeping the circuit away from water or other
hazardous materials.

9. Alternative Methods:

o Beyond digitalWrite() and delay(), you can achieve more precise control using
timer interrupts, or adjust the LED’s brightness using PWM (Pulse Width
Modulation). Each method has its advantages (e.g., accuracy, code complexity)
and disadvantages.

10. Real-World Applications:

o In real-world scenarios, such a blinking LED setup could be used as a status i

8
Microcontrollers for Steam-AI Students Kamil Bala

1.1.1. External LED Blinking Program


https://www.tinkercad.com/things/0oC9NRvKo64-111external-led-blinking

/*************************************************************************
Program Name: External LED Blinking Program

Program Purpose: We are connecting an external LED to pin 13,


where the control LED is connected.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
void setup()
{
pinMode(13, OUTPUT); // Pin 13 is set as output
}

void loop()
{
digitalWrite(13, HIGH); // Pin 13 is set to high, turning the LED on
delay(1000); // Waits for 1000 ms, i.e., 1 second
digitalWrite(13, LOW); // Pin 13 is set to low, turning the LED off
delay(1000); // Waits for another 1000 ms, i.e., 1 second
}

9
Microcontrollers for Steam-AI Students Kamil Bala

1.1.2. External LED Blinking Program: Using Breadboard


https://www.tinkercad.com/things/f5Bai63kZvP-112external-led-blinking-programusing-
breadboard

/*************************************************************************
Program Name: External LED Blinking Program: Using Breadboard

Program Purpose: We are connecting an external LED to pin 13,


where the control LED is connected.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
void setup()
{
pinMode(13, OUTPUT); // Pin 13 is set as output
}

void loop()
{
digitalWrite(13, HIGH); // Pin 13 is set to high, turning the LED on
delay(1000); // Waits for 1000 ms, i.e., 1 second
digitalWrite(13, LOW); // Pin 13 is set to low, turning the LED off
delay(1000); // Waits for another 1000 ms, i.e., 1 second
}

10
Microcontrollers for Steam-AI Students Kamil Bala

1.1.3. Alternating Two LEDs Blinking

https://www.tinkercad.com/things/0Z2fEnaJtYk-113alternating-two-leds-blinking

/**************************************************************************
***
Program Name: Alternating Two LEDs Blinking

Program Purpose: To make two LEDs blink alternately

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/

void setup()
{
// We set pin 13 as output
pinMode(13, OUTPUT);
// We set pin 12 as output
pinMode(12, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH); // We set pin 13 to logical HIGH
delay(1000); // We provide a 1000 ms delay
digitalWrite(13, LOW); // We set pin 13 to logical LOW
delay(1000); // We provide a 1000 ms delay
digitalWrite(12, HIGH); // We set pin 12 to logical HIGH
delay(1000); // We provide a 1000 ms delay
digitalWrite(12, LOW); // We set pin 12 to logical LOW
delay(1000); // We provide a 1000 ms delay

11
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

1. Initial Configuration (setup function):

o When the program starts, the setup() function runs only once.
o With the commands pinMode(13, OUTPUT); and pinMode(12, OUTPUT);, pins
numbered 13 and 12 are set to output mode. These pins will be used to control
the LEDs connected to them.

2. Main Loop (loop function):

o The loop() function repeats continuously for as long as the Arduino is operating.
o First, pin number 13 is set to the “HIGH” state with the digitalWrite(13, HIGH);
command, meaning power is applied to this pin. This causes the LED connected
to pin number 13 to light up.
o The delay(1000); function pauses the program for 1000 milliseconds (1 second),
during which the LED on pin 13 remains lit.
o With the digitalWrite(13, LOW); command, pin number 13 is set to the “LOW”
state, cutting off the power. This turns the LED off.
o The program is again paused for 1 second with the next delay(1000); function.
During this time, the LED on pin 13 is off.
o Then, with the digitalWrite(12, HIGH); command, pin number 12 is set to the
“HIGH” state, which lights up the LED connected to pin number 12.
o The program is paused again for 1 second with the subsequent delay(1000);, and
the LED on pin 12 remains lit.
o The digitalWrite(12, LOW); command cuts off power to pin number 12, and the
LED goes out.
o The final delay(1000); function pauses the program one more time for 1 second
at the end of the cycle.

These processes continuously repeat in a cycle, so the first LED lights up and goes out,
followed by the second LED lighting up and going out. This creates a sequence of flashing
lights and continues until the Arduino is stopped. Programs of this sort are often used to
attract visual attention, signal, or create simple animated displays.

12
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Program Starts:

o When the Arduino receives power or is restarted, the program begins.

2. Setup Function Executes:

o The void setup() function is executed. This function runs only once and is
typically used for setting pin modes, starting serial communication, etc.
o pinMode(13, OUTPUT);: Pin number 13 is set to output mode, allowing us to
send an electrical signal to the LED connected to this pin.
o pinMode(12, OUTPUT);: Similarly, pin number 12 is also set to output mode.
This pin will also control an LED.

3. Loop Function Starts:

o The void loop() function begins. This function continuously repeats as long as
the Arduino is operating.

4. First LED Turns On:

o digitalWrite(13, HIGH);: This command causes a “HIGH” voltage to be sent to


pin number 13. In this case, the LED connected to the pin lights up (i.e., it
receives electrical current and emits light).

5. Wait for One Second:

o delay(1000);: The program pauses here for 1000 milliseconds (or 1 second).
During this time, the first LED remains on because the voltage on the pin has
not yet been lowered.

6. First LED Turns Off:

o digitalWrite(13, LOW);: This command sets the voltage of pin number 13 to


“LOW.” As a result, the first LED turns off as it no longer receives power.

7. Another One-Second Pause:

o delay(1000);: The program pauses again for 1000 milliseconds (or 1 second).
During this time, the first LED is off because the pin is in a “LOW” state.

8. Second LED Turns On:

o digitalWrite(12, HIGH);: Now, pin number 12 is set to “HIGH,” causing the


second LED connected to pin number 12 to light up.

13
Microcontrollers for Steam-AI Students Kamil Bala

9. Wait for One Second:

o delay(1000);: The program pauses for 1000 milliseconds (1 second). During


this period, the second LED stays on.

10. Second LED Turns Off:

o digitalWrite(12, LOW);: This command sets the voltage of pin number 12 to


"LOW," turning off the second LED as it no longer receives power.

12. Another One-Second Pause:

o delay(1000);: The program pauses again for 1000 milliseconds (or 1 second).
During this time, the second LED is off.

12. Continuation of the Cycle:

o All these steps (from 4 to 11) are continuously repeated, creating an


"alternating flashing" effect. The first LED blinks, followed by the second
LED, and this cycle continues until the Arduino is stopped.

This code allows for the LEDs to blink in a specific pattern and is commonly used in simple
signaling, visual attraction, or educational electronic projects.

14
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. Circuit Design and Configuration: How would you redesign this circuit using a
different microcontroller platform (for example, Raspberry Pi, ESP32), and what are
the differences on these platforms?
2. Power Management: If you have concerns about the energy consumption of the
circuit, how would you optimize power usage, and what are the effects of these
optimizations on the circuit’s performance?
3. Debugging Processes: If the circuit does not work as expected, what steps do you
follow to diagnose and resolve issues?
4. Multiple LED Scenarios: If you want to blink multiple LEDs in more complex
patterns, how would you implement this functionality, and what challenges might you
encounter in this scenario?
5. User Interaction: If you want to add interactivity to the circuit, that is, allow users to
control the blinking speed or pattern of the LEDs, what hardware and software
changes are required?
6. Expandability: How could you expand this circuit with other types of sensors or
output devices, and what are the main considerations when making these expansions?
7. Different Signaling Methods: Apart from LEDs, what signaling methods can you use
(for example, sound, motion, etc.), and what are the advantages and disadvantages of
these methods?
8. Safety Measures: What safety precautions must you take when setting up and running
the circuit, and why are these precautions important?
9. Software Optimization: What techniques do you apply to improve performance and
optimize memory usage on the software side?
10. Real-World Applications: What could be the real-world applications of this simple
circuit? For example, in what areas could you use this type of circuit, such as home
automation, security systems, or entertainment products, and what modifications
would be necessary for these applications?

15
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Circuit Design and Configuration:


o Different microcontrollers have different pin configurations, operating
voltages, and programming environments. For example, on a Raspberry Pi,
you could perform similar control operations through GPIO pins, but coding
may be done in a different language like Python, and the current limitations per
pin may be different.
2. Power Management:
o To reduce energy consumption, you could implement strategies such as
lowering the LEDs’ brightness, disabling unused components, or putting the
microcontroller into sleep mode. However, these changes could affect the
visibility of the LEDs or extend response times.
3. Debugging Processes:
o Troubleshooting usually starts with a check of a simple electric circuit (for
example, checking connections, burnt LEDs, etc.), followed by a review of the
code (pin definitions, function calls, etc.), and finally the hardware’s physical
condition (cable connections, solder joints, etc.).
4. Multiple LED Scenarios:
o To create complex LED patterns, you may need to develop more sophisticated
logic in your program, using loops, conditional statements, and perhaps timing
functions. You can also use variables to track the status of each LED.
5. User Interaction:
o To provide user interaction, you could add input devices like potentiometers or
buttons and write code pieces to read their values. These values could be used
to adjust the speed or pattern of the blinking LEDs.
6. Expandability:
o To expand the circuit, you could integrate various sensors and output devices,
such as temperature sensors, motion detectors, or sound modules. This could
allow your circuit to respond to environmental conditions or perform a wider
range of functions.
7. Different Signaling Methods:
o You could use alternative signaling methods such as audio alerts, vibration
motors, or LCD screens. Each has its own advantages (for example, suitability
for the visually impaired, remote sensing) and disadvantages (power
consumption, complexity).
8. Safety Measures:
o Safety measures should be taken to prevent electric shock, reduce the risk of
short circuits, and use necessary resistances to protect components in
overcurrent conditions. Additionally, it’s important to place the circuit safely
and not touch it while it’s operating.

16
Microcontrollers for Steam-AI Students Kamil Bala

9. Software Optimization:
o To optimize your code, you can avoid unnecessary variables and delays,
effectively use functions and loops, and perhaps employ more advanced
programming techniques like interrupts.
10. Real-World Applications:
o This circuit could be used as an alarm indicator in a security system, in
interactive art projects, in educational DIY electronic kits, or even in a simple
home automation system. Each application would bring different requirements
(outdoor suitability, energy consumption, user interface, etc.) and specific
adaptations (wireless control, sensor integration, etc.).

17
Microcontrollers for Steam-AI Students Kamil Bala

1.1.4. Sequential Blinking of Five LEDs

https://www.tinkercad.com/things/7pDmaPLinjk-114-sequential-blinking-of-five-leds

/*************************************************************************
Program Name: Sequential Blinking of Five LEDs

Purpose of the Program: To make five LEDs blink in sequence

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/

void setup()
{
// Set pin 13 as output
pinMode(13, OUTPUT);
// Set pin 12 as output
pinMode(12, OUTPUT);
// Set pin 11 as output
pinMode(11, OUTPUT);
// Set pin 10 as output
pinMode(10, OUTPUT);
// Set pin 9 as output
pinMode(9, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH); // Set pin 13 to logical HIGH
delay(1000); // Wait for 1000 ms
digitalWrite(13, LOW); // Set pin 13 to logical LOW
delay(1000); // Wait for 1000 ms
digitalWrite(12, HIGH); // Set pin 12 to logical HIGH
delay(1000); // Wait for 1000 ms
digitalWrite(12, LOW); // Set pin 12 to logical LOW
delay(1000); // Wait for 1000 ms
digitalWrite(11, HIGH); // Set pin 11 to logical HIGH
delay(1000); // Wait for 1000 ms
digitalWrite(11, LOW); // Set pin 11 to logical LOW
delay(1000); // Wait for 1000 ms
digitalWrite(10, HIGH); // Set pin 10 to logical HIGH
delay(1000); // Wait for 1000 ms
digitalWrite(10, LOW); // Set pin 10 to logical LOW
delay(1000); // Wait for 1000 ms
digitalWrite(9, HIGH); // Set pin 9 to logical HIGH
delay(1000); // Wait for 1000 ms
digitalWrite(9, LOW); // Set pin 9 to logical LOW
delay(1000); // Wait for 1000 ms
}

18
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program is designed to control multiple LEDs (in this case, five LEDs) in
sequence. Each LED is turned on for a certain period and then turned off, creating a kind of
“sequential” or “running” blinking effect. Each LED is connected to a specific digital pin of
the Arduino. Here is a general description:
1. Initial Setup:
o The setup() function runs once with the initiation of the Arduino. Within this
function, the digital pins to be used (in this case, pins 13, 12, 11, 10, and 9) are
configured in the output mode. This means these pins can send electrical
signals to control the LEDs.
2. Main Control Loop:
o The loop() function repeats continuously while the Arduino is operating.
Within this loop, the five LEDs are controlled sequentially.
o For each LED, the corresponding digital pin is set to the “HIGH” state (done
with the digitalWrite(pin, HIGH) command), allowing current to flow through
the pin and thus lighting the LED. Then, the program pauses for one second
(delay(1000)).
o Next, the same digital pin is set to the “LOW” state, stopping the current on the
pin and turning off the LED. Then, the program pauses again for one second
before the next LED lights up.
o This process returns to the beginning after all LEDs have blinked in sequence.
This scenario is often used to provide a visual display, create a sequencing indicator, or create
a simple animation effect. The sequential blinking of the LEDs can attract user attention or
visualize the progression of a certain status or process.

19
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Program Starts:
o When the Arduino is powered or reset, the program begins.
2. Setup Function Executes:
o The void setup() function is executed. This function runs only once and is
generally used to set the pin modes.
o With the pinMode() functions, pins number 13, 12, 11, 10, and 9 are set to
output mode. This means we can send electrical signals to the LEDs through
these pins.
3. Main Loop (loop function) Starts:
o The void loop() function starts. This function continues repeating as long as the
Arduino is operating.
4. First LED Turns On:
o digitalWrite(13, HIGH);: This command causes pin number 13 to receive a
“HIGH” voltage. In this case, when the pin is “HIGH,” the connected LED
lights up (i.e., it receives electrical current and emits light).
5. Wait for One Second:
o delay(1000);: The program waits here for 1000 milliseconds (i.e., 1 second).
During this time, the first LED remains lit because the voltage of the pin has
not yet been lowered.
6. First LED Turns Off:
o digitalWrite(13, LOW);: This command sets the voltage of pin number 13 to
“LOW”. Consequently, the first LED turns off as it no longer receives power.
7. Wait for Another Second:
o delay(1000);: The program waits again for 1000 milliseconds (1 second).
During this period, the first LED is off because the pin is in a “LOW” state.
8. Second LED Turns On:
o digitalWrite(12, HIGH);: Now, pin number 12 is set to “HIGH,” turning on the
second LED connected to it.
9. Wait for One Second:
o delay(1000);: The program waits for 1000 milliseconds (1 second). During this
time, the second LED remains on.
10. Second LED Turns Off:
o digitalWrite(12, LOW);: This command sets pin number 12’s voltage to
"LOW," turning off the second LED as it no longer receives power.

20
Microcontrollers for Steam-AI Students Kamil Bala

11. Wait for Another Second:


o delay(1000);: The program waits again for 1000 milliseconds (1 second).
During this time, the second LED is off.
12. Similar Steps for Third, Fourth, and Fifth LEDs:
o Similar steps are repeated for pins number 11, 10, and 9: a "HIGH" command
is given for each pin, wait for one second, give a "LOW" command, and wait
for another second. This ensures each LED blinks in sequence.
13. Repeating the Cycle:
o All these steps (from 4 to 12) are continuously repeated, making the LEDs blin

21
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. Energy Efficiency: How can you optimize energy consumption for this application,
and what are the effects of these optimizations on the overall circuit operation and
performance?
2. User Interaction: How can this system be expanded or modified so that users can
change the LED blinking speed or pattern in real-time?
3. Debugging Strategies: If the LEDs do not work as expected, what debugging
strategies would you apply to diagnose and solve this problem?
4. Expandability: What additional features or components could be added to make this
basic blinking circuit more functional or impressive?
5. Programming Alternatives: What could be the alternative programming approaches
or code structures for controlling the sequential blinking of LEDs?
6. Real-World Applications: What could be the practical applications of this sequential
LED blinking circuit? How could it be used in different sectors or situations?
7. Design Modifications: If you want to integrate this circuit into a product, what design
changes would you need to make, and what would be the effects of these changes on
the product?
8. Safety Requirements: What measures should you take to operate the circuit safely?
What should a safe design look like, especially for children or animals?
9. Data Collection and Monitoring: How could this system be expanded with the
ability to collect and monitor performance data? How could these data be used, and
what value would gathering this information add to the project?
10. Customization and Personalization: What features or options could be offered to
allow users to customize the circuit according to their needs or preferences?

22
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Energy Efficiency:
o To reduce energy consumption, methods such as lowering the brightness of
LEDs, optimizing standby times, or putting the microcontroller into sleep
mode can be used. However, these changes may affect the visibility or
response time of the circuit.
2. User Interaction:
o To allow users to change the blinking pattern of LEDs, physical input elements
like potentiometers or buttons, or digital tools such as wireless control via a
mobile app can be added. This makes the circuit dynamic and interactive.
3. Debugging Strategies:
o During troubleshooting, a systematic approach should be followed: checking
electrical connections, reviewing the code, and testing LEDs and other
components individually.
4. Expandability:
o To expand the circuit, additional features such as sound effects, motion
sensors, or Wi-Fi / Bluetooth functionality can be added. This diversifies usage
scenarios and provides a richer user experience.
5. Programming Alternatives:
o Alternative programming approaches include using different timing functions,
event-based programming with interrupts, or more advanced logic structures
(e.g., finite state machines).
6. Real-World Applications:
o The sequential LED blinking circuit can be used in advertising billboards,
security alert systems, interactive art and entertainment products, or
educational materials. Each scenario may require the circuit to be adapted in a
specific way.
7. Design Modifications:
o Product integration may require various changes such as miniaturizing the
circuit, optimizing energy consumption, ensuring durability, and safety.
8. Safety Requirements:
o For a safe design, measures such as operating the circuit at low voltage, adding
short-circuit preventive protections, and a water and shock-resistant case
design can be taken.
9. Data Collection and Monitoring:
o Performance monitoring and data collection features can be provided by
adding sensors to the circuit and transferring the data to a central system or
cloud service. Collected data can be used for system performance analysis,
remote monitoring, or understanding user behaviors.

23
Microcontrollers for Steam-AI Students Kamil Bala

10. Customization and Personalization:


o Users can be offered options such as creating their blinking patterns,
personalizing colors, or setting themes for specific events through mobile apps
or built-in control mechanisms.

24
Microcontrollers for Steam-AI Students Kamil Bala

1.1.5. Sequential Blinking of Eight LEDs

https://www.tinkercad.com/things/10O6J2qeNKZ-115-sequential-blinking-of-eight-leds

/*************************************************************************
Program Name: Sequential Blinking of Eight LEDs

Purpose of the Program: To make eight LEDs blink in sequence

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/

void setup()
{
// Set pin 13 as output
pinMode(13, OUTPUT);
// Set pin 12 as output
pinMode(12, OUTPUT);
// Set pin 11 as output
pinMode(11, OUTPUT);
// Set pin 10 as output
pinMode(10, OUTPUT);
// Set pin 9 as output
pinMode(9, OUTPUT);
// Set pin 8 as output
pinMode(8, OUTPUT);
// Set pin 7 as output
pinMode(7, OUTPUT);
// Set pin 6 as output
pinMode(6, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH); // Make pin 13 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(13, LOW); // Make pin 13 logic LOW
delay(1000); // Delay of 1000 ms
digitalWrite(12, HIGH); // Make pin 12 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(12, LOW); // Make pin 12 logic LOW
delay(1000); // Delay of 1000 ms
digitalWrite(11, HIGH); // Make pin 11 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(11, LOW); // Make pin 11 logic LOW
delay(1000); // Delay of 1000 ms

25
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(10, HIGH); // Make pin 10 logic HIGH


delay(1000); // Delay of 1000 ms
digitalWrite(10, LOW); // Make pin 10 logic LOW
delay(1000); // Delay of 1000 ms
digitalWrite(9, HIGH); // Make pin 9 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(9, LOW); // Make pin 9 logic LOW
delay(1000); // Delay of 1000 ms
digitalWrite(8, HIGH); // Make pin 8 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(8, LOW); // Make pin 8 logic LOW
delay(1000); // Delay of 1000 ms
digitalWrite(7, HIGH); // Make pin 7 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(7, LOW); // Make pin 7 logic LOW
delay(1000); // Delay of 1000 ms
digitalWrite(6, HIGH); // Make pin 6 logic HIGH
delay(1000); // Delay of 1000 ms
digitalWrite(6, LOW); // Make pin 6 logic LOW
delay(1000); // Delay of 1000 ms
}

26
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program is designed to control a number of LEDs (in this case, eight LEDs) in
sequence. Each LED is turned on for a certain period and then turned off, so the LEDs blink
in order. This creates a kind of “sequential” or “stepped” blinking effect, often also known as
the “knight rider” effect (though in this case, the LEDs move only in one direction). Here’s a
general explanation of the program:
1. Initial Settings (setup function):
o At the start of the program, the setup() function runs. This happens only once,
along with the initialization of the Arduino.
o Within the function, the digital pins to be used (pins 13, 12, 11, 10, 9, 8, 7, and
6) are configured in output mode. This means these pins will send electrical
signals capable of controlling the LEDs.
2. Main Control Loop (loop function):
o The loop() function continuously repeats for as long as the Arduino is
operating. Within this function, the eight LEDs are controlled in sequence.
o For each LED, the corresponding digital pin is set to the “HIGH” state (this is
done with the digitalWrite(pin, HIGH) command), which allows current to
flow through the pin and thus turns on the LED. Then, the program is paused
for one second (delay(1000)).
o In the next step, the same digital pin is set to the “LOW” state, stopping the
flow of current and turning off the LED. Then, the program is paused again for
one second before the next LED is turned on.
o This process repeats from the beginning after all LEDs have blinked in
sequence. This scenario is often used to provide a visual display, create a
sequencing indicator, or produce a simple animation effect. The sequential
blinking of LEDs can attract the user’s attention or visualize the progression of
a certain state or process.

27
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Program Starts:
o When the Arduino receives power or is reset, the program begins execution.
2. Setup Function Executes:
o The void setup() function is executed. This function runs only once and is
typically used for setting pin modes.
o With pinMode() functions, pins 13, 12, 11, 10, 9, 8, 7, and 6 are set to output
mode. This means we can send electrical signals to the LEDs via these pins.
3. Main Loop (loop function) Starts Running:
o The void loop() function begins. This function continuously repeats for as long
as the Arduino is operating.
4. First LED Turns On:
o digitalWrite(13, HIGH);: This command ensures that a “HIGH” voltage is sent
to pin number 13. In this case, the LED connected to the pin turns on (i.e.,
receives electric current and emits light) when the pin is in the “HIGH” state.
5. Wait for One Second:
o delay(1000);: The program pauses here for 1000 milliseconds (i.e., 1 second).
During this time, the first LED remains on because the voltage on the pin has
not yet been lowered.
6. First LED Turns Off:
o digitalWrite(13, LOW);: This command sets the voltage of pin number 13 to
the “LOW” state. Thus, power is cut off to the first LED, and it turns off.
7. Wait for Another Second:
o delay(1000);: The program pauses again for 1000 milliseconds (i.e., 1 second).
During this time, the first LED is off because the pin is in the “LOW” state.
8. Second LED Turns On:
o digitalWrite(12, HIGH);: Now, pin number 12 is set to the “HIGH” state,
turning on the second LED connected to pin 12.
9. Wait for One Second:
o delay(1000);: The program pauses for 1000 milliseconds (1 second). During
this time, the second LED remains on.
10. Second LED Turns Off:
o `digitalWrite(12, LOW);`: This command sets the voltage on pin number 12 to
the "LOW" state. As a result, power is cut to the second LED, and it turns off.

28
Microcontrollers for Steam-AI Students Kamil Bala

11. Wait for Another Second:


o `delay(1000);`: The program pauses again for 1000 milliseconds (i.e., 1
second). During this time, the second LED is off.
12. Similar Steps for Other LEDs:
o Similar steps are repeated for pins 11, 10, 9, 8, 7, and 6: the "HIGH" command
is given for each pin, it waits for a second, the "LOW" command is given, and
it waits again for a second. This ensures each LED blinks in turn.
13. Repetition of the Loop:
o All these steps (from 4 to 12) are continuously repeated, so the LEDs blink in

29
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. Energy Consumption: What is the energy consumption of the LEDs used in this
project, and how might we reduce this consumption?
2. User Interface: How would it be to add an interface to this project that allows users to
change the blinking speed or pattern of the LEDs?
3. Debugging: If one or several of the LEDs are not lighting up, what steps would you
follow to diagnose and solve this issue?
4. System Optimization: What changes could be made to make this circuit more
effective or efficient?
5. Code Optimization: What are the ways to rewrite the existing code more effectively
so that the same functionality can be provided with less code or higher performance?
6. Scalability: By adding which components or features can you make this basic circuit
more complex and multifunctional?
7. Physical Layout: How can you improve the physical connections and arrangement in
your current circuit layout?
8. Safety Measures: What precautions should be taken to operate the circuit safely?
What should a safe design look like, especially for children or animals?
9. Data Collection: How could this system be expanded with the ability to collect and
analyze usage data, and how could this information be utilized?
10. Environmental Impact: What is the environmental impact of such a circuit, and what
improvements could be made in line with sustainability goals?

30
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Energy Consumption:
o LEDs generally consume low energy. However, to further reduce
consumption, you can use more efficient LEDs, lower their brightness, or
adjust the code to turn them off when they are not active.
2. User Interface:
o To give users control, you could add potentiometers or buttons. Also, control
can be provided by using wireless technologies like Bluetooth through a
mobile app.
3. Debugging:
o If the LED is not working, first check the physical connections and soldering
points. Then, it should be tested to make sure the LED is correctly oriented
(anode and cathode) and in working condition. Finally, a review of the code is
needed for any logic or timing errors within.
4. System Optimization:
o To optimize the system, you can improve transitions between blinking LEDs,
adjust delay times, or even change the frequency and duration of the LEDs
blinking. Also, changes at the hardware or software level could be made to
reduce energy consumption.
5. Code Optimization:
o To optimize the code, you can put repeating commands into a loop, remove
unnecessary variables and delays, or make the code modular using functions.
This facilitates the readability and maintenance of the code.
6. Scalability:
o To expand the project, you could add sound effects, different light patterns,
motion sensors, or interactive control mechanisms. Also, remote control and
monitoring can be provided by adding IoT capabilities.
7. Physical Layout:
o To improve the physical layout, you can optimize the arrangement of circuit
elements, organize cables, and make the circuit board more compact or
aesthetic.
8. Safety Measures:
o To ensure safety, you can build a suitable case for the circuit, ensure electrical
parts are insulated, and protect the circuit against water and dust. Also, you can
ensure safety by operating the circuit at low voltage, even when in direct
contact.

31
Microcontrollers for Steam-AI Students Kamil Bala

9. Data Collection:
o To add data collection capabilities, you can use sensors and collect this data in
a central database. Collected data can be used for performance analysis, usage
statistics, or diagnosing errors.
10. Environmental Impact:
o To reduce environmental impacts, you can use sustainable and eco-friendly
materials, minimize energy consumption, and ensure the circuit is designed to
be long-lasting. Also, attention can be paid to making the circuit recyclable at
the end of its life.

32
Microcontrollers for Steam-AI Students Kamil Bala

1.2.Led Patterns and Output Multiplexing

1.2.1. Giving the Desired Value Using a Variable


https://www.tinkercad.com/things/51kwDTEuCOj-121giving-the-desired-value-using-a-
variable

/*************************************************************************
Program Name: Giving the Desired Value Using a Variable

Objective of the Program: To easily change the delay we repeat in 16 lines


using a variable to the desired value.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=500; // We define the Time variable that sets the delay.
void setup()
{
// We set pin 13 as output
pinMode(13, OUTPUT);

// We set pin 12 as output


pinMode(12, OUTPUT);

// We set pin 11 as output


pinMode(11, OUTPUT);

// We set pin 10 as output


pinMode(10, OUTPUT);

// We set pin 9 as output


pinMode(9, OUTPUT);

// We set pin 8 as output


pinMode(8, OUTPUT);

// We set pin 7 as output


pinMode(7, OUTPUT);

// We set pin 6 as output


pinMode(6, OUTPUT);
}

void loop()
{
digitalWrite(13, HIGH); // We make pin 13 logically HIGH
delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(13, LOW); // We make pin 13 logically LOW

33
Microcontrollers for Steam-AI Students Kamil Bala

delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(12, HIGH); // We make pin 12 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(12, LOW); // We make pin 12 logically LOW


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(11, HIGH); // We make pin 11 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(11, LOW); // We make pin 11 logically LOW


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(10, HIGH); // We make pin 10 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(10, LOW); // We make pin 10 logically LOW


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(9, HIGH); // We make pin 9 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(9, LOW); // We make pin 9 logically LOW


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(8, HIGH); // We make pin 8 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(8, LOW); // We make pin 8 logically LOW


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(7, HIGH); // We make pin 7 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(7, LOW); // We make pin 7 logically LOW


delay(iTime); // We provide a delay for the duration of iTime

digitalWrite(6, HIGH); // We make pin 6 logically HIGH


delay(iTime); // We provide a delay for the duration of iTime
digitalWrite(6, LOW); // We make pin 6 logically LOW
delay(iTime); // We provide a delay for the duration of iTime
}

34
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino code is a program that sequentially turns a series of LEDs on and off with a
specific delay duration. In the program, a variable is used to control the blinking duration of
the LEDs. This approach enhances the flexibility of the code because the blinking duration is
not hard-coded along with the rest of the code; instead, it is taken from a variable defined at
the beginning of the program. Here’s a general description of the program:

1. Variable Definition:

o At the start of the program, an integer variable is defined with the statement int
iTime = 500. This variable represents the delay duration (in milliseconds)
between each on and off of the LEDs. In this example, the delay is set to 500
milliseconds.

2. Setup Function:

o The setup() function runs once during the Arduino’s initialization. This
function is used to initialize various digital pins in the output mode, allowing
electrical signals to be sent to the LEDs via these pins.

3. Main Loop - loop Function:

o The loop() function runs repeatedly as long as the Arduino is operating. Within
this cycle, various digital pins are set to “HIGH” (high) and “LOW” (low)
states, turning the connected LEDs on and off in sequence.
o For each LED, the relevant digital pin is set to the “HIGH” state, causing the
LED to light up. Then, there’s a wait for the duration specified in the iTime
variable. During this time, the relevant LED stays on.
o Next, the same digital pin is set to the “LOW” state, causing the LED to turn
off. Again, there’s a wait for the duration specified in the iTime variable.
During this period, the relevant LED remains off before the next LED lights
up. About Variables: Variables are symbolic names used for storing data
within programming. By using variables, you can store values that can be
referenced or altered later in your code. This makes your code more flexible
and reusable, as you are working with variables instead of hard-coded values.
In this particular case, the iTime variable controls the blinking duration of the
LEDs. By modifying this part of the program, you can easily adjust the
blinking duration of all LEDs in a single step. This is particularly useful when
you want to experiment with different blinking durations or wish to control this
aspect of the program dynamically.

35
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:

o A variable named iTime is defined with the command int iTime = 500, and this
variable is assigned the value of 500. This variable represents the delay
duration (in milliseconds) that will be used throughout the program.

2. Setup Function:

o The void setup() function is defined. This function is executed only once when
the Arduino board is initialized.
o The pinMode() functions set the pins to output mode. This process is necessary
for controlling the LEDs because electrical signals will be sent through these
pins.

3. Main Loop - loop Function:

o The void loop() function initiates a loop that continually operates as long as the
Arduino is working.
o In the first step, the command digitalWrite(13, HIGH) sets pin number 13 to a
“HIGH” voltage, turning the LED on.
o With the command delay(iTime), the program waits for the duration specified
in the iTime variable (in this case, 500 ms).
o The command digitalWrite(13, LOW) sets the voltage of pin number 13 to the
“LOW” state, turning the LED off.
o The program then waits again with the delay(iTime) command. During this
time, no LED is on because it’s the period that elapses until a new “HIGH”
command is issued. The above steps are repeated for all other pins and thus for
all other LEDs: 12, 11, 10, 9, 8, 7, and 6. For each LED, the cycle begins when
the relevant pin is made “HIGH” and ends when it is made “LOW”. Each time,
the duration between the lighting and extinguishing of the LED is equal to the
duration determined by the iTime variable.

4. Continuation of the Loop:

o After all LEDs have been turned on and off in sequence, the end of the loop()
function is reached. However, since the loop() function is a cycle, it returns to
the beginning when the end of the code is reached, and the process starts over.

36
Microcontrollers for Steam-AI Students Kamil Bala

5. Continuous Repetition:

o This process continues until the power to the Arduino is cut. This allows the
LEDs to keep blinking in a specific sequence continuously. This structure
ensures that the program operates in a certain order and at specific time
intervals. The iTime variable provides consistency between different parts of
the program because all delays are dependent on the same variable. By
changing the value of this variable, you can easily adjust the speed of the
LEDs’ blinking.

37
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. The Importance of Variables: What are the advantages of using variables in this
program, and what kind of flexibility does it provide compared to using a fixed value
in the code?
2. Optimization Strategies: What code optimization techniques can be applied to
improve the program’s operational efficiency?
3. Debugging Scenarios: What debugging methods would you apply when a fault
occurs in the program, and what preventive strategies would you plan to prevent
potential errors?
4. User Interaction: If you wanted to add a user interface to the program, what features
would you integrate, and what parameters would you allow users to control?
5. Physical Design Improvements: How could you improve the current circuit layout,
and how would these improvements contribute to the overall system performance?
6. Power Management: What strategies could you apply to reduce the circuit’s energy
consumption, and what would be the effects of this approach on the overall project?
7. Scalability: How could you expand this project for a larger-scale application, and
what challenges might you encounter during this expansion?
8. Security Measures: What steps would you take to ensure security concerning the
circuit and code, and what potential risks would you consider in this process?
9. Educational Use: How could you use this project as an educational tool, and what
fundamental concepts and skills would you aim to teach students?
10. Social and Environmental Impacts: What criteria would you use to assess the
project’s social and environmental impacts, and how could you maximize these
impacts in a positive direction?

38
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The Importance of Variables: Variables make the code more flexible and reusable.
Using a variable instead of a fixed value, for instance, allows for easy adjustment of
parameters, such as dynamically changing the timing of blinking LEDs. This means
the code can be easily customized for different situations or requirements.
2. Optimization Strategies: Code optimization could involve removing unnecessary
delays, redundant commands or functions, and simplifying the overall structure of the
code. Also, improving algorithms for more efficient data structures and streamlining
processes is important.
3. Debugging Scenarios: Debugging is typically done using the serial monitor, so we
can see what’s happening while the program is running. Preventing potential errors
might include systematic testing, code review, and adopting good programming
practices.
4. User Interaction: Physical elements such as potentiometers, buttons, or touchscreens
could be added to the user interface. This allows users to dynamically change LEDs’
brightness, blink rate, or colors.
5. Physical Design Improvements: Improving the circuit layout could involve steps like
cable management, organizing circuit components, reducing the circuit board’s size, or
designing a case for the circuit. These improvements could enhance system
performance and reliability.
6. Power Management: To reduce energy consumption, you could use low-power
modes, decrease the LEDs’ brightness, or put the system to sleep at intervals. This
extends battery life and can reduce energy costs.
7. Scalability: Expanding the project could involve adding more LEDs or different types
of sensors, as well as integrating more complex control mechanisms or wireless
connectivity capabilities. During expansion, you might encounter challenges with
resource management, power consumption, and data communication.
8. Security Measures: Security could include using low voltage, short-circuit protection
safeguards, and an appropriate case design. Also, adding error management and
monitoring functions in the code can secure the system in case of potential faults.
9. Educational Use: This project could be used to teach students fundamental
electronics, coding, logic building, and problem-solving skills. It could also be utilized
as part of team work and project-based learning activities.
10. Social and Environmental Impacts: Making the project environmentally friendly
could involve using materials from sustainable sources, minimizing energy
consumption, and implementing recycling strategies at the project’s end. Social impact
could be achieved by encouraging community involvement or through educational
use.

39
Microcontrollers for Steam-AI Students Kamil Bala

1.2.2. Simplifying Tasks Using Functions (6-13)


https://www.tinkercad.com/things/h8gCSZDJQns-122-simplifying-tasks-using-functions-6-13

/*************************************************************************
Program Name: Simplifying Tasks Using Functions (6-13)

Purpose of the Program: To simplify the operations we repeatedly perform in


the code by using functions.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/

int iTime=100; // The variable iTime defining the delay

void setup()
{
// We execute the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
// We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
// We execute the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
digitalWrite(iPin, HIGH); // We make the current iPin value
logically HIGH
delay(iTime); // We provide a delay for iTime duration
digitalWrite(iPin, LOW); // We make the current iPin value
logically LOW
delay(iTime); // We provide a delay for iTime duration
}
}

40
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:


The code utilizes the pins from 6 to 13 on the Arduino. LEDs connected on these pins light up
and go off in sequence with a set time interval.
Sections of the Code:
1. Variable Definition:
int zaman = 200;
This line defines a variable used to set the duration of the LEDs’ blinking. The
‘zaman’ variable is in milliseconds, meaning in this case, each LED lights up for 200
milliseconds and turns off for 200 milliseconds.
2. setup Function:
void setup() { for(int i=13; i>=6; i–) { pinMode(i, OUTPUT); } }
This function runs once at the start of the program. It sets the pins from 13 to 6 as
output, allowing signals to be sent through these pins.
3. loop Function:
void loop() { for(int i=6; i<=13; i++) {
digitalWrite(i, HIGH); delay(zaman);
digitalWrite(i, LOW); delay(zaman);
}}
This function runs continuously throughout the rest of the program. It performs a loop
over the pins from 6 to 13:
o The command digitalWrite(i, HIGH); lights up the LED on the current pin (sets it
to high state).
o The command delay(zaman); pauses for the defined period (in this case, 200
milliseconds).
o The command digitalWrite(i, LOW); turns off the LED on the current pin (sets it
to low state).
o It pauses again for the defined period. This loop continuously repeats across the
pins from 6 to 13, making the LEDs blink in sequence.
Conclusion:
This code can be used to sequentially blink LEDs connected to specific pins on the Arduino.
It could be handy in applications like a traffic light simulation or a sequential light show.

41
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

Setup Function:
for(int i=13;i>=6; i–) { pinMode(i, OUTPUT); }
1. First Step (i = 13):
o The variable ‘i’ is set to 13.
o The command pinMode(13, OUTPUT); is executed, setting pin number 13 to
output mode.
2. Second Step (i = 12):
o At the end of the loop, the i-- command decreases the value of ‘i’ by one, making
it 12.
o The command pinMode(12, OUTPUT); is executed, setting pin number 12 to
output mode.
3. Third Step (i = 11):
o The value of ‘i’ is decreased again, becoming 11.
o The command pinMode(11, OUTPUT); is executed, setting pin number 11 to
output mode.
This process continues until the value of ‘i’ equals 6. That is, pins numbered 10, 9, 8, 7, and 6
are also set to output mode in sequence.
As a result, this loop sets the pins from 13 to 6 to output mode, ready for signal transmission.
This means LEDs or other components can be connected and controlled on these pins.
Loop Function:
1. Start of the Loop: The loop starts with the variable ‘i’ set to 6, as specified in the line
for(int i=6; i<=13; i++).
2. Boundary Check (for i = 6): The loop checks whether the variable ‘i’ is equal to or
less than 13. Since ‘i’ is 6 in the first step, the condition is true, and the loop continues.
3. LED Lighting (for i = 6): The command digitalWrite(i, HIGH); sets the LED on pin
number 6 to the high state (lights it up).
4. Wait (for i = 6): The command delay(zaman); waits for the duration specified in the
‘zaman’ variable. As ‘zaman’ was set to 200 earlier, it waits for 200 milliseconds.
5. LED Turning Off (for i = 6): The command digitalWrite(i, LOW); sets the LED on
pin number 6 to the low state (turns it off).
6. Another Wait (for i = 6): It waits again for 200 milliseconds.
7. Variable Increment: At the end of the loop, the i++ command increases the ‘i’
variable by one. After the first round, ‘i’ becomes 7.

42
Microcontrollers for Steam-AI Students Kamil Bala

8. Boundary Check (for i = 7): The loop now checks if ‘i’ is equal to or less than 13.
Since ‘i’ is now 7, the condition is true again, and the loop repeats the same steps for
pin number 7.
9. Continuation of the Loop: These steps repeat until the ‘i’ variable reaches 13. At
each step, the next LED on the subsequent pin lights up and turns off.
10. End of the Loop: When the ‘i’ variable becomes 14, the boundary check fails (the
condition i <= 13 is no longer valid), and the loop ends.
This loop is used to make the LEDs on the Arduino’s pins from 6 to 13 blink in sequence,
each for 200 milliseconds. In each step, the boundary check determines whether the loop will
continue or not.

43
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. Which pins should you connect the LEDs to when using this code?

2. To which leg of each LED should you connect a resistor? What is the purpose of the
resistor?

3. How do you determine the value of the resistor to be used in this circuit?

4. If any LED in the circuit does not light up, what steps should you follow, and what
should you check?

5. What is the maximum number of LEDs that can be used while working with this code,
and why?

6. What happens if an LED is connected in the wrong direction in the circuit?

7. What type of LEDs (for example, RGB LEDs) can you use when you want to create a
sequential light show using this code?

8. What changes can you make in the circuit for this code to create a more complex light
show?

9. If you want to change the brightness of the LEDs in the circuit, what changes would
you need to make in the code or circuit?

10. If you want more than one LED to light up at the same time in this circuit, what
changes should you make in the code?

44
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. You need to connect LEDs to Arduino pins 6 through 13.

2. Typically, a resistor is connected to the anode (long) leg of the LED. The resistor
limits the current passing through the circuit, thereby protecting the LED from
excessive current.

3. The value of the resistor is determined based on the type of LED used and the desired
brightness. A typical value might be 220 ohms, but different values may be used for
different LEDs.

4. If an LED does not light up, you should check whether the LED is connected
correctly, if the value of the resistor is appropriate, and if the connections are made
properly.

5. With this code, you can use the pins up to 13, so you can use a maximum of 8 LEDs.

6. If an LED is connected in the wrong direction, it will not light up. LEDs allow current
to pass in only one direction.

7. For creating a sequential light show using this code, you can use different types of
LEDs, such as regular or RGB LEDs. With RGB LEDs, you can also provide color
changes.

8. If you want to create a more complex light show, you could use different delay times,
play with different colors, or add more LEDs.

9. If you wish to change the brightness of the LEDs, you can use analog writing on PWM
(Pulse Width Modulation) compatible pins, meaning you can use the analogWrite
command.

10. If you want multiple LEDs to light up simultaneously, you can identify and set high
the pins you want to be in a high state at the same time. You can then add the delay
you want.

45
Microcontrollers for Steam-AI Students Kamil Bala

1.2.3. Sequentially turns on and off the correct LEDs from 13 to 6


https://www.tinkercad.com/things/kO5lxQYcamF-123-sequentially-turns-on-and-off-the-
correct-leds-from-13-to-6

/*************************************************************************
Program Name: Program that sequentially turns on and off the correct LEDs
from 13 to 6

Program Objective: To ensure the LEDs from 13 to 6 light up and


go out in descending order

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int time=200;

void setup()
{
for(int i=13; i>=6; i--)
{
pinMode(i, OUTPUT);
}
}

void loop()
{
for(int i=13; i>=6; i--)
{
digitalWrite(i, HIGH);
delay(time);
digitalWrite(i, LOW);
delay(time);
}
}

46
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This code controls the sequential turning on and off of a series of LED lights using specific
pins of the Arduino. We can understand how this code works with the following steps:
1. Setting Initial Values: The line ‘int time = 200;’ determines how long the LEDs will
flicker on and off. Here, the time is set to 200 milliseconds (0.2 seconds).
2. Setup Function: This part runs only once and sets up the initial settings of the
Arduino.
a. The loop ‘for(int i = 13; i >= 6; i–)’ selects the pins from 13 to 6 in order.
b. The command ‘pinMode(i, OUTPUT);’ sets the selected pin to output mode,
meaning electrical signals can be sent through these pins.
3. Loop Function: This part runs repeatedly in a loop.
a. The ‘for(int i = 13; i >= 6; i–)’ loop again selects the pins from 13 to 6 in order.
b. The ‘digitalWrite(i, HIGH);’ command sends an electrical signal through the
selected pin and lights the LED.
c. The ‘delay(time);’ command waits for the specified time (200 milliseconds). The
LED stays on during this time.
d. The ‘digitalWrite(i, LOW);’ command stops the electrical signal through the
selected pin and turns off the LED.
e. The ‘delay(time);’ command again waits for the specified time (200 milliseconds).
The LED is turned off during this time.
f. These steps are repeated for every pin in the loop.
As a result, this code creates an animation by sequentially turning the LEDs on for 0.2
seconds and off for 0.2 seconds, starting from pin number 13 to pin number 6. This process
repeats indefinitely.

47
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

Setup Function:
for(int i=13;i>=6; i–) { pinMode(i, OUTPUT); }
1. First Step (i = 13):
o The variable i is set to the value of 13.
o The command ‘pinMode(13, OUTPUT);’ is executed, setting pin number 13 to
output mode.
2. Second Step (i = 12):
o At the end of the loop, the ‘i–’ command decreases the value of i by one, making it
12.
o The command ‘pinMode(12, OUTPUT);’ is executed, setting pin number 12 to
output mode.
3. Third Step (i = 11):
o The value of i is decreased again, and it becomes 11.
o The command ‘pinMode(11, OUTPUT);’ is executed, setting pin number 11 to
output mode.
This process continues until the value of i equals 6. That is, pins 10, 9, 8, 7, and 6 are also set
to output mode in sequence.
As a result, with this loop, the pins from 13 to 6 are set to output mode to be able to send
signals. This means that LEDs or other components can be connected to and controlled
through these pins.
Loop Function
for(int i=13;i>=6; i–) {
digitalWrite(i, HIGH); delay(time);
digitalWrite(i, LOW); delay(time);
}
1. First Step (i = 13):
o The variable i is set to 13.
o The comparison ‘i >= 6’ is made. Since 13 is greater than 6, the condition is
true. • The ‘digitalWrite(13, HIGH);’ command lights the LED on pin 13.
o The ‘delay(time);’ command waits for 200 milliseconds.
o The ‘digitalWrite(13, LOW);’ command turns off the LED on pin 13.
o The ‘delay(time);’ command again waits for 200 milliseconds.
o The ‘i–’ command decreases i by one, making it 12 now.

48
Microcontrollers for Steam-AI Students Kamil Bala

2. Second Step (i = 12):


o The comparison ‘i >= 6’ is made. Since 12 is greater than 6, the condition is
still true.
o The same operations are applied on pin 12: the LED is lit, it waits, the LED is
turned off, it waits again.
o The ‘i–’ command decreases i by one, making it 11 now.
o This process continues until the value of i equals 6.
3. Final Step (i = 6):
o When i equals 6, the comparison ‘i >= 6’ is still true.
o The same operations are applied on pin 6: the LED is lit, it waits, the LED is
turned off, it waits again.
o The ‘i–’ command decreases i by one, and it becomes 5.
4. Ending the Loop:
o Now the value of i is 5, so the comparison ‘i >= 6’ is false.
o At this point, the loop ends because the condition is no longer true.
As a result, this loop lights and turns off the LEDs on the pins from 13 to 6 in sequence, each
LED being on for 200 milliseconds and off for 200 milliseconds. In each step, it checks
whether i is 6 or greater, and if so, the LEDs on that pin are controlled within this loop.

49
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the purpose of the code? What function does it perform?

2. What does this code do in the setup function? Which pins are being operated on?

3. What is the logic behind the loops (for loop) used in this code? Between which values
does the variable ‘i’ range?

4. In the code’s loop function, what is the purpose of the digitalWrite(i, HIGH);
command?

5. What does the value of the ‘time’ variable signify? What happens if you change this
value?

6. What are the durations of the LEDs turning on and off?

7. What function does the pinMode(i, OUTPUT); command perform?

8. On which pins does the loop function of the code operate, and what type of operation
is being performed on these pins?

9. If you make the starting value of the ‘i’ variable 10, what kind of change occurs in
how this code operates?

10. What do you think about the real-life projects in which this code could be used? For
instance, could this code serve as a basis for a traffic light system?

50
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The code is used to sequentially turn on and off LEDs connected to pins ranging from
13 to 6.

2. The setup function sets the pins from 13 to 6 to output mode. This means signals can
be sent through these pins.

3. The loops start the ‘i’ variable at 13 and decrement it by 1 with each step, counting
back to 6. It ranges between these values.

4. The digitalWrite(i, HIGH); command turns on the LED on the pin numbered ‘i’ by
bringing it to a high state (i.e., on).

5. The ‘time’ variable specifies the duration the LEDs are on or off in milliseconds.
Changing this value alters the speed at which the LEDs turn on and off.

6. The LEDs turn on and off for 200 milliseconds each, as determined by the ‘time’
variable.

7. The pinMode(i, OUTPUT); command sets pin ‘i’ to output mode, allowing a signal to
be sent through that pin.

8. The loop function operates on pins from 13 to 6. It turns on and off the LED on each
pin sequentially.

9. If you set the starting value of the ‘i’ variable to 10, the operation occurs only on pins
from 10 to 6. There would be no operation on pins 13, 12, and 11.

10. This code could be used in any project where you want to control a series of LEDs in
sequence, such as a traffic light system. It can create an animation of lights turning on
and off in sequence.

51
Microcontrollers for Steam-AI Students Kamil Bala

1.2.4. Sequentially turns on and off LEDs (13-6, 6-13)

https://www.tinkercad.com/things/dXtVotXdLiT-124-sequentially-turns-on-and-off-
leds-13-6-6-13-

/*************************************************************************
Program Name: Program that sequentially turns on and off LEDs from 13 to 6,
then from 6 to 13

Program Objective: To enable the LEDs to blink in a decreasing order from


13 to 6, then in an increasing order from 6 to 13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/

int iTime=100; // Variable 'iTime' defining the delay

void setup()
{
//We execute the function by decrementing pins from 13 to 6
for(int iPin=13;iPin>5;iPin--)
{
//We set the current content of 'iPin' as the output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
//We execute the function by decrementing pins from 13 to 6

for(int iPin=13;iPin>5;iPin--)
{
digitalWrite(iPin, HIGH); //We set the current 'iPin' value to
logical HIGH
delay(iTime); // We provide a delay for 'iTime' duration
digitalWrite(iPin, LOW); //We set the current 'iPin' value to
logical LOW
delay(iTime); // We provide a delay for 'iTime' duration
}

//delay(1000); //Optional: We provide a 1000 ms (1 second) delay


//We execute the function by incrementing pins from 6 to 13

for(int iPin=6;iPin<14;iPin++)
{
digitalWrite(iPin, HIGH); //We set the current 'iPin' value to
logical HIGH

52
Microcontrollers for Steam-AI Students Kamil Bala

delay(iTime); // We provide a delay for 'iTime' duration


digitalWrite(iPin, LOW); //We set the current 'iPin' value to
logical LOW
delay(iTime); // We provide a delay for 'iTime' duration
}

//delay(1000); //Optional: We provide a 1000 ms (1 second) delay


}

53
Microcontrollers for Steam-AI Students Kamil Bala

Explanation of the Program in General:

This code blinks LEDs connected to Arduino pins from 6 to 13 in two different sequences.
The first loop lights the LEDs in sequence on pins from 6 to 13, while the second loop lights
them on pins from 13 to 6. Here is a step-by-step explanation of the code:
setup() Function
Setting Pin Modes: In the setup() function, pins from 13 to 6 are set to output mode. This is
necessary to control the LEDs connected to these pins.
loop() Function
o First Loop:
Sequence from 6 to 13: This loop lights and turns off the LEDs on pins from 6 to 13 in
sequence. Each LED lights up for 200 milliseconds and then turns off for 200
milliseconds.
o Second Loop:
Sequence from 13 to 6: This loop lights and turns off the LEDs on pins from 13 to 6 in
sequence. Again, each LED lights up for 200 milliseconds and then turns off for 200
milliseconds.
What Happens Overall?
This code sequentially blinks the LEDs on pins from 6 to 13, first from lower-numbered pins
to higher-numbered pins, then from higher-numbered pins to lower-numbered pins. This
creates a “back-and-forth” animation by creating a continuous up and down motion effect.
The code operates in an endless loop within the loop() function, causing the LEDs to repeat
this pattern continuously.

54
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

Setup Function:
for(int i = 13; i >= 6; i--) { pinMode(i, OUTPUT); }
1. First Step (i = 13):
o The variable i is set to 13.
o The command pinMode(13, OUTPUT) is executed, thus setting pin number 13
to output mode.
2. Second Step (i = 12):
o At the end of the loop, the i-- command decreases the value of i by one,
making i now 12.
o The command pinMode(12, OUTPUT) is executed, thus setting pin number 12
to output mode.
3. Third Step (i = 11):
o The value of i is decreased again by one, becoming 11.
o The command pinMode(11, OUTPUT) is executed, thus setting pin number 11
to output mode.
This process continues until the value of i is equal to 6. That is, pins numbered 10, 9, 8, 7, and
6 are also sequentially set to output mode.
As a result, with this loop, pins from 13 to 6 are set to output mode to be able to send signals.
This means that LEDs or other components can be connected and controlled on these pins.
Loop Function
for(int i = 6; i <= 13; i++) { digitalWrite(i, HIGH); delay(time); digitalWrite(i, LOW);
delay(time); }
1. Beginning of the Loop: The loop starts with the variable i as 6, as specified in the line
for(int i = 6; i <= 13; i++).
2. Boundary Check (for i = 6): The loop checks whether the variable i is equal to or
less than 13. Since i is 6 in the first step, the condition is true, and the loop continues.
3. Lighting the LED (for i = 6): The command digitalWrite(i, HIGH) brings the LED
on pin number 6 to a high state (lights up).
4. Wait (for i = 6): The delay(time) command waits for the duration specified in the
time variable. Since the time was set to 200 in the previous code snippet, it waits for
200 milliseconds.
5. Turning Off the LED (for i = 6): The command digitalWrite(i, LOW) brings the
LED on pin number 6 to a low state (turns off).
6. Wait Again (for i = 6): Again, it waits for 200 milliseconds.

55
Microcontrollers for Steam-AI Students Kamil Bala

7. Incrementing the Variable: At the end of the loop, the i++ command increases the i
variable by one. After the first round, i becomes 7.
8. Boundary Check (for i = 7): The loop now checks if i is equal to or less than 13.
Since i is currently 7, the condition is again true, and the loop repeats the same steps
for pin number 7.
9. Continuation of the Loop: These steps are repeated until the i variable becomes 13.
At each step, the next LED on the subsequent pin is lit and turned off.
10. End of the Loop: When the i variable becomes 14, the boundary check fails (the
condition i <= 13 is no longer valid), and the loop ends.
This loop is used to sequentially light up and turn off LEDs on the Arduino pins from 6 to 13
for 200 milliseconds each. At each step, the boundary check determines whether the loop will
continue or not.
Second for loop:
for(int i = 13; i >= 6; i--) { digitalWrite(i, HIGH); delay(time); digitalWrite(i, LOW);
delay(time); }
1. First Step (i = 13):
o The variable i is set to 13.
o The comparison i >= 6 is made. Since 13 is greater than 6, the condition is
true.
o The command digitalWrite(13, HIGH) lights up the LED on pin 13.
o The command delay(time) waits for 200 milliseconds.
o The command digitalWrite(13, LOW) turns off the LED on pin 13.
o Again, the command delay(time) waits for 200 milliseconds.
o The command i-- decreases the value of i by one, and i becomes 12.
2. Second Step (i = 12):
o The comparison i >= 6 is made. Since 12 is greater than 6, the condition is
again true.
o The same operations are applied to pin number 12: the LED is lit, it waits, the
LED is turned off, and it waits again.
o The command i-- decreases the value of i by one, and i becomes 11.
o This process continues until the value of i equals 6.
3. Final Step (i = 6):
o When the value of i is equal to 6, the comparison i >= 6 is still true.
o The same operations are applied to pin number 6: the LED is lit, it waits, the
LED is turned off, and it waits again.
o The command i-- decreases the value of i by one, and i becomes 5.

56
Microcontrollers for Steam-AI Students Kamil Bala

4. Termination of the Loop:


o The value of i is now 5, so the comparison i >= 6 becomes false.
o At this point, the loop ends because the condition is no longer true.
As a result, this loop sequentially lights up and turns off the LEDs on pins from 13 to 6, each
LED lighting up for 200 milliseconds and turning off for 200 milliseconds. At each step, it
checks whether the i value is 6 or greater, and if so, the LEDs on that pin are controlled within
this loop.

57
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. How many LEDs are needed to run this code?


2. Which pins should the LEDs be connected to?
3. What is the required resistance value when connecting the LEDs? How is this value
determined?
4. How should the anode (long leg) and cathode (short leg) ends of the LEDs be
connected?
5. In which order do the LEDs light up and turn off in the first cycle? What about the
second cycle?
6. If you want to change the duration of the LEDs’ lighting, which line of code should
you change?
7. What should you do if you want to stop the first cycle and only run the second cycle?
8. How many different pin modes does this code set? Which lines of code do this?
9. If some LEDs do not light up, what kinds of problems could there be, and how do you
fix them?
10. What kind of changes can you make if you want to make the LED animation created
with this code more complex?
11. What processes are performed in the setup part of the program?
12. How many cycles are there in the loop function, and what do these cycles do?
13. How is the burning and extinguishing duration of the LEDs controlled?
14. What do you need to change in the code if you want the LEDs to light up
simultaneously, not sequentially?
15. Why does the program run in an endless loop?
16. What do the HIGH and LOW commands used in the code mean?
17. What purpose does the pinMode function serve in this code?

58
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. 8 LEDs are needed.


2. The LEDs should be connected to pins 6 through 13.
3. The resistance value varies depending on the type of LED and the voltage used.
Typically, a 220-ohm or 330-ohm resistor is used. The resistance value is determined
based on the LED’s nominal current and the operating voltage of the Arduino (usually
5V).
4. The anode (long leg) of the LEDs should be connected to the Arduino’s pins, and the
cathode (short leg) should be connected to the ground (GND). The resistor should also
be included in this circuit.
5. In the first cycle, LEDs light up and turn off in sequence from 6 to 13. In the second
cycle, they light up and turn off in sequence from 13 to 6.
6. If you want to change the lighting duration of the LEDs, you should change the value
of the time variable. This is specified at the beginning of the code as int time=200.
7. If you want to stop the first cycle and only run the second cycle, you can comment out
or delete the first for loop.
8. This code sets 8 different pin modes. This is done with the for loop inside the setup()
function.
9. If some LEDs do not light up, there could be connection issues, incorrect pin
connections, damaged LEDs, or incorrect resistance values. You can fix these by
checking the connections.
10. If you want to make the LED animation more complex, you can change the order in
the cycles, add different delay times, or create different animation effects by adding
new cycles.
11. In the setup part, the pins from 13 to 6 are set to output mode (OUTPUT). This is
necessary for controlling the LEDs through these pins.
12. There are two cycles in the loop function. The first cycle lights up the LEDs on pins 6
to 13 in sequence, and the second cycle lights up the LEDs on pins 13 to 6 in
sequence.
13. The duration of the LEDs’ lighting and turning off is controlled with the time variable.
The value of this variable is 200 milliseconds.
14. If you want the LEDs to light up simultaneously, you need to remove the delay
commands within both for loops and add a new loop to set all LEDs HIGH. Then,
after waiting for a certain period, add another loop to set all LEDs LOW.
15. The program runs in an endless loop because the loop function is called repeatedly.
This is a typical feature of Arduino programs.
16. The HIGH command used in the code sets the relevant pin to high voltage; the LOW
command sets it to low voltage. These are used for turning the LEDs on and off.
17. The pinMode function is used to designate a particular pin as input (INPUT) or output
(OUTPUT). In this code, it is used to set the pins to output mode to control the LEDs.

59
Microcontrollers for Steam-AI Students Kamil Bala

1.2.5. Sequential Lighting up of LEDs 6-13 and Turning Them All Off
Simultaneously
https://www.tinkercad.com/things/8D2j4EQek6J-125-sequential-lighting-up-of-leds-6-13-
and-turning-them-all-off

/*************************************************************************
Program Name: Sequential Lighting up of LEDs 6-13 and
Turning Them All Off Simultaneously

Objective of the Program: The purpose is to light up the LEDs in the


sequence below and then extinguish them all at once:
6
6 7
6 7 8
6 7 8 9
6 7 8 9 10
6 7 8 9 10 11
6 7 8 9 10 11 12
6 7 8 9 10 11 12 13

6
6 7
6 7 8
6 7 8 9
6 7 8 9 10
6 7 8 9 10 11
6 7 8 9 10 11 12
6 7 8 9 10 11 12 13

Author: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
**/
int iTime=500; // Variable 'iTime' defining the delay

void setup()
{
//Execute the function by incrementing pins from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
//We set the current content of 'iPin' as an output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
//Execute the function by incrementing pins from 6 to 13
// Light up all LEDs in order from 6 to 13
for(int iPin=6; iPin<14; iPin++)

60
Microcontrollers for Steam-AI Students Kamil Bala

{
digitalWrite(iPin, HIGH); //We set the current 'iPin' value to logical
HIGH
delay(iTime); // We provide a delay for 'iTime'
}
//Turn off again from 6 to 13
// Since there is no delay, it seems to turn off simultaneously
for(int iPin=6; iPin<14; iPin++)
{
digitalWrite(iPin, LOW); //We set the current 'iPin' value to logical
LOW
}
delay(iTime); // We provide a delay for 'iTime'
}

61
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program is designed to control a series of LEDs with a specific delay period.
The program switches the LEDs on and off in a particular sequence. The key concept here
involves control and automation done using the “for” loop. The general flow of the program is
as follows:
1. Variable Definition: At the start of the program, a delay time variable is defined with
the command int iTime = 500. This sets the wait time to be used between transitions
of the LEDs.
2. Setup Function: The setup() function is executed only once when the program starts.
Within this function, pins 6 through 13 are set as output. This is done using a “for”
loop, thereby eliminating the need to write a separate pinMode command for each pin.
The “for” loop is a programming command used for repeating certain actions a
specific number of times. In this case, the loop starts at 6 and iterates for each number
less than 14 (i.e., up to 13). In each iteration, the pinMode function is used to set the
current pin number (iPin) as output.
3. Main Loop - loop Function: The loop() function is the part continuously executed by
the Arduino. In this section, two “for” loops are used to control the LEDs. The first
loop sequentially switches on each LED. The digitalWrite command sends a “HIGH”
signal for each pin, causing the LED to light up. Then, the delay(iTime) function
ensures a wait for a specific period before moving on to the next LED. The second
loop switches off all LEDs simultaneously. This is done by sending a “LOW” signal
for each pin. After all LEDs are off, the delay(iTime) function is called again, ensuring
the LEDs remain off for a specific period before the next cycle begins. This structure
demonstrates the efficient use of the “for” loop, a fundamental building block of
programming, particularly when multiple tasks need to be carried out automatically
and sequentially. The “for” loop makes processing each element of a set of tasks easy
and manageable.

62
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime = 500;: Defines an integer variable named “iTime” and assigns it a
value of 500. This will be used as the delay time in subsequent stages.
2. Setup Function:
o This function runs only once when the program is loaded, and the Arduino
receives power.
o for(int iPin = 6; iPin < 14; iPin++): Here, a loop is initiated for pin numbers
ranging from 6 to 13 (14 not included). The “iPin” variable takes the next pin
number with each iteration of the loop. o pinMode(iPin, OUTPUT);: In each
iteration, the pin specified by the “iPin” variable is set to output mode. This
signifies that these pins will be used for controlling the LEDs.
3. Loop Function:
o This function is the main continuous loop running on the Arduino.
o The first “for” loop (for(int iPin = 6; iPin < 14; iPin++)):
▪ Again, a loop is initiated for pins ranging from 6 to 13.
▪ digitalWrite(iPin, HIGH);: In each iteration, the pin specified by “iPin” is
set to high voltage (HIGH), causing the connected LED to light up.
▪ delay(iTime);: After each LED lights up, the program pauses for the time
specified in the “iTime” variable (in milliseconds). In this case, this
duration is 500 ms.
o The second “for” loop (with the same parameters):
▪ In this loop, the same pins are revisited, but this time the digitalWrite(iPin,
LOW); command sends a low voltage (LOW) for each pin, causing the
LEDs to switch off. There is no delay command, delay(iTime); in this loop,
so the LEDs turn off immediately.
o Finally, after all LEDs are off, the delay(iTime); function is called again, and
the program waits for another 500 ms before the next loop iteration starts. This
structure enables the program to switch the LEDs on and off in a specific
sequence. The use of the “for” loop keeps the code clean and manageable, as
there’s no need to write separate commands for each pin. Also, thanks to the
loop structure, the program can easily be expanded with more LEDs or adapted
to different sequencing or timing schemes.

63
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. Efficiency of Loops: What other types of loops could be used instead of the “For”
loop, and how would this affect the efficiency of the program?
2. Code Optimization: How can delay times in the program be optimized so that the
system can respond more quickly to real-time situations?
3. Debugging: If the LEDs do not blink as expected, how can you diagnose this situation
and solve the problem?
4. User Interaction: How can this system be expanded or modified so that it can be
controlled by users, for example, through a button?
5. Power Management: How can you minimize energy consumption in a continuously
operating system?
6. Functional Expansion: What functions or features can be added to the existing system
to make it more useful or entertaining?
7. Environmental Impact: How is the environmental footprint of such a project assessed
and minimized?
8. Usage in Education: How can you use this project as an educational tool, and what
concepts can it help students understand?
9. Social Impact: What could be the social impacts of such projects on communities, and
how can they be used to encourage community participation?
10. Alternative Use Scenarios: Apart from this basic circuit and code structure, what
alternative application areas can you think of, and what changes in the code would be
necessary for these scenarios?

64
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Efficiency of Loops: “While” or “do-while” loops could also be used. They allow the
loop to continue as long as a certain condition is met. The choice of loop type can
affect the readability of the program, reduce software errors, and affect performance in
certain cases.
2. Code Optimization: Optimizing delay times could perhaps be achieved by using a
timer and/or employing different threads or interrupts. This would allow the program
to perform other operations during the delay time, making the system more
responsive.
3. Debugging: If the LEDs do not light up, the first step could be to check the physical
connections of the circuit. Subsequently, tools like a serial monitor could be used to
check whether the code is functioning correctly. The debugging process involves a
systematic approach and the application of specific test scenarios.
4. User Interaction: To provide user interaction, you can add input elements like
buttons or potentiometers. For instance, a button could alter the blinking state of the
LEDs, or a potentiometer could adjust the blinking speed.
5. Power Management: To reduce energy consumption, you can use low-power modes,
decrease the brightness of the LEDs, or put the system to sleep at regular intervals.
This extends battery life and can reduce energy costs.
6. Functional Expansion: Features such as sound effects, different lighting modes (like
blinking, fading), or motion sensors can be added to the system. This can make the
project more interactive and enjoyable.
7. Environmental Impact: The environmental footprint can be assessed by considering
elements like the project’s energy consumption, the sustainability of the materials
used, and waste management. Reducing energy consumption, using materials from
sustainable sources, and implementing recycling strategies at the end of the project can
minimize environmental impact.
8. Usage in Education: This project can be used to teach students basic electronics,
coding, logic building, and problem-solving skills. It can also be used as part of team
work and project-based learning activities.
9. Social Impact: To encourage community participation, such projects can be shared
through local workshops, educational programs, or community-based technology
initiatives. This can increase access to technology and encourage education and
collaboration within the community.
10. Alternative Use Scenarios: This basic circuit and code can be employed in various
fields such as customized lighting, interactive art installations, educational tools, or
home automation. Each scenario would require customizations in the code and
hardware to meet specific user requirements and interactions.

65
Microcontrollers for Steam-AI Students Kamil Bala

1.2.6. 6-13 Sequentially lighting and then extinguishing LEDs 6-13

https://www.tinkercad.com/things/4WUXTkY4Mff-126sequentially-lighting-and-then-
extinguishing-leds-6-13

/**************************************************************************
***
Program Name: Sequentially lighting and then extinguishing LEDs 6-13

Purpose of the Program: We ensure the LEDs light up and then


sequentially turn off in the following order:

6
6 7
6 7 8
6 7 8 9
6 7 8 9 10
6 7 8 9 10 11
6 7 8 9 10 11 12
6 7 8 9 10 11 12 13
7 8 9 10 11 12 13
9 10 11 12 13
10 11 12 13
11 12 13
12 13
13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

66
Microcontrollers for Steam-AI Students Kamil Bala

***************************************************************************
**/
int iTime=500; // Variable iTime defines the delay
void setup()
{
// We operate the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
// We set the current content of iPin as an output pin
pinMode(iPin, OUTPUT);
}
}
void loop()
{
// We operate the function by increasing the pins from 6 to 13 by 1
// Sequentially lighting all LEDs from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{

digitalWrite(iPin, HIGH); // We make the current iPin value logically


HIGH
delay(iTime); // We provide a delay of iTime
}
// We extinguish them again from 6 to 13
// Because there's no delay, it seems they turn off simultaneously
for(int iPin=6; iPin<14; iPin++)
{
digitalWrite(iPin, LOW); // We make the current iPin value logically
LOW
delay(iTime); // We provide a delay of iTime
}
delay(iTime); // We provide a delay of iTime
}}

67
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

1. Variable Definition:

o int iTime = 500;: A variable named “iTime” is defined and assigned the value
of 500. This will be used as a delay duration in various parts of the program.

2. Setup Function:

o The setup() function, which will run when the program starts, makes the
necessary initial adjustments.
o for(int iPin = 6; iPin < 14; iPin++): A loop is initiated for pin numbers between
6 and 13. In this loop, each pin number is sequentially assigned to the “iPin”
variable.
o pinMode(iPin, OUTPUT);: Within the loop, every pin is set to output mode.
This means these pins will be used later to control the LEDs.

3. Loop Function:

o The loop() function is the part that Arduino continuously operates.


o The first “for” loop (for(int iPin = 6; iPin < 14; iPin++)):
▪ A loop is restarted for the same pin numbers.
▪ digitalWrite(iPin, HIGH);: In each iteration of the loop, the pin
represented by the “iPin” variable is set to high voltage (HIGH),
meaning the relevant LED is lit.
▪ delay(iTime);: After the LED is lit, the program waits for the duration
specified in the “iTime” variable (in this case, 500 milliseconds).
o The second “for” loop (with the same parameters):
▪ Another loop is initiated for the same pins.
▪ digitalWrite(iPin, LOW);: In each iteration, the relevant pin is set to
low voltage (LOW), meaning the relevant LED is turned off.
▪ delay(iTime);: After each LED is turned off, the program waits again
for the specified duration. This ensures the LEDs are turned off
sequentially in a manner visible to the eye. o delay(iTime);: After all
LEDs are turned off, an additional delay is applied before the next loop
iteration.
This program performs the process of sequentially lighting and extinguishing LEDs in a
specific order. The “For” loop ensures the code is cleaner and more manageable because this
process is automated using a loop instead of writing separate commands for each pin. Also,
this method enhances the expandability of the code; for example, adding more LEDs is quite
simple.

68
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime = 500;: Defines the delay duration (in milliseconds) to be used in the
program. This variable controls the transition time between LEDs.
2. Setup Function:
o void setup() {…}: This function runs once when the Arduino receives power.
It is used for making initial start-up settings.
o for(int iPin = 6; iPin < 14; iPin++) {…}: Initiates a loop over the digital pins
from 6 to 13. These pins will be set as outputs and used to control the LEDs.
o pinMode(iPin, OUTPUT);: Each pin within the loop is configured as an output.
This means electrical signals will be sent through these pins to control the
connected LEDs.
3. Loop Function: void loop() {…}: This function is continuously operated by the
Arduino after the setup() function completes.
o First for loop: Lighting the LEDs
 for(int iPin = 6; iPin < 14; iPin++) {…}: The same loop is used, this time to
light the LEDs sequentially.
 digitalWrite(iPin, HIGH);: The electrical signal for the current pin (iPin) is
raised to a high level (HIGH). This causes the connected LED to light up.
 delay(iTime);: The program is paused for the duration specified in the
“iTime” variable (500 ms). During this time, the current LED remains lit. o
Second for loop: Extinguishing the LEDs
o for(int iPin = 6; iPin < 14; iPin++) {…}: The same loop is reinitiated, this time
to extinguish the LEDs sequentially.
 digitalWrite(iPin, LOW);: The electrical signal for the current pin (iPin) is
reduced to a low level (LOW). This causes the connected LED to turn off.
 delay(iTime);: After each LED is turned off, the program is paused again for
the duration specified in the “iTime” variable. During this period, all LEDs in
the current iteration remain off.
o delay(iTime);: After all LEDs are extinguished and the last LED has also been
turned off, an additional delay is implemented before the next loop iteration.
This ensures that all LEDs remain off until the next cycle.
These steps form the basic operating principle of the program. The process of lighting and
extinguishing LEDs occurs automatically and continuously through these loops. This code
structure is a convenient method for creating LED animations and controlling digital pin
outputs.

69
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. Performance Optimization: How can delay times in the program be optimized, and
how does this optimization affect system performance?
2. Debugging Strategies: If the LEDs do not work as expected, how do you diagnose
this situation and solve the problem?
3. Adding User Interaction: What features could be added to this system to allow users
to control the blinking speed or pattern of the LEDs?
4. Energy Efficiency: What strategies could you implement to make this project more
energy-efficient?
5. Functional Expansion: What hardware or software features could you add to expand
the functionality of this basic LED on/off system?
6. Environmental Impact: What factors would you consider to assess the
environmental footprint of this electronic circuit, and what improvements could you
make for sustainability?
7. Use in Education: How could you use this project as an educational tool, and what
concepts would you want students to learn?
8. Social Impact: What could be the social impacts of such DIY (do-it-yourself)
electronic projects on society, and how could you maximize these effects?
9. Alternative Usage Scenarios: Using the foundation of this circuit and code, what
alternative projects could you design and implement?
10. User Testing and Feedback: Are you planning to conduct user tests on this system?
If so, how would you design the structure of these tests, and how would you use user
feedback in product development?

70
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Performance Optimization:
o Delay times can be optimized using processor timers and interrupts, which
could allow for more efficient use of processor resources. Additionally, multi-
threading or non-blocking code structure can also improve performance.
2. Debugging Strategies:
o Troubleshooting often starts with a simple visual inspection (such as checking
whether the LEDs are connected correctly). Next, voltage tests are conducted
on the circuit using a multimeter. For software errors, error messages can be
sent via the serial monitor.
3. Adding User Interaction:
o We could allow users to adjust the speed of the blinking LEDs by adding a
potentiometer or buttons. Wireless modules like Bluetooth can also be added,
providing the ability to control through a smartphone.
4. Energy Efficiency:
o More energy-efficient LEDs can be used, or the brightness of the blinking
LEDs can be reduced to lower energy consumption. Additionally, putting the
system into sleep mode when not in use can save energy.
5. Functional Expansion:
o By adding sound sensors, motion detectors, or other environmental sensors, the
LEDs can be made to respond to environmental stimuli. For example, a system
that synchronizes with music can be created.
6. Environmental Impact:
o When assessing the environmental footprint of the circuit, factors such as the
sustainability of the materials used, energy consumption, and waste
management should be considered. Strategies such as transitioning to
renewable energy sources or using recyclable materials can be implemented.
7. Use in Education:
o This project can be used to teach students basic electronics, programming, and
physical computing concepts. Additionally, skills such as problem-solving,
debugging, and creative thinking can also be encouraged through these
projects.
8. Social Impact:
o DIY projects can create educational resources for communities without access
to technology and increase interest in STEM education. Also, these kinds of
projects can encourage community building, collaboration, and a culture of
sharing.

71
Microcontrollers for Steam-AI Students Kamil Bala

9. Alternative Usage Scenarios:


o This basic circuit can be used in a variety of different projects, such as
customized lighting solutions, interactive art installations, or home automation
systems.
10. User Testing and Feedback:
o User tests can be conducted to understand how the system performs under real-
world conditions. Feedback can be collected through surveys, focus groups, or
usability tests, and this data can be used to improve future iterations of the
product or system.

72
Microcontrollers for Steam-AI Students Kamil Bala

1.2.7. Program that sequentially adds and removes LEDs 6-13

https://www.tinkercad.com/things/bDgS2C6YLu7-127-program-that-sequentially-adds-and-
removes-leds-6-13

/**************************************************************************
***
Program Name: Program that sequentially adds and removes LEDs 6-13

Program Purpose: We make the LEDs light up and turn off in the following
sequence:

6
6 7
6 7 8
6 7 8 9
6 7 8 9 10
6 7 8 9 10 11
6 7 8 9 10 11 12
6 7 8 9 10 11 12 13
6 7 8 9 10 11 12
6 7 8 9 10 11
6 7 8 9 10
6 7 8 9
6 7 8
6 7
6

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
**/
int iTime=300; // The variable 'iTime' defining the delay

void setup()
{
// We run the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
// We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
// We run the function by increasing the pins from 6 to 13 by 1
// We sequentially light up all LEDs from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{

73
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(iPin, HIGH); // We make the current value of iPin


logically HIGH
delay(iTime); // We provide a delay for 'iTime' duration
}

// We run the function by decreasing the pins from 13 to 6 by 1


// We turn off the LEDs from 13 to 6
for(int iPin=13; iPin>5; iPin--)
{
digitalWrite(iPin, LOW); // We make the current value of iPin logically
LOW
delay(iTime); // We provide a delay for 'iTime' duration
}
}

74
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino code controls a sequence of processes that turn LEDs on and off in order with a
specific delay. The program enables the control of a series of LEDs using loops and
conditional operations. Here is a general explanation of the code:
1. Variable Definition:
o int iTime = 500;: This line defines a variable that controls the blinking duration
between the LEDs in milliseconds. It determines how long each LED will
blink.
2. Setup Function:
o void setup() {…}: This function runs once with the initiation of Arduino and
sets the pins to their initial states.
o for(int iPin = 6; iPin < 14; iPin++) {…}: This loop sets the pins from 6 to 13 to
output mode. These pins are where the LEDs are connected, and this code
prepares them for control.
3. Main Loop (Loop) Function: o void loop() {…}: After running the setup() function,
Arduino continually operates the loop() function. This is the main program cycle, and
LED control is performed here.
o The first for loop (for(int iPin = 6; iPin < 14; iPin++)) turns on the LEDs
connected to pins 6 through 13 in order, with the specified delay time (500
ms).
o The second for loop (for(int iPin = 13; iPin > 5; iPin–)) turns off the LEDs, this
time in reverse order from 13 back to 6, with the same delay time. This
program turns the LEDs on and off in sequence based on the specified delay
time, creating a “running lights” effect. The duration between each LED
blinking is controlled by the iTime variable. By altering this variable’s value,
you can adjust how quickly the LEDs blink. This creates a visual effect and
can be used as a simple electronic project for learning purposes.

75
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime = 500;: At the start of the program, a variable named iTime is
defined, setting the delay duration (blink duration) between LEDs in
milliseconds. This is a global variable that dictates how long each LED will
blink.
2. Setup Function:
o void setup() {…}: This function runs once when the Arduino starts. It contains
commands that set the initial states of the hardware.
o for(int iPin = 6; iPin < 14; iPin++) {…}: This for loop iterates through pin
numbers starting from 6 up to 13. During each loop iteration, the command
pinMode(iPin, OUTPUT); sets the current pin, corresponding to the iPin
number, as output. This way, LEDs will be controllable via these pins.
3. Main Loop (Loop) Function: o void loop() {…}: After the setup() function, Arduino
continuously runs the loop() function. This is the heart of the program, where the
continuously repeating operations take place.
o The first for loop (for(int iPin = 6; iPin < 14; iPin++)) lights up the LEDs by
sequentially powering pins 6 through 13. The command digitalWrite(iPin,
HIGH); sends a high voltage (HIGH) to the current pin with the iPin value,
turning the LED on. Then it waits for the duration specified by delay(iTime);.
o The second for loop (for(int iPin = 13; iPin > 5; iPin–)) turns off the LEDs by
cutting power to the pins from 13 to 6. This loop works by decreasing the pin
numbers (backward from 13). The command digitalWrite(iPin, LOW); sends a
low voltage (LOW) to the current pin with the iPin value, turning the LED off.
Then again, it waits for the duration specified by delay(iTime);. These steps
are continuously repeated on the Arduino, meaning when the loop() function
ends and restarts, the LEDs light up and turn off in sequence again. This
process continues until the Arduino is turned off. This operation creates a
visual effect and can be used to teach users basic principles of electronics and
coding.

76
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. Review for Optimization: Is the current structure of the program the best in terms of
performance? Could there be a more effective approach, and if so, how could this be
achieved?
2. Debugging Process: If you notice a fault in the program, what steps would you follow
in your troubleshooting process?
3. User Interaction: If you wanted to add a user interaction feature to this program,
what features would you integrate and how would you facilitate this interaction?
4. Energy Efficiency: If you have concerns about the energy consumption of this circuit,
what strategies would you employ to increase energy efficiency?
5. Functional Expansion: If you wanted to expand the functionality of the program,
what additional features or functions would you consider adding?
6. Environmental Impact Analysis: If you have concerns about the environmental
footprint of this project, what would you consider in terms of sustainability and eco-
friendly practices?
7. Educational Perspective: If you were to use this project as educational material, what
fundamental concepts would you aim to teach students, and how would you do so?
8. Societal Impact: How do you assess the overall impact of such technological projects
on society? Specifically, what is the role of this project in terms of STEM education?
9. Usage Scenarios: How could you adapt this project for a different context or usage
scenario? What creative applications can you think of?
10. User Feedback: If you were planning to collect user feedback on this system, what
methods would you use, and how would you employ the data gathered?

77
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Review for Optimization:


o The current program is quite simple and straightforward; however, for larger
projects or when more complex functions are required, we could make the code
modular by using functions and perhaps classes to improve the code’s
effectiveness. Also, we can optimize processor usage by minimizing unnecessary
delays and operations.
2. Debugging Process:
o Debugging is often done by checking the outputs of variables and states using the
serial monitor, with step-by-step analysis of the code, and sometimes by
commenting on or modifying specific parts of the code.
3. User Interaction:
o For user interaction, we could add physical inputs like buttons or potentiometers,
or even digital interfaces such as Bluetooth control through a mobile application.
Users could change the brightness, blinking speed, or color of the LEDs via these
inputs.
4. Energy Efficiency:
o To increase energy efficiency, we can ensure that LEDs are on only when needed,
lower their brightness using PWM (Pulse Width Modulation), or have the LEDs
turn on only when activity is detected by a motion sensor.
5. Functional Expansion:
o To expand functionality, we can integrate additional sensors like sound sensors,
motion detectors, or light sensors. We could also add new features like different
blinking modes, color transitions, or synchronization with music.
6. Environmental Impact Analysis:
o For eco-friendly practices, we can use renewable energy sources like solar panels,
choose technologies that keep energy consumption low (e.g., low-energy LEDs),
or use recyclable materials.
7. Educational Perspective:
o For educational purposes, we can use this project to teach the use of basic
electronic components, coding logic, loops and conditions, and even concepts such
as physical computing and human-computer interaction.
8. Societal Impact:
o Technological projects encourage problem-solving, analytical thinking, and
creative innovation skills, especially among young people. In terms of STEM
education, these kinds of projects allow for the merging of theoretical knowledge
with practical, tangible results.

78
Microcontrollers for Steam-AI Students Kamil Bala

9. Usage Scenarios:
o This project can be adapted for different contexts, such as decorative lighting,
entertainment, artistic performances, security applications, or interactive
installations for educational purposes.
10. User Feedback:
o To collect user feedback, we can conduct surveys, interviews, or usability tests.
The data collected can be used to improve user experience, add new features, or
fix existing issues.
.

79
Microcontrollers for Steam-AI Students Kamil Bala

1.2.8. Program that sequentially collects LEDs 13-6 and turns them off all
at once
https://www.tinkercad.com/things/jlUcOuKebKS-128-13-6-sequentially-collects-leds-13-6-
and-turns-them-off-all

/**************************************************************************
***
Program Name: Program that sequentially collects LEDs 13-6
and turns them off all at once

Program Purpose: We are enabling the LEDs to light up and


turn off in the sequence below:

13
12 13
11 12 13
10 11 12 13
9 10 11 12 13
8 9 10 11 12 13
7 8 9 10 11 12 13
6 7 8 9 10 11 12 13

13
12 13
11 12 13
10 11 12 13
9 10 11 12 13
8 9 10 11 12 13
7 8 9 10 11 12 13
6 7 8 9 10 11 12 13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
**/
int iTime=500; //The variable iTime defining the delay

void setup()
{
//Running the function by increasing the pins from 6 to 13
for(int iPin=6;iPin<14;iPin++)
{
//We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}

void loop()
{

80
Microcontrollers for Steam-AI Students Kamil Bala

//Running the function by decreasing the pins from 13 to 6


//We sequentially light up all LEDs from 13 to 6
for(int iPin=13;iPin>5;iPin--)
{
digitalWrite(iPin, HIGH); //We make the current iPin value logically
HIGH
delay(iTime); // We provide a delay for iTime
}

//Running the function by decreasing the pins from 13 to 6


//We see it as turning off at the same time because there is no time
delay
for(int iPin=13;iPin>5;iPin--)
{
digitalWrite(iPin, LOW); //We make the current iPin value logically
LOW
}
delay(iTime); // We provide a delay for iTime

81
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

Of course, this Arduino code is designed to control a series of LEDs in a specific sequence.
The program creates a simple animation by sequentially lighting and extinguishing the LEDs
within a certain time delay range. Here is a general summary of the program:
1. Variable Definition:
o int iTime = 500;: This is a variable that specifies the delay time in milliseconds
between the LEDs. In this example, there is a half-second delay between the
lighting and extinguishing of each LED.
2. Setup Function:
o void setup() { … }: This function is the block of code that the Arduino runs
once at the start. In this case, it sets the pins from 6 to 13 to output mode.
These pins are where the LEDs are connected.
3. Main Loop Function:
o void loop() { … }: This function is the block of code that the Arduino
continually runs over and over. The main functionality of the program is here.
o The first for loop (decreasing from 13 to 6) lights each LED in the specified
order. Each LED lights up with a certain delay (iTime) after the previous LED
is turned off.
o The second for loop turns off all LEDs at the same time. This happens because
there is no delay command in the loop; therefore, the loop is executed
immediately, and all LEDs shut off almost simultaneously.
4. delay:
o The final delay(iTime); command provides a delay before the next loop
iteration begins. This creates a pause before the next “animation cycle” starts
after all LEDs have been turned off. This program allows for the control of a
series of LEDs and creates a simple light animation. It is often used in
instructional projects, interactive art or entertainment applications, or any
situation where visual feedback to the user is desired.

82
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime = 500;: At the start of the program, a variable that defines the delay time in
milliseconds between the LEDs is initiated. This controls how long each LED stays on
and when the next LED will light up.
2. Setup Function (setup):
o The setup part of the program runs only once and sets the pins to initial conditions.
a. The for loop scans the pins from 6 to 13.
b. pinMode(iPin, OUTPUT); is executed for each pin, setting these pins to output
mode. This means the LEDs connected to these pins can be controlled.
3. Main Loop Function (loop):
o The loop function is the part that continuously operates as long as the Arduino
is on. In this example, it controls the lighting and extinguishing behavior of the
LEDs. a. The first for loop:
o This loop counts back through the pins, starting from 13 down to 6 (not
inclusive).
o On each cycle, the digitalWrite(iPin, HIGH); command turns on the LED at the
current iPin number (making the logic level HIGH).
o delay(iTime); pauses the program for a specific time (defined by the iTime
variable) after each LED is turned on. In this case, it’s
a 500-millisecond or half-second delay.
b. The second for loop:
o This loop operates over the same range of pins (13 down to 6) but this time
turns the LEDs off.
o digitalWrite(iPin, LOW); is called for each LED, turning it off by making the
logic level LOW. No delay call is made in this loop, so the LEDs turn off
almost simultaneously.
4. Final Delay:
o After all the LEDs are off, the delay(iTime); command adds a delay before the
program moves to the next loop iteration. This sets the time between turning
off all LEDs and lighting the first LED in the next cycle. Each of these steps
forms a loop for the LEDs to light up and extinguish in sequence, creating a
“running light” effect. This process continues until the Arduino’s power source
is cut or another stopping command is given.

83
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. If you change the iTime variable, what effect does it have on the blinking speed of the
LEDs, and how might this affect the user experience?
2. In the current circuit, what changes would you need to make in the program code to
create a ripple or “running light” effect instead of the LEDs blinking simultaneously?
3. What sensors or additional components could be integrated to expand this project, and
how might these integrations enhance overall functionality or user experience?
4. How efficient is this code in terms of energy consumption? What improvements could
be made to reduce energy consumption in a continuously operating system?
5. What additional features or modifications are necessary if this system is to be used as
a security system?
6. If you want to connect more than one set of LEDs, how can you modify or expand this
code?
7. What should the debugging process of this program be like? What common problems
might you encounter, and how can you identify and resolve them?
8. The current code uses a for loop. If you want to use a while loop instead, what changes
would you need to make in the code, and how would this change affect the behavior of
the loop?
9. Can an information indicator of some sort be created using different colored LEDs in
this system? If so, what changes would this require in the code?
10. If you want to add wireless communication (e.g., via Bluetooth) to this project, what
components would you need to add, and how could you modify this code for wireless
control?

84
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. If you change the iTime variable, how does it affect the LEDs’ blinking speed, and
how does this affect the user experience?
o Answer: The iTime variable determines how long the LEDs stay on (or off).
As the value decreases, the blinking speed increases, creating a more dynamic
visual effect. However, too fast a blinking rate can be irritating to the human
eye or make it difficult to interpret the message.
2. What changes should be made to create a ripple or “running light” effect instead of the
LEDs blinking simultaneously?
o Answer: Each LED should be controlled independently, meaning the next
LED changes based on the previous one’s state. This is often achieved using
separate timers or loops for each LED.
3. What sensors or additional components can be added to expand this project?
o Answer: Various sensors can be added, such as motion sensors, sound sensors,
light sensors, temperature sensors, etc. These sensors can alter the behavior of
the LEDs in response to environmental changes.
4. How efficient is the code in terms of energy consumption? What can be done to
reduce energy consumption?
o Answer: Constant blinking of LEDs consumes energy. For energy savings,
you could integrate a sensor to ensure the LEDs are only on when necessary or
add periods of low power operation.
5. If this system were used as a security system, what features should be added?
o Answer: For security applications, features like motion detectors, camera
systems, automatic locking mechanisms, and an alarm system should be added.
Additionally, a network connection is crucial for recording events and
transmitting notifications.
6. If you want to add more LED sets, how can this code be expanded?
o Answer: To add more LED groups, control loops for each group and
additional codes for power management, if necessary, should be included.
Each group can behave differently in response to a specific situation or input.
7. What should the debugging process for this program be like?
o Answer: Debugging involves testing the code step-by-step, identifying
unexpected behaviors, and locating and fixing errors within the code. Using a
serial monitor is a good method to see real-time data.
8. If a while loop were used instead of a for loop, what would change?
o Answer: A while loop continues the loop as long as a certain condition is true.
Unlike a for loop, initializing and updating the loop control variable must be
done manually. This can provide more control within the loop but may lead to
infinite loops if used carelessly.
9. Can an information indicator be created using LEDs of different colors?

85
Microcontrollers for Steam-AI Students Kamil Bala

o Answer: Yes, using LEDs of different colors, you can create a visual indicator
for various statuses or alerts. Each color can represent a different situation. For
example, red could indicate danger or an emergency, while green could
indicate normal operating conditions.
10. What needs to be done to add wireless control?
o Answer: For wireless control, you need to add a module capable of wireless
communication, like a Bluetooth or Wi-Fi module. This allows for remote
control of the device. Relevant libraries and codes for data transmission and
reception should be included.

86
Microcontrollers for Steam-AI Students Kamil Bala

1.2.9. Program that sequentially lights up LEDs from 13-6 and extinguishes
them from 6-13

https://www.tinkercad.com/things/66Tcu9zq9Mx-129-sequentially-lights-up-leds-
from-13-6-and-extinguishes-them

/*************************************************************************
Program Name: Program that sequentially lights up LEDs from 13-6 and
extinguishes them from 6-13

Program Objective: We are ensuring that the LEDs light up and extinguish in
the following sequence:
13
12 13
11 12 13
10 11 12 13
9 10 11 12 13
8 9 10 11 12 13
7 8 9 10 11 12 13
6 7 8 9 10 11 12 13
6 7 8 9 10 11 12
6 7 8 9 10 11
6 7 8 9 10
6 7 8 9
6 7 8
6 7
6
Author: Kamil Bala
kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=500; // iTime variable defining the delay

void setup()
{
// We execute the function by increasing the pin numbers from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
// We assign the current content of iPin as an output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
// We execute the function by decreasing the pin numbers from 13 to 6
// We sequentially light up all LEDs from 13 to 6
for(int iPin=13; iPin>5; iPin--)
{

87
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(iPin, HIGH); // We set the current iPin value to logical


HIGH
delay(iTime); // We create a delay of iTime
}

// We execute the function by decreasing the pin numbers from 13 to 6


// Since there's no time delay, it appears to turn off simultaneously
for(int iPin=13; iPin>5; iPin--)
{
digitalWrite(iPin, LOW); // We set the current iPin value to logical
LOW
delay(iTime); // We create a delay of iTime
}
delay(iTime); // We create a delay of iTime

88
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program is designed to control a series of LEDs. The program creates a kind of
visual animation by turning the LEDs on and off in a specific sequence. Here is a general
summary of the program:
1. Variable Definition:
o iTime: At the start of the program, a variable is defined that determines how
long the LEDs will stay on or off. This is used to control the speed of the
blinking effect.
2. Setup Function:
o This function is a block of code that the Arduino runs at startup. Here, the
digital pins from 6 to 13 are initiated in output mode so that these pins can
power the LEDs.
3. Main Loop Function:
o This function is a block of code that the Arduino continuously repeats,
controlling the blinking of the LEDs.
o The first for loop lights up each LED in sequence, starting from pin number 13
and decreasing the pin number (from 13 to 6). Each LED remains on for the
duration specified in the iTime variable.
o The second for loop turns off each LED in sequence, starting from pin number
6 and increasing the pin number (from 6 to 13). In this case, each LED remains
off for the duration specified in the iTime variable.
This program simulates the “flow” of LEDs from one end to the other, i.e., it creates a wave
of light moving from one end to the other. This effect is often known as the “Knight Rider”
effect (after a popular TV show). The basic logic of the program is to turn the LEDs on and
off in sequence with a specific delay, thus creating a visual animation.

89
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o At the very start of the program, a variable named “iTime” is defined with the
code
int iTime = 500;
and assigned a value of 500. This represents a duration in milliseconds and
determines how long the LEDs will stay on or off.
2. Setup Function:
o The void setup() function is executed once immediately after the program is
loaded and every time the Arduino device is powered on.
o Within this function, digital pins from 6 to 13 are set as outputs using the
pinMode(iPin, OUTPUT); command. This means these pins can provide power
to control the LEDs.
3. Main Loop Function:
o The void loop() function is the part that continuously operates on the Arduino.
The codes written inside this function are executed repeatedly.
o First for loop (counting backward from 13): Using for(int iPin=13; iPin>5;
iPin–), LEDs from pin number 13 to pin number 6 are lit in sequence. Each
LED stays on for the time specified in the iTime variable. This process is
carried out with the digitalWrite(iPin, HIGH); and delay(iTime); commands.
o Second for loop (counting forward from 6): Using for(int iPin=6; iPin<14;
iPin++), LEDs from pin number 6 to pin number 13 are turned off in sequence.
Turning off the LEDs is done with the digitalWrite(iPin, LOW); command,
and after each LED is turned off, there is a wait for the duration specified with
the delay(iTime); command. Through these cycles, the LEDs blink in a
specific sequence, creating a moving light effect. This method can be used for
various visual displays and light animations.

90
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. With the program in its current state, how can you change the blinking duration of the
LEDs, and how would this change affect the visual effect?
2. If you wanted to add many more LEDs, what changes would you need to make in the
program code?
3. If the distances between the LEDs were different during the extinguishing process,
what kind of effect would this have on the visual animation?
4. If you want this animation to operate with a different sequence or pattern, what
changes would you need to make in the existing code?
5. What other programming structures could you use instead of the current loop
structures to create a similar LED animation?
6. If there were a bug in this program, what debugging techniques would you use to
identify it?
7. If you wanted to synchronize this LED array with music or sound, what kind of
sensors would you need, and how would you need to modify the Arduino code?
8. If you wanted to control this system wirelessly, what additional components would
you need to include in your project, and what changes would you need to make in the
code?
9. If you wanted to design a system that could change the colors of the LEDs, what type
of LEDs should you choose, and how should you adjust the program?
10. What strategies could you implement to reduce power consumption in the current
system, and how would these changes affect the overall performance of the system?

91
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Changing the blinking duration of the LEDs:


o You can adjust the blinking duration by changing the value of the iTime
variable. You can shorten or extend this period for a faster or slower visual
effect.
2. Adding more LEDs:
o To add more LEDs, you should define the new LED pins in the setup()
function and adapt the loop() function’s cycle accordingly.
3. Distance between LEDs:
o The physical distance between the LEDs can change the visual perception of
the animation. Closer LEDs may give the impression of a smoother motion.
4. Different sequence or pattern:
o For a different sequence or pattern, you would need to change the loop()
function’s cycle and the digitalWrite() commands. You can code a customized
sequence or blinking pattern.
5. Alternative programming structures:
o To create a similar animation, you could use while loops, if-else conditions, or
functions. Also, more advanced techniques like timer interrupts can be
employed.
6. Debugging techniques:
o In case of a bug, you can use the serial monitor to check outputs, physically
inspect the LED connections, or review your code to find logical errors.
7. Sound synchronization:
o For sound synchronization, you could use a sound sensor or microphone
module. You should add code snippets that read the data from the sensor and
control the LEDs accordingly.
8. Wireless control:
o For wireless control, you can use communication modules like Bluetooth or
Wi-Fi modules (e.g., HC-05/06 or ESP8266/ESP32). You will need to write
codes to exchange data with these modules.
9. Color-changing LEDs:
o For color changes, you should use RGB LEDs. You will need to make the
necessary adjustments in your code to control each color of these LEDs
separately.
10. Reducing power consumption:
o To reduce power consumption, you could lower the brightness of the LEDs
(using PWM) or put the Arduino into sleep mode when not in use. These

92
Microcontrollers for Steam-AI Students Kamil Bala

changes may reduce power consumption without directly affecting


performance.

93
Microcontrollers for Steam-AI Students Kamil Bala

1.2.10.Program accumulating and then subtracting LEDs in sequence 13-6

https://www.tinkercad.com/things/3qISFAnPVUW-1210accumulating-and-subtracting-leds-
in-sequence-13-6

/**************************************************************************
***
Program Name: Program accumulating and then subtracting LEDs in sequence
13-6

Program Objective: We ensure the LEDs light up and go out in the sequence
below:
13
12 13
11 12 13
10 11 12 13
9 10 11 12 13
8 9 10 11 12 13
7 8 9 10 11 12 13
6 7 8 9 10 11 12 13
7 8 9 10 11 12 13
8 9 10 11 12 13
9 10 11 12 13
10 11 12 13
11 12 13
12 13
13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=300; // The variable iTime defining the delay

void setup()
{
// We run the function by increasing the pins from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
// We set the current value of iPin as an output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
// We run the function by decreasing the pins from 13 to 6
// We sequentially light up all the LEDs from 13 to 6
for(int iPin=13; iPin>5; iPin--)
{

94
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(iPin, HIGH); // We make the current iPin value logically


HIGH
delay(iTime); // We provide a delay for iTime
}
// We run the function by increasing the pins from 6 to 13
// We turn off the LEDs from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
digitalWrite(iPin, LOW); // We make the current iPin value logically
LOW
delay(iTime); // We provide a delay for iTime
}
}

95
Microcontrollers for Steam-AI Students Kamil Bala

1.2.11. Program to add and subtract 6-13, 13-6 LEDs


https://www.tinkercad.com/things/gwIkFOMpXsR-1211-program-to-add-and-subtract-6-13-
13-6-leds

/**************************************************************************
***
Program Name: Program that lights up LEDs from 6-13, turns off LEDs from
13-6, then
lights up LEDs from 13-6, and turns off LEDs from 6-13.

Program Objective: We ensure the LEDs light up and go out in the sequence
below:
6
6 7
6 7 8
6 7 8 9
6 7 8 9 10
6 7 8 9 10 11
6 7 8 9 10 11 12
6 7 8 9 10 11 12 13
6 7 8 9 10 11 12
6 7 8 9 10 11
6 7 8 9 10
6 7 8 9
6 7 8
6 7
6

13
12 13
11 12 13
10 11 12 13
9 10 11 12 13
8 9 10 11 12 13
7 8 9 10 11 12 13
6 7 8 9 10 11 12 13
7 8 9 10 11 12 13
8 9 10 11 12 13
9 10 11 12 13
10 11 12 13
11 12 13
12 13
13
Written by: Kamil Bala
kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=200; // The variable iTime defining the delay

void setup()

96
Microcontrollers for Steam-AI Students Kamil Bala

{
// We run the function by increasing the pins from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
// We set the current value of iPin as an output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
// We run the function by increasing the pins from 6 to 13
// We sequentially light up all the LEDs from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
digitalWrite(iPin, HIGH); // We make the current iPin value logically
HIGH
delay(iTime); // We provide a delay for iTime
}
// We run the function by decreasing the pins from 13 to 6
// We turn off the LEDs from 13 to 6
for(int iPin=13; iPin>5; iPin--)
{
digitalWrite(iPin, LOW); // We make the current iPin value logically
LOW
delay(iTime); // We provide a delay for iTime
}
// Delay between two functions
delay(2*iTime); // We provide a delay for 2iTime

// We run the function by decreasing the pins from 13 to 6


// We sequentially light up all the LEDs from 13 to 6
for(int iPin=13; iPin>5; iPin--)
{
digitalWrite(iPin, HIGH); // We make the current iPin value logically
HIGH
delay(iTime); // We provide a delay for iTime
}
// We run the function by increasing the pins from 6 to 13
// We turn off the LEDs from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
digitalWrite(iPin, LOW); // We make the current iPin value logically
LOW
delay(iTime); // We provide a delay for iTime
}
}

97
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino code is written to blink a series of LEDs in a specific sequence. The program
facilitates the LEDs to light up and turn off in order, creating a kind of “running light” effect.
Here’s a broad explanation of the program:
1. Variable Definition:
o iTime: Specifies the delay time in milliseconds between the blinking actions.
In this instance, there is a 200-millisecond delay between each LED’s blinking.
2. Setup Function:
o This function contains the code that Arduino executes at the beginning.
o Pins from 6 to 13 are set to output mode. These pins are where the LEDs are
connected.
3. Main Loop (loop) Function:
o This function is continuously run by the Arduino in a loop. o Firstly, LEDs
connected to pins from 6 to 13 light up in sequence (an upward sequence).
Each LED lights up for the duration determined by the iTime variable.
o Then, LEDs from 13 to 6 are turned off in sequence (a downward sequence).
This also happens with the same delay. o Between the two cycles, there’s a
delay for 2*iTime, making the effect more noticeable.
o In the next phase, LEDs from 13 to 6 light up again in sequence but this time
in reverse order (a downward sequence).
o o Finally, LEDs from 6 to 13 are turned off in sequence (an upward sequence).
This process repeats endlessly within the loop() function, making the LEDs
blink in the set sequence. It forms a visually appealing LED animation display
for observers. The logic of the program revolves around controlling LEDs in a
specific pattern, providing a dynamic light show.

98
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

This program creates a “running light” effect by controlling LEDs in a specific sequence.
Here is a step-by-step explanation:
1. Variable Definition:
o int iTime = 200;: This is the variable used by the program to control the delay
between each LED action. The value is in milliseconds and determines how long each
LED will stay on (or off).
2. setup Function:
o for(int iPin = 6; iPin < 14; iPin++): This loop configures digital pins from 6 to 13.
These pins are set to output mode since they will control the LEDs connected to them.
3. loop Function: This section is the main cycle that controls the sequential blinking of
the LEDs and consists of several stages:
o First Stage: Lighting up the LEDs
▪ for(int iPin = 6; iPin < 14; iPin++): LEDs connected to pins from 6 to
13 are turned on in sequence according to the value of iPin. Each LED
stays lit for the duration of iTime.
o Second Stage: Turning off the LEDs
▪ for(int iPin = 13; iPin > 5; iPin–): This time, LEDs from 13 down to 6
are turned off in sequence. Each LED stays off for the duration of
iTime.
o Adding a Delay
▪ delay(2*iTime);: An additional delay is inserted between the two series
of actions to enhance the visual effect. This delay is double the
previous one.
o Third Stage: Lighting up the LEDs in Reverse Order
▪ for(int iPin = 13; iPin > 5; iPin–): LEDs from 13 to 6 are lit again, this
time in reverse sequence. Each LED stays lit for the duration of iTime.
o Fourth Stage: Turning off the LEDs in Reverse Order
▪ for(int iPin = 6; iPin < 14; iPin++): Lastly, LEDs from 6 to 13 are
turned off again in sequence. Each LED stays off for the duration of
iTime. Through these cycles and actions, the LEDs blink in a set
pattern, giving observers the impression of continuous motion. These
actions are continually repeated within the loop function, so the
animation goes on without stopping.

99
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the role of the iTime variable in the program, and how can the value of this
variable be adjusted in different scenarios?
2. If we set the iTime variable to a different value, what kind of change can we expect in
the blinking speed of the LEDs?
3. What types of loop structures are used in the program to make the LEDs turn on and
off in sequence, and how do these structures work?
4. In the current version of the program, how is the order of lighting and extinguishing of
LEDs determined? What changes could be made in the code to change this ordering?
5. If we wanted to control multiple LEDs simultaneously, what changes would we need
to make in the program?
6. What is the purpose of the processes performed in the setup part of the program, and
what other processes could be carried out at this stage besides configuration?
7. What is the purpose of the delay(2*iTime); command in the program? How would the
program function without this command?
8. How can debugging processes be performed in the event of an error in the current
program? What methods can be used?
9. What changes should be made in the program to make the LEDs blink randomly
instead of in a specific order?
10. How could a more complex version of this program be designed? For instance, what
should be done to add features like different-colored LEDs blinking at different times
or synchronizing with music?

100
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The iTime variable determines how long the LEDs will stay on and the delay between
transitions to the next LED. The value of this variable can be increased or decreased to
control the blinking speed of the LEDs in different scenarios.
2. If the value of the iTime variable is reduced, the LEDs will blink faster; if increased,
they will blink slower. This can create a blinking effect perceivable by the human eye.
3. “For” loop structures are used in the program. These structures perform sequential
operations from a specific starting point to an ending point through a certain number
of iterations. In this case, they are used for turning the LEDs on and off in sequence.
4. The order of the LEDs lighting and extinguishing is determined by the iteration order
in the for loop. To change this ordering, one could alter the start and end values of the
loop variables or reverse the direction of the loop.
5. To control multiple LEDs simultaneously, we can use the digitalWrite command with
multiple pin numbers at the same time or perform port manipulation using a single
command for adjacent pins.
6. In the setup section, the modes (input/output) of the pins to be used are determined. At
this stage, initial values can be assigned as needed, or other initial adjustments such as
serial communication can be made.
7. The delay(2*iTime); command creates a pause between two sets of loops, so there is a
certain wait after all LEDs are off before the next cycle begins. Without this
command, the LEDs would start lighting up again immediately.
8. For debugging, the serial monitor can be used, the states of the LEDs can be printed
over the serial port, or control statements can be placed at certain points in the code.
9. To make the LEDs blink randomly, functions generating random numbers can be
used, and these numbers can determine which LED pin will light up or the duration of
the blinking.
10. A more complex version of the program could be designed by adding features like
using LEDs of different colors, synchronizing the blinking pattern with music or
sound, using different light intensities, etc. This would require more hardware
components and complex coding techniques.

101
Microcontrollers for Steam-AI Students Kamil Bala

1.2.12. Outward-Blinking Lights Program

https://www.tinkercad.com/things/1Nfh8WQlVBq-1212-outward-blinking-lights-program

/**************************************************************************
***
Program Name: Outward-Blinking Lights Program

Program Objective: We ensure the LEDs blink in the following sequence:


9 10
8 11
7 12
6 13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova Deneyap Workshop / 2022
***************************************************************************
***/
int iTime=300; // The variable iTime defining the delay

void setup()
{
// We execute the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
// We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
int iPinTwo=9; // We define the starting value for the second LED

for(int iPin=10; iPin<14; iPin++)


{
digitalWrite(iPin, HIGH); // We make the current value of iPin logic
HIGH
digitalWrite(iPinTwo, HIGH); // We make the current value of iPinTwo
logic HIGH
delay(iTime); // We provide a delay of iTime
digitalWrite(iPin, LOW); // We make the current value of iPin logic LOW
digitalWrite(iPinTwo, LOW); // We make the current value of iPinTwo
logic LOW
delay(iTime); // We provide a delay of iTime
iPinTwo--; // We decrease the current value of iPinTwo by one
}

102
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:


This Arduino program controls the blinking of LEDs in a specific sequence. The basic logic
of the program is to turn the LEDs on and off in order with a certain delay. However, in this
scenario, the LEDs blink in a symmetrical pattern, meaning they light up towards the center
from one side and from the other side respectively. Here are the fundamental operating
principles of the program:
1. Variable Definition: A variable named ‘iTime’ is defined to manage delays
throughout the rest of the program. This delay determines how long the LEDs will stay
on and the waiting period before transitioning to the next LED.
2. Setup Function: In Arduino’s setup method, the pins to be used are set as outputs. In
this instance, pins 6 through 13 are set to output mode. These pins are used to control
the connected LEDs.
3. Loop Function: The loop function is Arduino’s main operating cycle, and the code
runs continuously here. In this section, the sequence of blinking LEDs is managed.
o Two different variables (iPin and iPinIki) are used to control the sequence of
the blinking LEDs. iPin starts at 10 and increases to 13, while iPinIki starts at 9
and is decremented with each cycle.
o Both LEDs (iPin and iPinIki) are set to the HIGH (on) state using the
digitalWrite function. This action turns on the LEDs.
o The delay(iTime) function allows for a certain waiting period. During this
time, both LEDs remain lit. o Subsequently, both LEDs are set to the LOW
(off) state, meaning they are turned off. Again, a certain waiting period is
ensured with the delay(iTime) function.
o With the iPinIki-- command, the value of the iPinIki variable is decreased by
one with each cycle. This action creates symmetrical movement in the blinking
sequence of the LEDs.
This program creates a visual effect by controlling the blinking of LEDs connected to
adjacent pins in a specific pattern. The blinking speed of the LEDs is controlled by the value
of the iTime variable. By decreasing or increasing this value, the speed of the blinking effect
can be adjusted.

103
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

This program controls the blinking of LEDs in a specific pattern. The process of turning each
LED on and off is managed with the help of two variables, and these variables are updated in
each iteration. Here is a step-by-step explanation of this process:
1. Variable Definition:
o A variable named ‘iTime’ is defined with ‘int iTime = 200;’ and assigned the
value of 200. This represents the delay time in milliseconds.
2. Setup Function:
o The ‘void setup()’ function runs once when the program starts. Pin
configurations are made within this function.
o Using the loop ‘for(int iPin = 6; iPin < 14; iPin++)’, pins 6 through 13
(inclusive) are set to output mode. These pins will be used for controlling the
LEDs.
3. Loop Function:
o The ‘void loop()’ function is the continuously operating part of the Arduino
program. This section includes the LED blinking cycle.
4. Variable Definition and Turning on the First LED:
o A second variable is defined with ‘int iPinIki = 9;’, assigning it the value of 9.
o The loop ‘for(int iPin = 10; iPin < 14; iPin++)’ is initiated. This loop involves
pins 10 through 13 (inclusive).
o First, the LED numbered iPin is turned on (set to HIGH level) with the
command ‘digitalWrite(iPin, HIGH);’.
o Similarly, the iPinIki numbered LED is also turned on with the
‘digitalWrite(iPinIki, HIGH);’ command.
5. Delay Period and Turning off the LEDs:
o The ‘delay(iTime);’ command ensures a wait for the defined period. During
this time, the LEDs remain lit.
o Both LEDs are turned off (set to LOW level) with the ‘digitalWrite(iPin,
LOW);’ and ‘digitalWrite(iPinIki, LOW);’ commands.
6. Updating the Second Variable and Continuing the Loop:
o The command ‘iPinIki–;’ decreases the value of the iPinIki variable by one.
This action changes the LED being controlled in the next cycle.
o Again, a wait for the defined period is ensured with the ‘delay(iTime);’
command. During this time, the LEDs stay off.
7. Ending the Loop:
o When the ‘for’ loop ends (i.e., when the iPin variable reaches 14), the end of
the loop() function is reached, and the function returns to the beginning.

104
Microcontrollers for Steam-AI Students Kamil Bala

These steps are repeated in each cycle of the program. This process continues as long as the
Arduino is connected to a power source. In each cycle, the LEDs controlled by the iPin and
iPinIki variables change, creating a pattern of different LEDs blinking.

105
Microcontrollers for Steam-AI Students Kamil Bala

Questions Related to the Circuit:

1. What is the purpose of the iTime variable used in the program, and how can the value
of this variable be adapted to different scenarios?
2. What is the effect of the iPinIki variable on the operation of the program, and how is
the initial value of this variable chosen?
3. What processes are carried out with the for loop defined in the program, and how can
this loop be expanded for more complex scenarios?
4. If multiple LEDs are to be controlled simultaneously in the program, how should the
existing code structure be changed?
5. What is the operating principle of the digitalWrite function, and how can this function
be used to control different electronic components?
6. How is the delay function in the program used to add different delays between the
blinking durations of the LEDs?
7. What are the effects of this program on energy consumption, and what changes can be
made to save energy?
8. How is the debugging process carried out in case of any error in the program?
9. If this code is to be part of a larger project, what functions or modules can be added to
expand the system?
10. If this program were to be used in a product, what additional features or precautions
could be considered for the safety or ease of use of users?

106
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. The iTime variable is used to determine the delay time between the blinking of the
LEDs. This value can be changed to adapt to different scenarios; for example, if we
want the LEDs to blink faster, we can decrease this value.
2. The iPinIki variable is used to control a second set of LEDs. The initial value of this
variable is chosen based on the desired starting LED position and is decreased with
each loop to create a “running” effect among the LEDs.
3. The for loop is used for tasks that need to be repeated a certain number of times. For
more complex scenarios, it can be expanded by changing the commands and/or
conditions within the loop.
4. If we want to control multiple LEDs at the same time, we can use multiple
digitalWrite commands or expand the code using an array and loop.
5. The digitalWrite function works by sending a high (HIGH) or low (LOW) signal to a
specific pin. To control different components, this function is used in conjunction with
the pin number to which the component is connected.
6. To add different delays between the blinking times of the LEDs, we can diversify
within the code using the delay function and different time values.
7. Energy consumption is especially important for devices running on batteries.
Strategies like reducing the burning duration of LEDs, using lower power-consuming
components, or switching to low-power modes can save energy.
8. The debugging process could involve printing error messages over the serial port or
observing the behavior of the LEDs to identify logical errors in the code.
9. To expand the code, additional functions and modules such as reading data from
sensors, providing remote control, or adding a user interface can be included.
10. For user safety and ease of use, features such as preventing overheating, providing
guidance to the user, or controlling with a physical button could be considered.

107
Microcontrollers for Steam-AI Students Kamil Bala

1.2.13. sequentially lights up from the inside out, then extinguishes


simultaneously
https://www.tinkercad.com/things/d7KfLzqpalU-1213-sequentially-lights-up-from-the-inside-
out-then

/**************************************************************************
***
Program Name: Program that sequentially lights up from the inside out, then
extinguishes simultaneously

Purpose of the Program: We enable the LEDs to light up and go off in the
following sequence

9 10
8 9 10 11
7 8 9 10 11 12
6 7 8 9 10 11 12 13

9 10
8 9 10 11
7 8 9 10 11 12
6 7 8 9 10 11 12 13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=200; // iTime variable defining the delay

void setup()
{
// Execute the function by incrementing pins from 6 to 13
for(int iPin=6; iPin<14; iPin++)
{
// We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
int iPinIki=9; // We define the starting value for the second LED

for(int iPin=10; iPin<14; iPin++)


{
digitalWrite(iPin, HIGH); // We make the current value of iPin
logically HIGH
digitalWrite(iPinIki, HIGH); // We make the current value of iPinIki
logically HIGH
delay(iTime); // We provide a delay for iTime

108
Microcontrollers for Steam-AI Students Kamil Bala

iPinIki--; // We decrease the current value of iPinIki by one


}
delay(iTime); // We provide a delay for iTime

iPinIki=9;
for(int iPin=10; iPin<14; iPin++)
{
digitalWrite(iPin, LOW); // We make the current value of iPin logically
LOW
digitalWrite(iPinIki, LOW); // We make the current value of iPinIki
logically LOW
iPinIki--; // We decrease the current value of iPinIki by one
}
delay(iTime); // We provide a delay for iTime
}

109
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program is written to control a series of LEDs within a specific sequence. The
program ensures each LED lights up in order, one at a time, for a certain period, creating a
visible animation or sequential light pattern. The operation of the program is as follows:
1. Variable Definition:
o int iTime=200;: This is the delay time that determines how long each LED will stay
on (or off) (in milliseconds).
2. Setup Function:
o This function runs once when the program starts. Here, the pins to be used are set to
output mode. Pin numbers from 6 to 13 are associated with LEDs in this example and
are defined as output.
3. Loop Function:
o This function is the part that the Arduino continuously operates, and here the
sequence of the LEDs blinking is controlled.
o The iPinIki variable is set to 9 as a starting value for use in an inner loop. This
determines which second LED will light up.
o The first for loop operates for pins from 10 to 14 (including 13). In each loop
iteration, a specific LED receives a high signal (HIGH), lights up, and after a
certain period, receives a low signal (LOW) to turn off. Meanwhile, a second
LED indicated by the iPinIki variable undergoes the same process. In each
iteration, the iPinIki variable is decreased by one, allowing the next LED to be
selected.
o After the first loop, the program pauses for a short period (delay(iTime)), then
the iPinIki variable is reset to its starting value.
o The second for loop operates to turn off the LEDs. This loop reverses the
blinking effect of the LEDs. Each iteration turns off a specific LED and a
second LED indicated by the iPinIki variable.
o Note that there is a delay between and at the end of both loops. This creates a
visible delay between the LEDs’ blinking, making the animation appear
smooth.
This program creates a visual effect by making the LEDs blink in sequence. Such animations
are useful for user interface notifications, simple visual indicators, or light shows for
entertainment purposes.

110
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o At the start of the program, a delay time is defined with the line int iTime =
200;, indicating how long the LEDs will stay on or off. This value is in
milliseconds and controls the wait time between each LED operation.
2. Setup Function:
o The setup() function runs once with the initiation of Arduino. Here, the pins to
be used are set to output mode. Digital pins from 6 to 13 on the Arduino are
associated with LEDs in this example and are set as output.
3. Loop Function:
o The loop() function is the continuously operating section on the Arduino, and
here the LEDs are controlled. The program continuously repeats the codes
within this function.
4. LED Control Variables:
o The int iPinIki = 9; variable is defined; this sets the starting point that will be
used for controlling a second set of LEDs.
5. LED Lighting Loop:
o The first for loop operates for pins from 10 to 14 (including 13). Each time the
loop iterates, a certain LED (iPin) and a second LED (iPinIki) light up by
receiving a HIGH signal.
o After each iteration, the iPinIki variable decreases by one (iPinIki–), meaning
the next LED is activated.
o This process continues until all LEDs in the specified pin range are lit.
o After each LED is lit, the program waits for a certain period with the
delay(iTime) command. This ensures the LEDs stay lit for a visibly
appreciable time.
6. Transition Time:
o After the first loop, a pause is implemented using the delay(iTime) function.
This provides a pause before moving on to the next step.
7. LED Extinguishing Loop:
o The iPinIki variable is reset to its starting value (9).
o The second for loop starts, and this time it is used to turn off the LEDs. With
the same logic, each iteration turns off a certain LED (iPin) and a second LED
(iPinIki) by giving them a LOW signal.
o The iPinIki variable decreases by one in each iteration, meaning the next LED
turns off.
o After all LEDs are turned off, the program waits again with the delay(iTime)
command for a certain period.

111
Microcontrollers for Steam-AI Students Kamil Bala

8. Loop Repetition:
o Since there is no other command at the end of the loop() function, the Arduino
reruns this function from start to finish repeatedly, thus the LEDs keep
blinking in a certain sequence. These steps explain the general flow of the
program and how each part operates. Control of the LEDs happens through
defined time intervals and loops, thereby creating the desired visual effect.

112
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. What is the primary purpose of this Arduino program, and what could be the real-
world applications of such a program?
2. What does the iTime variable in the program signify, and how would changing its
value affect the behavior of the program?
3. What is the function of the for loop structure used in this code, and how would you
describe the relationship between the loops?
4. How is the synchronization of the LEDs achieved? That is, can you explain why one
LED does not turn off at the same time as another is on?
5. What is the function of the iPinTwo variable, and how and why does its value change?
6. If a different number of LEDs were used, how would you modify this code? What
parameters would need to be changed?
7. What would be the debugging steps in any error situation in this program (for
example, if an LED does not light up)?
8. What is the role of the delay() function in the program? What kind of problems could
arise without this function?
9. If you wanted to change the blinking pattern of the LEDs, how would you alter which
parts of this code?
10. What features or functions would you consider adding to take this project further? For
example, using LEDs of different colors or synchronization with music.

113
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The main purpose of the program is to control LEDs in a specific pattern. In the real
world, such programs are often used in visual alert systems, entertainment lighting, or
educational projects.
2. The iTime variable controls the transition time (delay) between LEDs. Increasing this
value makes the transitions between LEDs slower, while decreasing it leads to faster
transitions.
3. The for loop allows for a certain number of repetitions. In this case, it’s used to turn
LEDs on and off in sequence. Loops ensure specific tasks in the program are
performed in a particular order.
4. Synchronization of the LEDs is controlled by the flow of the code. Each LED is
controlled individually, dictating its interaction with others. If one LED is on, others
will either light up or turn off based on the instructions in the program.
5. The iPinTwo variable represents an index indicating which LED is to be controlled in
a loop. This value decreases with each repetition of the loop, thus moving on to the
next LED.
6. Using a different number of LEDs would require changing the start and end
parameters in the for loop. Also, you might need to update the pin numbers to which
each LED is connected.
7. In error situations, the first step is usually to check the physical connections and verify
whether each LED is functioning correctly. Then, checking the accuracy of the code
and using tools like a serial monitor for debugging could follow.
8. The delay() function causes the program to pause for a specific time. Without this
function, all operations happen immediately, making it difficult to observe the
blinking effect of the LEDs.
9. To change the blinking pattern, you could alter the digitalWrite() and delay() functions
within the loops or add new loops to create a different sequence or timing.
10. To expand the project, you could add LEDs of different colors, use additional
components like sound sensors or motion detectors, or create an interface that allows
the user to control the blinking pattern of the LEDs.

114
Microcontrollers for Steam-AI Students Kamil Bala

1.2.14. sequentially lights up from the inside out, then extinguishes from the
inside out
https://www.tinkercad.com/things/a4b6by5hFUs-1214-sequentially-lights-up-from-the-inside-
out-then

/**************************************************************************
***
Program Name: Program that sequentially lights up from the inside out, then
extinguishes from the inside out

Program Objective: We enable the LEDs to light up and turn off in the
following sequence:

9 10
8 9 10 11
7 8 9 10 11 12
6 7 8 9 10 11 12 13
6 7 8 11 12 13
6 7 12 13
6 13
Written by: Kamil Bala
kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=300; // Variable 'iTime' defining the delay

void setup()
{
//Executing the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
//We set the current content of 'iPin' as an output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
int iPinTwo=9; // We define a starting value for the second LED

for(int iPin=10; iPin<14; iPin++)


{
digitalWrite(iPin, HIGH); //We make the current value of 'iPin' logically
HIGH
digitalWrite(iPinTwo, HIGH); //We make the current value of 'iPinTwo'
logically HIGH
delay(iTime); // We provide a delay of 'iTime'
iPinTwo--; // We decrease the current value of 'iPinTwo' by one
}

115
Microcontrollers for Steam-AI Students Kamil Bala

delay(iTime); // We provide a delay of 'iTime'


iPinTwo=9;
for(int iPin=10; iPin<14; iPin++)
{
digitalWrite(iPin, LOW); //We make the current value of 'iPin' logically
LOW
digitalWrite(iPinTwo, LOW); //We make the current value of 'iPinTwo'
logically LOW
delay(iTime); // We provide a delay of 'iTime'
iPinTwo--; // We decrease the current value of 'iPinTwo' by one
}
delay(iTime); // We provide a delay of 'iTime'
}

116
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program

This Arduino program controls a series of LEDs in a specific sequence. The program creates a
“running lights” effect by lighting two LEDs simultaneously, with one following one
direction and the other moving in the opposite direction. Between the two cycles, there is a
delay (delay) to ensure the lights remain lit for a certain time before moving on to the next
cycle. Here’s the general structure of the program:
1. Variable Definition:
o The delay time (in milliseconds) to be used throughout the program is defined
with ‘int iTime = 200;’.
2. Setup Function:
o Pins are set in output mode with ‘pinMode(iPin, OUTPUT);’. This is done
within a loop for pins ranging from 6 to 13.
3. Main Loop (loop) Function:
o First, a variable named ‘iPinTwo’ is initiated with the value 9. This represents
the starting point of the second LED to be controlled.
o Two ‘for’ loops are used. In the first loop, LEDs connected to pins ranging
from 10 to 14, and the LED indicated by ‘iPinTwo’, are set to high (HIGH)
sequentially, causing them to light up. With each step, the ‘iPinTwo’ variable
is decreased by one, meaning it moves in the opposite direction.
o The LEDs light up and stay on for a certain period (as long as ‘iTime’).
o After the first cycle is complete, the ‘iPinTwo’ variable is reset to 9. o In the
second ‘for’ loop, a similar process occurs, but this time the LEDs are turned
off (made LOW). ‘iPinTwo’ also decreases with each iteration.
o Between and at the end of both cycles, a specific delay time is applied. This is
necessary for the blinking effect of the lights to be visually perceptible.
This program provides a simple visual interaction with LEDs and is often used in
entertainment, decorative lighting, or educational electronic projects. The sequence and
duration of each LED lighting are controlled using the program’s flow and delay functions.

117
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o ‘int iTime = 200;’: At the beginning of the program, the delay time (in
milliseconds) is defined, determining how long the LEDs will stay on or off.
2. Setup Function:
o This function runs once when the program is loaded. Here, the modes of the
pins to be used are set. o Using a ‘for’ loop, all pins from 6 to 13 are set to
output mode. This process signifies that the LEDs are connected to these pins
and these pins will be used to control the LEDs.
3. Loop Function:
o This function is continuously repeated on the Arduino and is where the main
operations occur.
o Initially, a variable named ‘iPinTwo’ is initiated with the value 9. This
represents the second set of LEDs that will be controlled simultaneously.
o Then, a ‘for’ loop begins, and LEDs connected to pins from 10 to 14 (actually
up to 13, because 14 is not included) are made HIGH (on). Simultaneously, the
LED represented by the ‘iPinTwo’ variable is also made HIGH. At this step,
both groups of LEDs light up.
o The ‘delay(iTime)’ function stops the rest of the program for the specified
period, allowing the LEDs to stay lit for a certain duration.
o With each iteration, ‘iPinTwo’ decreases by one, creating a “running” effect as
the next LED lights up in every cycle.
o After the first ‘for’ loop completes, another delay is added with the
‘delay(iTime)’ function.
o ‘iPinTwo’ is reset to 9, reinitializing the starting position for the next cycle.
o A second ‘for’ loop starts. This cycle turns the LEDs on the same pins to LOW
(off), but this time, each LED is turned off individually during the cycle.
o ‘iPinTwo’ decreases with each iteration, and by the end of the cycle, both sets
of LEDs are off. o Finally, another delay is added with the ‘delay(iTime)’
function, ensuring all LEDs stay off for a certain period.
This process repeats indefinitely until the Arduino is stopped. In each cycle, the
LEDs light up and go off in a specific sequence, presenting a moving light show to
the viewers.

118
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. What is the general logic of the program’s operation, and can you explain in which
practical scenarios such a LED arrangement could be used?
2. What role does the ‘iTime’ variable play in the program, and can you discuss what
kinds of effects you might encounter if you change this variable’s value?
3. Can you explain the structure of the ‘for’ loops used in the program and how these
loops contribute to the overall flow of the program?
4. How is the sequence of the LEDs lighting up and turning off controlled in the
program? Can you explain what changes need to be made to alter this sequence?
5. What is the significance of the delay() function in the program? Without this function,
how would the behavior of the LEDs lighting up and turning off be affected?
6. What is the purpose of the ‘iPinIki’ variable in the program, and how and why does
this variable change?
7. Based on the logic of this code, can you discuss how you could make changes to have
the LEDs light up and turn off in a different sequence or pattern?
8. What would your debugging process be in the event of any error or unexpected
behavior in the program?
9. What are your thoughts on the energy efficiency of this program in terms of power
consumption, and what optimizations could you suggest to reduce energy use?
10. How do you think this program could be expanded to create a more complex light
show, and what additional features or functions could be added?

119
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Answer: The program creates a visual pattern by turning LEDs on and off in a
specific sequence. In practice, such arrangements can be used in scenarios such as
entertainment light shows, warning signals, or status indicators.
2. Answer: ‘iTime’ determines how long the LEDs stay on (or off). Changing this value
directly affects the blinking speed of the LEDs.
3. Answer: The ‘for’ loop allows for a certain number of repetitions to be performed. In
this program, the loops facilitate controlling the LEDs in sequence, enabling the
creation of a visual pattern.
4. Answer: The sequence of lighting up and turning off is controlled by the
digitalWrite() commands within the loop. To change the sequence, it may be
necessary to alter the order of these commands or the loop parameters.
5. Answer: The delay() function causes the program to pause for a certain period,
allowing the LEDs to be visibly turned on and off. Without this function, the LEDs
would blink too fast, making it imperceptible to the eye.
6. Answer: ‘iPinIki’ is used to control a second set of LEDs. With each loop iteration,
the value of this variable decreases, allowing the transition to the next LED.
7. Answer: To create a different sequence or pattern, we could alter loop variables, the
order of digitalWrite() commands, and/or delay() durations.
8. Answer: The debugging process would involve checking the code step by step,
monitoring outputs on the serial monitor, and making systematic changes in the code
to diagnose potential issues.
9. Answer: The program continuously controls the LEDs, requiring a certain amount of
energy consumption. To reduce energy consumption, we could decrease the number of
LEDs flashing, shorten the on-times, or use lower-energy LEDs.
10. Answer: To expand the program, features such as LEDs of different colors, music or
sound synchronization, different flashing patterns, and even dynamic changes
controlled by user input could be added.

120
Microcontrollers for Steam-AI Students Kamil Bala

1.2.15. lights up LEDs from the inside out, then extinguishes them from the
outside in
https://www.tinkercad.com/things/6r9LezgZbR8-1215-lights-up-leds-from-the-inside-out-
then-extinguishes-them

/**************************************************************************
***
Program Name: Program that lights up LEDs from the inside out, then
extinguishes them from the outside in

Program Purpose: We are making the LEDs light up and turn off in the
sequence below:
9 10
8 9 10 11
7 8 9 10 11 12
6 7 8 9 10 11 12 13
7 8 9 10 11 12
8 9 10 11
9 10

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=300; // The variable 'iTime' defining the delay

void setup()
{
//We execute the function by incrementing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
//We set the current content of 'iPin' as an output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
int iPinTwo=9; // We define a starting value for the second LED

for(int iPin=10; iPin<14; iPin++)


{
digitalWrite(iPin, HIGH); //We set the current 'iPin' value to logic
HIGH
digitalWrite(iPinTwo, HIGH); //We set the current 'iPinTwo' value to
logic HIGH
delay(iTime); // We induce a delay for 'iTime'
iPinTwo--; // We decrease the current 'iPinTwo' value by one
}

121
Microcontrollers for Steam-AI Students Kamil Bala

delay(2*iTime); // We induce a delay for '2iTime'

iPinTwo=6;
for(int iPin=13; iPin>9; iPin--)
{
digitalWrite(iPin, LOW); //We set the current 'iPin' value to logic LOW
digitalWrite(iPinTwo, LOW); //We set the current 'iPinTwo' value to
logic LOW
delay(iTime); // We induce a delay for 'iTime'
iPinTwo++; // We increase the current 'iPinTwo' value by one
}
delay(2*iTime); // We induce a delay for '2iTime'
}

122
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program controls the blinking of LEDs in a specific sequence and timing. The
LEDs are managed through pins physically connected to the microcontroller. The operation of
the program is based on sending electrical signals to specific pins to enable these LEDs to
light up or turn off. Here are the steps:
1. Initial Settings (setup function):
o The program sets certain pins of the microcontroller (in this case, pins 6
through 13) to output mode. This means these pins will send electrical signals
and control the connected LEDs.
2. Main Loop (loop function):
o The main part of the program occurs here, and this function continuously
repeats as long as the microcontroller is operating.
o First, it lights up the LEDs in a specific sequence (on pins 10 through 14) and
simultaneously manages another series of LEDs (starting initially at pin 9).
Each pair of LEDs stays on for a certain duration (defined by the iTime
variable).
o In each iteration, the control point of the second LED series (iPinTwo)
decreases by one, causing the lights to move in a pattern.
o After the LEDs are lit, the program pauses for a period of 2_iTime.
o Then, the LEDs are turned off through a similar loop. This time, the LEDs on
pins 13 to 10 are turned off, and another series of LEDs is controlled using the
iPinTwo variable. iPinTwo increases by one in each iteration.
o After the LEDs are turned off, the program pauses again for 2_iTime. This
loop repeats until the microcontroller is turned off.
The program uses these cyclical lighting and extinguishing processes to provide a visual
display. The blinking speed and pattern of the LEDs depend on the iTime variable and the
increment/decrement logic in the loops.

123
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime=200;: Defines the delay duration (in milliseconds) that determines
how long the LEDs will remain on (or off).
2. Setup Function:
o This function runs once when the program is loaded and sets the pins to their
initial states. o pinMode(iPin, OUTPUT);: Sets the pins from 6 to 13 to output
mode. These pins will control the LEDs.
3. Loop Function: This function continuously operates on the Arduino and contains the
main processes.
o int iPinTwo=9;: Defines a variable named iPinTwo and sets its value to 9. This
variable is used for managing the control of a series of LEDs.
o First Loop (Turning on the LEDs):
▪ Lights up the LEDs on pins 10 to 13 and those indicated by the
iPinTwo variable.
▪ In each iteration, the digitalWrite function turns on the LED on the
relevant pin (sets it to HIGH).
▪ delay(iTime);: After each LED is lit, the program pauses for a certain
period (defined by the iTime variable).
▪ iPinTwo–;: In each loop, it decreases the iPinTwo variable by one, thus
lighting a different LED in the next iteration.
▪ After the first loop is completed, a delay period is applied
(delay(2_iTime);) before moving on to the next stage. o Second Loop
(Turning off the LEDs):
▪ iPinTwo=6;: Resets the value of the iPinTwo variable to 6.
▪ Turns off the LEDs on pins 13 to 10 and those indicated by the
iPinTwo variable.
▪ In each iteration, the digitalWrite function turns off the LED on the
relevant pin (sets it to LOW).
▪ delay(iTime);: After each LED is turned off, the program pauses for a
certain period (defined by the iTime variable).
▪ iPinTwo++;: In each loop, it increases the iPinTwo variable by one,
thus turning off a different LED in the next iteration.
▪ After the second loop is completed, a delay period is applied
(delay(2_iTime);) before moving on to the next stage.
4. Repetition of the Loop:
o The loop function endlessly repeats the steps until the Arduino device is turned
off. These steps detail how the program operates and how the LEDs are

124
Microcontrollers for Steam-AI Students Kamil Bala

controlled. Each step dictates how the LEDs blink in a specific pattern and
timing.

125
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the basic logical structure used in this program, and how can this structure be
adapted to different scenarios?
2. What is the role of the iTime variable in the program, and what kind of differences can
we achieve by changing this value?
3. If you wanted to expand this program to create a more complex light show, what steps
would you follow?
4. What strategies would you implement for maintaining and updating this code, and
which parts of the code could be made more modular?
5. What is the impact of the delay function on the overall performance of the program,
and what could be the ways to reduce this impact?
6. Are there any errors in the current code, or could any optimizations be made in the
code to enhance the program’s efficiency?
7. How could we adapt this code using a different microcontroller, and what challenges
might we encounter in this process?
8. What hardware components could be added to expand the functionality of this
program, and how would this addition process be managed?
9. By making any changes in the current loops, how can we alter the blinking pattern of
the LEDs?
10. What methods would you use during a debugging process in this program, and how
could you identify and fix potential errors?

126
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The program uses loops to control the blinking of LEDs in a specific pattern. This
structure can be modified or expanded to create different blinking models.
2. The iTime variable controls the delay intervals between the LEDs. By changing this
value, we can adjust the blinking speed.
3. For a more complex light show, we could add features like different blinking models,
colors, or even synchronization with music. This might require additional hardware or
more advanced coding techniques.
4. For code maintenance and updates, we can increase reusability by using functions and
modules. Also, optimizations on variables and loops could improve code efficiency.
5. The delay function can hinder the program’s real-time performance because the
processor does nothing else during this time. Reducing this duration or using
asynchronous programming techniques can mitigate this issue.
6. It is important to check if there are any unnecessary loops or functions in the code that
could affect performance. For optimization, processes within loops can be minimized,
and unnecessary operations can be eliminated.
7. When using a different microcontroller, pin assignments, utilized libraries, and even
some function calls may change. We can manage these changes by referring to the
new system’s technical documentation.
8. Interactivity can be enhanced by adding new sensors, buttons, or other input devices to
the program. This addition process involves integrating new hardware and accordingly
updating the code.
9. By altering variables and conditions in the loops, we can enable the LEDs to create
different blinking patterns. This depends on creativity and the desired effect.
10. In the debugging process, adopting a systematic approach is important. First, we
should identify the differences between the expected and actual behaviors of the code.
Then, by narrowing down the potentially faulty sections and making corrections in the
code, we can eliminate errors.

127
Microcontrollers for Steam-AI Students Kamil Bala

1.2.16. Outward to Inward Blinking Program


https://www.tinkercad.com/things/0u738zUBqtq-1216-outward-to-inward-blinking-program

/**************************************************************************
***
Program Name: Outward to Inward Blinking Program

Purpose of the Program: We ensure that the LEDs blink in the following
order:

6 13
7 12
8 11
9 10

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova Deneyap Workshop / 2022
***************************************************************************
***/
int iTime=300; // The variable iTime defining the delay

void setup()
{
// We execute the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
// We assign the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
int iPinIki=13; // We define the starting value for the second LED

for(int iPin=6; iPin<10; iPin++)


{
digitalWrite(iPin, HIGH); // We make the current value of iPin
logically HIGH
digitalWrite(iPinIki, HIGH); // We make the current value of iPinIki
logically HIGH
delay(iTime); // We provide a delay for iTime duration
digitalWrite(iPin, LOW); // We make the current value of iPin logically
LOW
digitalWrite(iPinIki, LOW); // We make the current value of iPinIki
logically LOW
delay(iTime); // We provide a delay for iTime duration
iPinIki--; // We decrease the current value of iPinIki by one
}
}

128
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program controls a series of LEDs in a specific pattern. The program uses a
loop to sequentially turn each LED on and off, thus providing a moving light show for the
user. With two sets of LEDs, an effect is created of movements in opposite directions. Here is
a step-by-step explanation:
1. Variable Definition:
o iTime: This variable defines the delay time that indicates how long the LEDs
will remain on (or off). In this instance, each LED lights up and goes off for
200 milliseconds.
2. Setup Function:
o This is the block of code that is run once by the Arduino when the program
starts. o The function sets each pin being used to output mode, so the pins can
control the LEDs.
3. Main Loop (loop) Function:
o This function is continuously executed in a loop by the Arduino. o The iPinIki
variable defines the starting pin number for the second set of LEDs (in this
case, 13).
o The first for loop controls two sets of LEDs. One set is controlled with the iPin
variable (starting from 6 to 10), while the other set is controlled with the
iPinIki variable, decreasing with each loop iteration (starting from 13).
o In each loop, the corresponding LEDs are turned HIGH (i.e., on), waited for a
certain period, then turned LOW (i.e., off). This process is managed with a
specific delay using the delay function.
o At the end of each iteration, the iPinIki variable is decreased, so the LED to the
left of the previous one is selected in the next iteration.
This code allows the LEDs to blink in opposition to each other, creating a “running” effect
from one end to the other. Each loop iteration turns on and turns off the current pair of LEDs
before moving on to the next pair. This action is synchronized with the delay time defined in
the iTime variable, allowing control over the speed of the visual effect.

129
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o The program defines a variable named iTime and sets it to 200 milliseconds.
This is the delay time to be used at various points in the program.
2. Setup Function:
o The setup function runs once at the beginning of the program. Here, digital
pins from 6 to 13 are set as outputs. These pins are connected to LEDs, thus
allowing the Arduino to control them.
3. Loop Function:
o The loop function is the continuously operating part in the Arduino, where the
main operations are performed.
o First, a variable named iPinIki is defined and set to 13. This variable is used to
control a second set of blinking LEDs.
4. Control of the First LED Array:
o A for loop is initiated for LEDs on pins 6 through 10. Simultaneously, LEDs
controlled by the iPinIki variable are activated in reverse from 13.
o Within the loop, the digitalWrite function is used for both pins, turning the
LEDs on (set HIGH) sequentially. This action lights up the LEDs.
o The delay(iTime) function ensures the LEDs stay lit for a specific period (200
ms). o Then, the LEDs are turned off (set LOW) using the digitalWrite
function, causing the LEDs to go out.
o The next delay ensures the LEDs remain off until the next loop iteration after
they are extinguished.
o The iPinIki variable is decreased by one in every loop (iPinIki–), which
ensures the selection of the LED to the left of the previous one in each loop
iteration.
5. End of the Loop:
o The loop concludes when it has completed for the specified range of pins (in
this case, from 6 to 10). This completes one cycle of the process of lighting
the LEDs in a specific sequence. These steps constitute the main operational
logic of the program, and this process is continuously repeated within the loop
function, allowing the LEDs to blink in the desired order. The program
controls the LEDs in a specific pattern, creating a “running lights” animation.

130
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the role and significance of the iTime variable in the program? How would the
behavior of the program be affected if this variable did not exist?
2. What are the purposes of the setup and loop functions defined in the program, and
how do they operate?
3. What exactly does the digitalWrite function do, and what role does it have in
controlling the LEDs?
4. What is the purpose of the iPinIki variable within the program, and what is the impact
of changes made to this variable’s value on the overall functioning of the program?
5. What is the function of the for loop used in the program, and what processes occur
within the loop?
6. What changes could be made in the program to alter the durations of the LEDs turning
on and off?
7. Apart from the current operation of the program, what changes could be made to
create a different LED animation?
8. What factors could affect the overall performance of the program, and how can these
factors be optimized?
9. If this program encounters an error, what could be the possible sources of these errors,
and how could they be rectified?
10. What kind of projects or applications could be developed by expanding or modifying
this program? What contribution does adding new features or functions make?

131
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The iTime variable acts as a timer controlling how long the LEDs will blink. Without
this variable, the blinking duration of the LEDs would be constant, reducing the
program’s flexibility.
2. The setup function runs once when the program starts and sets the input/output states
of the pins. The loop function continuously repeats, controlling the blinking of the
LEDs.
3. The digitalWrite function is used to change the electrical state of a specific pin.
Through this function, the LEDs connected to the pins blink by switching between
HIGH (on) and LOW (off) states.
4. The iPinIki variable is used as a loop control variable within the program. Its value
determines which LED will blink. Changes to this value affect which LED will blink
and the sequence of this process.
5. The for loop repeatedly executes the code block within it as long as a certain condition
is met. In this program, the loop enables the LEDs to blink in sequence.
6. To change the blinking durations of the LEDs, the value of the iTime variable can be
altered. This directly affects how long each LED will blink.
7. To create a different LED animation, codes related to loops, delays, and which LEDs
will be activated can be changed. Additionally, different animations can be created by
changing the number or order of cycles.
8. Factors that could affect the overall performance of the program include the
microcontroller used, the configuration of the code, and external factors (like power
supply). Performance can be improved with code optimization and appropriate
hardware selection.
9. Possible sources of errors could include hardware issues, coding mistakes, or
connection errors. Debugging processes, code reviews, and checking connections may
be required to rectify errors.
10. This program could form the basis for various projects, such as different light shows,
interactive art projects, or educational applications. Adding new features or functions
enhances the project’s functionality and allows for the creation of more complex,
engaging systems.

132
Microcontrollers for Steam-AI Students Kamil Bala

1.2.17. LEDs blinking from the outside to inside, simultaneously


https://www.tinkercad.com/things/1Rlg8FAzhl0-1217leds-blinking-from-the-outside-to-
inside-simultaneously

/**************************************************************************
***
Program Name: LEDs blinking from the outside to inside, simultaneously

Purpose of the Program: We are making the LEDs blink in the order below:

6 13
6 7 12 13
6 7 8 11 12 13
6 7 8 9 10 11 12 13

6 13
6 7 12 13
6 7 8 11 12 13
6 7 8 9 10 11 12 13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/
int iTime=300; // The variable iTime defines the delay

void setup()
{
//We increment the pins from 6 to 13 by 1 and execute the function
for(int iPin=6; iPin<14; iPin++)
{
//We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
int iPinIki=13; // We define a starting value for the second LED

for(int iPin=6; iPin<10; iPin++)


{
digitalWrite(iPin, HIGH); // We set the current value of iPin to logic
HIGH
digitalWrite(iPinIki, HIGH); // We set the current value of iPinIki to
logic HIGH
delay(iTime); // We create a delay for iTime duration

133
Microcontrollers for Steam-AI Students Kamil Bala

iPinIki--; // We decrement the current value of iPinIki by one


}

delay(2*iTime); // We create a delay for iTime duration

iPinIki=13; // We retrieve the last value again


for(int iPin=6; iPin<10; iPin++)
{
digitalWrite(iPin, LOW); // We set the current value of iPin to logic
LOW
digitalWrite(iPinIki, LOW); // We set the current value of iPinIki to
logic LOW
iPinIki--; // We decrement the current value of iPinIki by one
}
delay(2*iTime); // We create a delay for iTime duration
}

134
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino code controls the blinking of LEDs in a specific pattern. The purpose of the
code is to control a series of LEDs in a particular order and within specific time intervals,
thereby creating a visual effect. Here’s a general description of the code:
1. Variable Definition:
o iTime: This variable indicates how long the LEDs will blink (in milliseconds).
In this example, the blinking duration for each LED is set to 300 ms.
2. Setup Function:
o This function performs the initial setup of the Arduino. Here, the code sets the
digital pins from 6 to 13 to output mode, allowing it to control the LEDs
connected to these pins.
3. Loop Function:
o This function is the main loop that runs continuously on the Arduino. The main
functionality of the program occurs here.
o First, the iPinIki variable is initiated with the value of 13, representing a
second set of LEDs. o The loop sends a high (HIGH) signal to specific pins
while the iPin variable changes from 6 to 10. This causes the LEDs connected
to the relevant pins to light up. Simultaneously, another set of LEDs is
controlled using the iPinIki variable, and these LEDs also light up.
o At each step, a specific delay is provided with the delay(iTime) function,
allowing the LEDs to be visibly lit for a while. o The iPinIki variable is
decreased by one with each loop, causing the second set of LEDs to progress in
the reverse order.
o After the LEDs are done lighting up, the program pauses for a while
(delay(2*iTime)).
o Subsequently, the same LEDs are turned off in sequence (set to LOW). This
process works like a mirror of the above procedure, but this time the LEDs are
turned off.
o Finally, another delay is added, and the loop repeats from the beginning. This
program provides a visual display by ensuring the synchronized blinking of
LEDs. The blinking duration of each LED is controlled with the iTime
variable, within a certain rhythm and pattern. In this scenario, the LEDs will
appear to be moving in opposite directions to each other, thus creating a
reciprocal animation effect.

135
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime=300;: A variable named iTime is defined, and its value is set as 300
milliseconds. This variable will be used to determine the delay durations
throughout the program.
2. Setup Function:
o void setup() { … }: This function runs once with the initiation of the Arduino.
Typically, pin configurations and initial settings are done here.
o Pin settings: Pins from 6 to 13 are set in output mode. This allows for the
control of the LEDs connected to these pins.
3. Loop Function:
o void loop() { … }: This function continuously operates on the Arduino. Here,
the cycle of LED lighting and turning off is controlled.
o int iPinIki=13;: A variable named iPinIki is defined with 13 as the initial value.
This represents a second set of LEDs to be controlled simultaneously.
o First for loop: The iPin variable undergoes iterations from
o 6 to 10 (10 not included). In each cycle, the LEDs represented by the iPin and
iPinIki variables are lit by receiving a HIGH signal. iPinIki is decreased by one
with each cycle, maintaining the relationship between the two sets of LEDs.
The program pauses for iTime milliseconds in each iteration.
o First delay: After the LEDs are lit, the program pauses for 2_iTime
milliseconds. This ensures the LEDs stay lit for a certain period.
o iPinIki=13;: The iPinIki variable is reset to the value of 13, resetting the
starting position for the second cycle.
o Second for loop: This loop is similar to the first one; however, this time, the
LEDs are turned off by receiving a LOW signal. The iPinIki variable is again
decreased by one in each cycle.
o Second delay: After the extinction process is complete, the program pauses
again for 2_iTime milliseconds. This ensures all LEDs remain off until the
next cycle. The program continuously repeats this cycle, thus creating a form
of animation with the LEDs blinking on and off over designated periods. This
process continues until the Arduino device is turned off or another program is
loaded.

136
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the function of the iTime variable in the program, and how does changing this
value affect how the program works?
2. If you wanted to create a different LED animation, what changes would you make to
the existing code?
3. What are the main differences between the setup() and loop() functions, and why is
each important?
4. Are there any errors in this code? If so, what steps need to be taken to correct these
errors?
5. How do the for loop structures in this code work, and what are the advantages of using
these structures?
6. What exactly does the digitalWrite() function do? What do the HIGH and LOW
parameters mean?
7. What are the potential disadvantages of the delay function used in this program? What
alternative methods could be used?
8. If you wanted to change the order in which the LEDs light up or turn off, what
changes would you make in the code?
9. What additional features or components could be added to make this project more
advanced?
10. With the current state of the code, what types of test scenarios should be applied, and
how should the results of these tests be evaluated?

137
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The iTime variable controls the delay time between LEDs. Changing this value
directly affects how long the LEDs stay on (or off), essentially setting the speed of the
animation.
2. To create a different LED animation, I could adjust the boundaries of the for loops, the
digitalWrite() commands, and the delay() durations to create different blinking
patterns and sequences.
3. The setup() function runs once when the program starts and is typically used for initial
settings like setting pin modes. The loop() function runs continuously, meaning the
code within it is repeatedly executed, allowing for continuous control over the circuit.
4. There do not appear to be any blatant errors in the code; however, testing with real
hardware is crucial to identify possible logic errors or unexpected behaviors.
5. The for loop is used to perform a certain number of iterations. Advantages of using
this structure include reducing code repetition, writing cleaner and more manageable
code, and having full control over the variable within the loop.
6. The digitalWrite() function writes a specified value to a specified pin. HIGH typically
raises the pin to 5V (or 3.3V, depending on the microcontroller used), and LOW pulls
it to 0V (ground). This controls the turning on and off of the LED.
7. The disadvantage of the delay() function is that it stops the program from doing
anything else during that time (blocking behavior). Alternatives include using a timer
to call a function after a certain time or non-blocking approaches that check elapsed
time with the millis() function.
8. To change the order of LED lighting or turning off, we can adjust the increment or
decrement values in the loop variables, the start and end conditions, and/or the order
of digitalWrite() calls.
9. To make the project more advanced, components like sensors (e.g., temperature,
motion), an LCD screen (to display status or values), or wireless control (via
Bluetooth or Wi-Fi) could be added.
10. Test scenarios could include running with different delay times, using a different
number of LEDs, and running for extended periods to check the circuit’s reliability
and durability. Test results should be evaluated for conformity with expectations,
performance, and reliability.

138
Microcontrollers for Steam-AI Students Kamil Bala

1.2.18. LEDs that gather from outside to inside and fade from outside to
inside
https://www.tinkercad.com/things/cU36XkmELam-1218-leds-that-gather-from-
outside-to-inside-and-fade-from

/**************************************************************************
***
Program Name: Program that accumulates from outside to inside, then removes
from outside to inside

Program Purpose: We ensure that the LEDs light up and go out in the order
below:
6 13
6 7 12 13
6 7 8 11 12 13
6 7 8 9 10 11 12 13
7 8 9 10 11 12
8 9 10 11
9 10

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova Deneyap Workshop / 2022

***************************************************************************
***/
int iTime=300; // Variable 'iTime' defining the delay

void setup()
{
// We execute the function by increasing the pins from 6 to 13 by 1
for(int iPin=6; iPin<14; iPin++)
{
// We assign the current content of iPin as an output pin
pinMode(iPin, OUTPUT);

139
Microcontrollers for Steam-AI Students Kamil Bala

}
}

void loop()
{
int iPinIki=13; // We define a starting value for the second LED

for(int iPin=6; iPin<10; iPin++)


{
digitalWrite(iPin, HIGH); // We make the current value of iPin
logically HIGH
digitalWrite(iPinIki, HIGH); // We make the current value of iPinIki
logically HIGH
delay(iTime); // We provide a delay of 'iTime'
iPinIki--; // We decrease the current value of iPinIki by one
}

delay(2*iTime); // We provide a delay of 2iTime

iPinIki=13; // We take the last value again


for(int iPin=6; iPin<10; iPin++)
{
digitalWrite(iPin, LOW); // We make the current value of iPin logically
LOW
digitalWrite(iPinIki, LOW); // We make the current value of iPinIki
logically LOW
delay(iTime); // We provide a delay of 'iTime'
iPinIki--; // We decrease the current value of iPinIki by one
}
delay(iTime); // We provide a delay of 'iTime'
}

140
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino code creates a specific LED blinking pattern. The program takes control of
LEDs connected to specific pins, turning them on and off in a certain sequence to create a
kind of “light show.” Here’s an overview:

1. Variable Definition:
o A variable that will act as a timer for delays between LEDs is defined with ‘int
iTime = 200’. In this case, there will be a 200-millisecond delay between each
LED change.
2. Setup Function:
o The ‘setup()’ function is used to initiate pin configurations. Here, pins 6
through 13 are set to output mode because LEDs are connected to these pins.
3. Main Loop Function:
o The ‘loop()’ function is the code that Arduino continuously runs, and this is
where the LED animation happens.
o Initially, the iPinIki variable is set to 13; this will be used to control a second
set of LEDs.
o Two for loops are used to control different LEDs. The first loop turns on the
LEDs on pins 6 through 10 in sequence and also turns on the LED indicated by
iPinIki at the same time. In every iteration, the iPinIki variable is decreased
(decremented), moving to the next LED.
o After the LEDs are turned on, the program waits for a certain period with the
command delay(2*iTime); this ensures the LEDs stay on for a while.
o Then, a second for loop turns off the same LEDs in sequence. The iPinIki
variable is reset to 13 and decreases with every iteration.
o Finally, the delay(iTime) function provides a delay before the next loop
iteration. This code offers a visual display by making the LEDs blink in
synchronization. The blinking duration of each LED is controlled by the iTime
variable, which can be used to adjust the animation’s speed.

141
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o As soon as the program starts, a variable named iTime is defined, and a value
of 200 milliseconds is assigned. This variable sets the standard duration for
delays to be used throughout the program.
2. Setup Function:
o The ‘setup()’ function is used for the Arduino’s initial configuration. This
function runs only once when the Arduino is powered up or reset.
o In this section, digital pins 6 to 13 are set to output mode. These pins are where
the LEDs are connected.
3. Main Loop Function - loop():
o The ‘loop()’ function is the main part of the Arduino program, and the code
here operates in a continuous loop.
o A variable named iPinIki is defined and set to 13. This variable is used in
controlling the LEDs.
4. LED Turning On Cycle:
o The first for loop sequentially turns on the LEDs connected to pins starting
from 6 to 10 (making them HIGH). Simultaneously, another LED, controlled
using the iPinIki variable, is turned on. In every cycle, the value of iPinIki is
decreased, thus switching on a different LED.
o After each LED is turned on, the program is paused for a certain period with
the delay(iTime) function. This ensures the LEDs remain lit for a visibly
appreciable duration.
5. Transition Delay:
o After all LEDs are on, the program is paused for a longer period with the
delay(2*iTime) function. This provides a delay in the transition between the
blinking LEDs.
6. LED Turning Off Cycle:
o The iPinIki variable is reset to 13. The second for loop sequentially turns off
the same LEDs (making them LOW). Simultaneously, another LED, controlled
by the iPinIki variable, is turned off. With each cycle, the value of iPinIki is
decreased, thus switching off a different LED. o After each LED is turned off,
the program is paused for a certain period with the delay(iTime) function.
7. End of the Cycle and Repetition:
o At the end of the cycle, the end of the loop() function is reached, and the
Arduino automatically returns to the start of the loop() function, thereby
continuously repeating the cycle. The program operates by continually
repeating these steps. This process continues until the Arduino’s power is cut
or another program is loaded.

142
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. In what real-world scenarios do you think this program could be used? What could be
alternative applications?
2. What kind of changes do you expect if you change the value of the iTime variable in
the program, and how would these changes affect the user experience?
3. If this system were to go through a debugging process, what methods would you use,
and what would the potential errors be?
4. What other electronic components could be used in place of the LEDs used in the
program, and how would this change affect the overall functioning of the program?
5. What are your thoughts on the efficiency of this code in terms of energy consumption?
What optimizations would you suggest to reduce energy consumption?
6. What sensors or input devices could be added to make this program more interactive?
How would this addition affect the overall operation of the system?
7. What methods could you use to dynamically change the delay times in the program?
What role would feedback from users play in this process?
8. What can you say about the overall reliability and stability of this program? What
steps would you take to make the system more reliable?
9. Are there any security vulnerabilities in the current code? If so, how would you
address these vulnerabilities, and what are your remediation strategies?
10. If you wanted to expand this project and make it more complex, what features or
components would you consider adding, and how would this expansion affect the
overall system performance?

143
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. This program can be used in areas such as amusement parks, advertising billboards, art
installations, or interactive displays. Alternatively, it can also be used in educational
projects or simple alert systems.
2. Changing the value of the iTime variable will change the blinking speed of the LEDs.
A shorter time will cause the LEDs to blink faster, while a longer time will make them
blink more slowly. This could be used to control the speed of a visual performance.
3. During the debugging process, I could use the serial monitor to check outputs, observe
whether the LEDs are behaving as expected, and perform a systematic review of the
code to identify potential errors.
4. Other electronic components, such as buzzers, motors, or different lights (e.g., RGB
LEDs), could be used instead of LEDs. This change would alter the program’s
functionality and the interaction experienced by the user.
5. The energy consumption of the code depends on the number of LEDs used and their
illumination duration. To reduce energy consumption, we could decrease the
brightness of the LEDs, turn off the LEDs when not in use, or use more energy-
efficient components.
6. For a more interactive experience, input devices like motion, sound, light sensors, or
buttons could be added. This enhances user interaction with the system and can create
different responses based on various inputs.
7. To dynamically change delay times, we could use a potentiometer or make
adjustments based on feedback received from users. This allows us to control how the
system responds in real-time.
8. The reliability of the system depends on the hardware used and the software of the
code. To increase reliability, it is important to prevent overloading, manage cables
properly, and fix bugs in the program.
9. Security vulnerabilities are typically more prominent in systems connected to the
internet. If this system is not connected to the internet, the risk of security
vulnerabilities is low. However, sabotage through physical access could be possible.
10. To expand the system, we could add wireless control, remote access, more LEDs, or
different visual effects. As the complexity of the system increases, managing the load
on performance and maintaining system stability may require more resources.

144
Microcontrollers for Steam-AI Students Kamil Bala

1.2.19. LEDs that gather from outside to inside and fade from inside to
outside

https://www.tinkercad.com/things/9gU9RNQrMb4-1219leds-that-gather-from-
outside-to-inside-and-fade-from-inside

/**************************************************************************
***
Program Name: Program that lights up LEDs from the outside inward
in a summative manner and then extinguishes them from
the inside outward.

Program Purpose: We ensure that the LEDs light up and go out in the order
below:
6 13
6 7 12 13
6 7 8 11 12 13
6 7 8 9 10 11 12 13
6 7 8 11 12 13
6 7 12 13
6 13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime = 300; // Variable 'iTime' defining the delay

void setup()
{
// We execute the function by increasing the pins from 6 to 13 by 1
for(int iPin = 6; iPin < 14; iPin++)
{
// We assign the current content of iPin as an output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
int iPinIki = 6; // We define a starting value for the second LED

for(int iPin = 13; iPin > 9; iPin--)


{
digitalWrite(iPin, HIGH); // We make the current value of iPin
logically HIGH
digitalWrite(iPinIki, HIGH); // We make the current value of iPinIki
logically HIGH
delay(iTime); // We provide a delay of 'iTime'

145
Microcontrollers for Steam-AI Students Kamil Bala

iPinIki++; // We decrease the current value of iPinIki by one


}

delay(2 * iTime); // We provide a delay of 2*iTime

iPinIki = 9;
for(int iPin = 10; iPin < 14; iPin++)
{
digitalWrite(iPin, LOW); // We make the current value of iPin logically
LOW
digitalWrite(iPinIki, LOW); // We make the current value of iPinIki
logically LOW
delay(iTime); // We provide a delay of 'iTime'
iPinIki--; // We decrease the current value of iPinIki by one
}

delay(2 * iTime); // We provide a delay of 2iTime


}

146
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino code allows for the control of multiple LEDs that follow a specific sequence.
The program manages the synchronized blinking of LEDs within a specific time interval. In
this particular case, LEDs light up and go off in a specific order within a sequence, creating a
visual effect.
The basic structure of the program is as follows:
1. Global Variable Declaration: A variable named ‘iTime’ is defined, specifying the
delay time (in milliseconds) between LEDs. In this instance, the delay time is 200 ms.
2. Setup Function: This function runs only once when the program starts. Here, digital
pins (ranging from 6 to 13) are initialized in output mode because they will control the
LEDs attached.
3. Loop Function: This is the continuous loop that Arduino runs and contains the main
logic of the program. In this specific snippet of code, the blinking of LEDs is managed
in two arrays:
o The first loop activates the LEDs from the 13th pin to the 10th, and
simultaneously, another set of LEDs starting from the 6th pin in ascending
order are set to high (on) status. Each iteration includes a delay for the ‘iTime’
value.
o Then, a specific delay (2_iTime) occurs. This keeps the LEDs on for some
time.
o The next loop manages the turning off of LEDs. This time, LEDs from the
10th to the 14th pin and another set in the reverse order are set to low (off)
status. This process also happens with a delay of ‘iTime.’
o Finally, the loop ends with another 2_iTime delay, and the loop returns to the
beginning.
This program provides an algorithm for the synchronized blinking of LEDs, which can be
useful in various entertainment and visual systems. Especially in this scenario, the LEDs blink
on and off in an alternating manner, creating an interesting visual effect.

147
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o int iTime = 200;: This line defines the delay time between LEDs in
milliseconds. There will be a 200 ms delay between each LED operation.
2. Setup Function:
o void setup() { … }: This function runs once when the program is loaded. In
this section, the configuration of the pins to be used is carried out.
▪ Setting pins as output: The command pinMode(iPin, OUTPUT);
initializes the pins from 6 to 13 in output mode; these pins will be used
to control the LEDs.
3. Loop Function:
o void loop() { … }: This function is the main cycle that runs continuously on
the Arduino. Here, it is determined how and when the LEDs will blink.
▪ int iPinTwo = 6;: This variable defines the starting pin of another set of
LEDs that blink in synchronization.
▪ First for loop: This cycle turns on the LEDs from the 13th pin to the
10th in sync with iPinTwo. With each LED ignition, the program waits
for ‘iTime’ milliseconds (providing a delay). In each iteration, the
iPinTwo variable is increased by one, allowing the next LED in the
other set to light up.
▪ First delay: The command delay(2_iTime); is used to keep the LEDs on
for a while. This specifies the duration the LEDs stay lit.
▪ iPinTwo = 9;: The value of the iPinTwo variable is updated to
synchronize correctly in the next cycle.
▪ Second for loop: This cycle manages turning off the LEDs. Contrary to
the first cycle where LEDs were lit, this one turns them off from the
10th pin to the 14th. Again, there is a wait of ‘iTime’ milliseconds with
each iteration, and the iPinTwo variable is decreased by one.
▪ Second delay: The command delay(2_iTime); is used to keep the
LEDs off for a while. This determines the duration the LEDs stay off.
4. Repetition of the Cycle:
o At the end of the loop() function, control returns to the beginning of the loop()
function. This is a continuous cycle that the Arduino maintains as long as the
program is loaded, allowing the LEDs to continue blinking in the specified
pattern.

148
Microcontrollers for Steam-AI Students Kamil Bala

Questions Related to the Circuit:


1. Are there any mistakes or deficiencies in the current state of the program? If so, how
can they be corrected?
2. What effect would changing the value of the iTime variable have on the blinking
speed of the LEDs?
3. In the current code, if you want to change the order or pattern of the LEDs lighting up,
what changes would you need to make?
4. What can be said about the overall efficiency and performance of the code? What
steps could be taken to improve it?
5. In a larger project where this code is a part, what kind of functionality or features
might you want to add?
6. If this system behaves erratically or encounters an error, how is this situation detected,
and what kind of debugging process should be followed?
7. Are there any security risks in the current code? If so, how can these risks be
minimized?
8. If you want to integrate a real-time clock or timer into the system, how could you add
this functionality?
9. If this code were part of a product, what features or settings accessible by users might
you want to add?
10. If you wanted to scale this project, meaning adding many more LEDs or different
types of hardware components, what changes would be necessary in the current code
structure?

149
Microcontrollers for Steam-AI Students Kamil Bala

Answers:
1. There are no apparent errors or deficiencies in the current state of the program.
However, as with any program, changes and improvements can be made according to
different scenarios and requirements.
2. The value of the iTime variable determines how long the LEDs stay on (or off). The
higher the value, the longer the LEDs stay lit, and vice versa.
3. To change the order or pattern in which the LEDs light up, we could alter the sequence
of iterations in the loops (for loops) or the digitalWrite commands within the code.
4. The code is quite straightforward and written in a comprehensible manner, so its
overall performance is good. However, in more complex systems or situations where a
larger number of LEDs need to be controlled, different programming techniques or
data structures could be employed to enhance the code’s efficiency.
5. In a larger project, features such as a user interface for custom effects or control of the
LEDs, timing settings, brightness control, etc., could be added.
6. Error detection and debugging typically involve a thorough examination of the code,
using debugging tools, and perhaps step-by-step testing of the system. System logs or
error reports could also be utilized to identify the sources of unexpected behaviors.
7. The current code essentially provides a simple LED control mechanism and doesn’t
carry significant security risks. However, it’s always important to take hardware
protection measures when working with any electronic circuit.
8. Real-time clock or timer functionality can usually be achieved by integrating a clock
module and adding code segments triggered at specific times.
9. For users, features could be adjustable, such as the colors of the LEDs, their lighting
durations, patterns, or animations. Additionally, interactive features like alerts or
notifications for specific events could be considered.
10. Scaling the project requires adding more LEDs or different components. This typically
involves reorganizing the code, programming additional hardware interfaces, and
possibly managing additional power requirements.

150
Microcontrollers for Steam-AI Students Kamil Bala

1.2.20. Randomly Blinking LEDs

/*************************************************************************
Program Name: Randomly Blinking LEDs

Program Purpose: We enable the LEDs to blink randomly.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int iTime=300; // The variable iTime defining the delay

void setup()
{
// Execute the function by incrementing pins from 6 to 13

for(int iPin=6; iPin<14; iPin++)


{
// We assign the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
int iPin=random(6,13);

digitalWrite(iPin, HIGH);
delay(iTime);

151
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(iPin, LOW);
delay(iTime);

}}

152
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program creates a random LED blinking pattern with a certain delay duration.
Here is a general summary of the program:
1. Variable Definition:
o iTime: This variable is used to determine how long the LEDs will blink (in
milliseconds). In this example, there is a 300 ms delay between each blinking
operation.
2. Setup Function:
o This function runs once when the program is loaded. In this example, digital
pins from 6 to 13 are set as output. These pins are where the LEDs are
connected.
3. Main Loop (loop) Function:
o The loop function is the part that the Arduino continuously runs, and this is
where the main operations occur.
o First, the random(6, 13) function picks a random number between 6 and 13.
This number represents the pin number of the LED to be activated.
o The digitalWrite(iPin, HIGH) command sets the randomly selected pin to high
voltage (HIGH), i.e., turns on the corresponding LED.
o Then, it waits for the duration specified by the delay(iTime) function; during
this time, the LED stays on.
o The digitalWrite(iPin, LOW) command sets the pin to low voltage (LOW), i.e.,
turns the LED off.
o Again, it waits for the same duration with the delay(iTime) function; during
this time, the LED stays off.
o These processes are continuously repeated, thus creating a random blinking of
LEDs. This program creates a visual effect by randomly and continuously
turning the connected LEDs on and off. The blinking duration of each LED is
controlled by the iTime variable. This simple structure can be used as a visual
output in interactive projects.

153
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:
1. Variable Definition:
o At the start of the program, a variable named iTime is defined, and its value is
set at 300. This determines the delay duration in milliseconds and controls how
long the LED will blink.
2. Setup Function:
o The setup function is executed once immediately after the Arduino starts.
o In this part, digital pins from 6 to 13 are set to output mode. This process is
done using the pinMode function and is set individually for each pin.
3. Loop Function:
o The loop function is the continuously operating section in the Arduino and
makes up the main part of the program. The codes written within this function
are executed repeatedly for as long as the Arduino is connected to a power
source. Step 1: Random Pin Selection
o The iPin variable is set to a random value between 6 and 13 using the
random(6, 13) function. This random process determines which LED will light
up. Step 2: Turning on the LED o The digitalWrite(iPin, HIGH) command
brings the selected pin to a high state (HIGH). This action turns on the LED
connected to the selected pin. Step 3: Delay
o The delay(iTime) function pauses the program for a certain period. In this
example, this duration is 300 milliseconds, as defined by the iTime variable.
The LED remains on during this period. Step 4: Turning off the LED
o The digitalWrite(iPin, LOW) command brings the selected pin to a low state
(LOW). This action turns off the LED connected to the selected pin. Step 5:
Delay
o The delay(iTime) function causes the program to pause again for a certain
period after the LED is turned off. This determines how long the LED will stay
off after it is extinguished.
4. Repetition of the Cycle:
o The steps within the loop function are continuously repeated. This leads to the
LED randomly and continuously blinking. The program provides a visual
effect and user interaction by establishing a simple random blinking LED
pattern. In each step, the lighting or turning off of the LED is controlled with a
certain delay duration.

154
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. If the value of the iTime variable is changed, what kind of impact does it create on the
blinking speed of the LED, and what kind of changes could this bring in terms of user
experience?
2. What is the purpose of the random function used in the program, and what is its effect
on LED selection?
3. How does the digitalWrite function work, and what are the technical details related to
this function’s ability to control an LED?
4. What do the HIGH and LOW commands used in Arduino signify in terms of the
working principles of electronic components?
5. If it is desired to control multiple LEDs simultaneously, how could this program be
modified?
6. What is the efficiency of this program in terms of energy consumption, and what
optimizations could be made to save energy?
7. What are the effects of the delay function used in the program on the system, and are
there any alternative approaches that could replace this function?
8. What practical applications could this kind of LED blinking arrangement have in the
real world?
9. How is the debugging process of the program managed, and what potential errors
could be encountered in the program?
10. If this program is expanded and made part of a more complex lighting system, what
additional features and functions could be integrated?

155
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The value of the iTime variable directly affects the duration of the LED’s blinking. A
lower value causes the LED to blink faster, while a higher value results in a slower
blinking process. From a user experience standpoint, different blinking speeds may be
more appropriate for different applications and environments.
2. The random function generates a random number within a specified range. In this
case, it selects a random LED, creating an unpredictable pattern of blinking. This is
useful for creating aesthetic or attention-grabbing visual effects.
3. The digitalWrite function sends a specific voltage (HIGH or LOW) to a specific pin.
HIGH typically means 5V, and LOW means 0V. This function allows control over
electronic components, such as turning an LED on or off.
4. HIGH and LOW are terms frequently used in electronics. HIGH is often used when
power is applied to a circuit, and LOW when power is cut off. In Arduino, HIGH
signifies that positive voltage is being applied to the pin (typically 5V), and LOW
indicates that no voltage is being applied (0V).
5. To control multiple LEDs, an array or list can be used within the program, and the
digitalWrite function can be called for each LED through a loop.
6. The program’s energy consumption depends on the hardware used and the blinking
duration of the LED. For energy savings, the burning duration of the LEDs can be
reduced, or low-power LEDs can be used.
7. The delay function causes the program to pause for a specific period. However, during
this time, the Arduino cannot do anything else. Alternatives include timing using the
millis function or using a timer interrupt.
8. Such an LED blinking arrangement can be used in various applications, including
decorative lighting, visual alert signals, entertainment, and artistic performances.
9. The debugging process typically involves printing error messages over the serial port
and observing the program’s expected behavior. Potential errors may include hardware
failures, incorrect pin connections, or coding errors.
10. Expanding the program could involve adding colored LEDs, sound response
capabilities, motion sensors, or other interactive features. This could offer a more
dynamic and interactive experience for users or viewers.

156
Microcontrollers for Steam-AI Students Kamil Bala

1.2.21. Traffic Light Simulation Program

https://www.tinkercad.com/things/lhd1vRDOA6W-1221traffic-light-simulation-program

/*************************************************************************
Program Name: Traffic Light Simulation Program

Program Purpose: To simulate the daily operation of traffic lights

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
// Assigning pin variables for pedestrians
int pedestrianGreen=9, pedestrianRed=10;

// Assigning pin variables for vehicles


int vehicleGreen=11, vehicleYellow=12, vehicleRed=13;
void setup()
{
for(int iPin=13; iPin>8; iPin--)
{
pinMode(iPin, OUTPUT); // we set the current iPin value as output
}

void loop() {

// Green light for pedestrians

digitalWrite(vehicleRed, HIGH); // we light up red for vehicles


digitalWrite(pedestrianGreen, HIGH); // we light up green for pedestrians
digitalWrite(pedestrianRed, LOW); // we turn off red for pedestrians
digitalWrite(vehicleYellow, LOW); // we turn off yellow for vehicles
delay(5000); // we provide a delay of 5 seconds (5000ms)

// Red for pedestrians, yellow for vehicles

digitalWrite(vehicleRed, LOW); // we turn off red for vehicles


digitalWrite(pedestrianGreen, LOW); // we turn off green for pedestrians
digitalWrite(vehicleYellow, HIGH); // we light up yellow for vehicles
digitalWrite(pedestrianRed, HIGH); // we light up red for pedestrians
delay(2000); // we provide a delay of 2 seconds (2000ms)

// Free passage for vehicles

digitalWrite(vehicleYellow, LOW); // we turn off yellow for vehicles


digitalWrite(vehicleGreen, HIGH); // we light up green for vehicles
delay(10000); // we provide a delay of 10 seconds (10000ms)

157
Microcontrollers for Steam-AI Students Kamil Bala

// Yellow for vehicles to stop

digitalWrite(vehicleGreen, LOW); // we turn off green for vehicles


digitalWrite(vehicleYellow, HIGH); // we light up yellow for vehicles
delay(2000); // we provide a delay of 2 seconds (2000ms)

158
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino code represents a simulation of a traffic light control system and manages light
changes for both pedestrians and vehicles. To ensure and regulate safety in traffic, different
lights (green, yellow, red) turn on or off at specific time intervals. Separate LEDs for
pedestrians and vehicles are used to simulate traffic signals for both scenarios. Here is a
general summary of the program:
1. Initial Settings (Setup):
o First, various pins are set to output mode. These pins represent LEDs of
different colors, each associated with a traffic signal (green, yellow, or red).
o At this stage, the system is initiated and prepared to control the traffic lights.
2. Main Cycle (Loop):
o In the first phase of the cycle, the green light for pedestrians turns on,
indicating that pedestrians may cross. Simultaneously, the red light for vehicles
is on, indicating that vehicles must stop.
o In the second phase, the red light for pedestrians and the yellow light for
vehicles turn on, indicating that vehicles should be ready to move but not start
moving yet.
o In the third phase, the green light for vehicles turns on, allowing them to pass.
During this time, the red light remains on for pedestrians.
o In the fourth phase, the yellow light for vehicles turns on, indicating that they
should prepare to stop. This part occurs just before the end of the green light
and serves as a warning for vehicles to prepare to stop.
o There is a specific “delay” period between each phase. This simulates the real-
world traffic lights, which wait a certain amount of time between each light
sequence. This program mimics the basic operation of a real traffic light
system and can be used for traffic safety education, engineering projects, or
traffic-related simulations.

159
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

Since this program runs on an Arduino control circuit, it cannot be simulated or executed
step-by-step in real-time. However, I can explain the logic of the program and what happens
at each step. I can explain the steps of the program and what happens in each as follows:
1. Setup Function:
o When the program starts, the setup() function is automatically called. This
function is typically used to set the initial states of the pins.
o In this case, the setup() function sets the pins used to control the lights to
output mode. This means that the control device can send electrical signals
through these pins to control the LEDs.
2. Main Loop - Green Light for Pedestrians:
o Within the loop() function, the first step is to turn on the green light for
pedestrians and the red light for vehicles. This indicates that pedestrians can
cross and vehicles must wait.
o This situation continues for a certain period (e.g., 5 seconds).
3. Red for Pedestrians, Yellow for Vehicles:
o In the next step, the red light turns on for pedestrians, indicating that it is no
longer safe to cross. At the same time, the yellow light for vehicles turns on,
indicating that drivers need to be cautious and will move soon.
o This situation also continues for a certain period (e.g., 2 seconds).
4. Green Light for Vehicles:
o Next, the green light turns on for vehicles. This indicates that drivers can pass.
The red light stays on for pedestrians during this time, indicating that they
must wait.
o The green light for vehicles continues for a certain period (e.g., 10 seconds).
5. Yellow Light for Vehicles:
o After the end of the green light, the yellow light for vehicles turns on again.
This indicates that drivers need to slow down and stop because the red light
will turn on soon.
o The yellow light stays on for a short period (e.g., 2 seconds). These steps are
continuously repeated within the Arduino’s loop() function, thus creating a
traffic light system simulation. For this code to work on actual hardware, the
necessary electronic components (e.g., LEDs, resistors, cables, and of course,
an Arduino) are required.

160
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. How do you think this code could affect traffic flow if implemented in real-world
scenarios?
2. How can the delay times (delay) in the program be optimized, and what are the
impacts of these times on traffic?
3. What features could be added to make this system more flexible (e.g., sensors,
artificial intelligence algorithms, etc.)?
4. What safety measures are critical to adopt in such a system, and how can they be
reflected in the code?
5. What could be the implications of this program in terms of energy consumption and
sustainability?
6. How can we detect and manage failures or unexpected situations that may occur in the
system?
7. Besides traffic lights, what other applications do you think this code could be used
for?
8. How can we address real-world variables such as weather conditions, road works, or
emergencies in this system?
9. How can the program’s user interface and interaction be improved? For example,
could a screen displaying real-time traffic data be added?
10. How is scalability achieved in such a system? What adaptations can be made in big
cities or areas with different traffic patterns?

161
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. This code automates the control of traffic lights, regulating traffic flow. By providing
smooth traffic and reducing waiting times at intersections, it could potentially reduce
traffic congestion and, consequently, emissions.
2. Delay times can be optimized by considering traffic density, time zones (e.g., rush
hours, end of work shifts), and special events. The impact on traffic could be more
orderly traffic flow and reduced risk of accidents.
3. The system can be made more flexible by adding features such as sensors, artificial
intelligence, priority for emergency vehicles, weather information, and traffic density
data.
4. As safety measures, automatic red light activation in case of failure, backup power
sources, regular system checks, and error notifications are important. These measures
ensure greater resilience of the system.
5. Concerning energy consumption, using LED lights and activating lights only when
necessary with sensors can save energy. For sustainability, renewable energy sources
like solar energy could be used.
6. The system should be continuously monitored to detect failures, and it should respond
quickly to any anomalies. Emergency protocols and automatic recovery solutions can
be integrated for unexpected situations.
7. This code can be used for railroad crossings, pedestrian crossings, bicycle paths, or
even certain industrial control systems.
8. For weather conditions and emergencies, real-time data integration, priority passage,
and special modes can be added to the system.
9. The user interface could include information on real-time traffic flow, light status, and
system status. Additionally, remote control and adjustment capabilities could be
included.
10. Scalability can be achieved with a central control system, data analytics, machine
learning models, and IoT integration. The system can be dynamically adjusted to
accommodate different traffic patterns and intensities.

162
Microcontrollers for Steam-AI Students Kamil Bala

1.2.22. Intersecting LEDs


https://www.tinkercad.com/things/6LDB9AMxNYK-1222intersecting-leds

/*************************************************************************
Program Name: Intersecting LEDs

Program Purpose: To facilitate the intersection of two counters

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova Deneyap Workshop / 2022
***************************************************************************
***/

void setup()
{
// We execute the function by decrementing the pins from 13 to 6
for(int iPin=13; iPin>5; iPin--)
{
// We set the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}

void loop()
{
int delayTime=100; // The variable 'delayTime' defining the delay

for(int iPin=6, iPin2=13; iPin<14; iPin++, iPin2--)


{
digitalWrite(iPin, HIGH);

163
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(iPin2, HIGH);
delay(delayTime);
digitalWrite(iPin, LOW);
digitalWrite(iPin2, LOW);
delay(delayTime);

}
delay(2*delayTime);

164
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program is written to control a series of LEDs. These LEDs are controlled from
specific digital pins physically connected to the Arduino board. The program facilitates the
blinking of the LEDs in a pattern, creating a visual effect or display. Here’s a general
summary of the tasks the program performs:
1. Setup Function: At the beginning of the program, it sets certain pins of the Arduino
(in this case, pins 6 to 13) to output mode. This means that these pins can send
electrical signals to control the connected LEDs. This setting is done once for each pin
as it determines the initial configuration of the hardware.
2. Loop Function: This is the part that Arduino continuously runs, and here the blinking
pattern of the LEDs is established. o Firstly, it defines a variable called iTime, which
determines how long the LEDs will stay on (or off) in milliseconds.
o Then, the program controls the LEDs on two different pins (iPin and iPin2)
simultaneously. These pins are counted from 6 to 13 and from 13 to 6,
respectively, meaning one increases while the other decreases. This creates a
kind of “confrontation” pattern for the LEDs.
o In each cycle, a specific pair of LEDs (iPin and iPin2) is turned on for a certain
period (iTime), then turned off. This process is repeated for all pins (and
therefore LEDs).
o Finally, a short delay (2*iTime) is added before moving to the next cycle. This
program enables the LEDs to blink in synchronization, providing a simple light
show. Each pair of LEDs moves in opposite directions, creating a dynamic
visual effect. Such projects are often used in education, entertainment, or DIY
(do-it-yourself) electronic projects.

165
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Setup Function:
void setup() { // We execute the function by decreasing the pins from 13 to 6
for(int iPin=13; iPin>5; iPin–) { // We assign the current content of iPin as the output
pin pinMode(iPin, OUTPUT);
}}
o The setup function runs once with the startup of Arduino. It is used to initiate
hardware settings.
o The for loop iterates for each pin, starting from pin 13 and moving to the
previous pin (down to 6).
o In each loop iteration, the pinMode function takes the current iPin number and
sets the mode of that pin to OUTPUT. This means the Arduino can control the
LEDs attached to this pin by sending power through it.
2. Loop Function:
void loop() { int iTime=100; // The iTime variable defining the delay
for(int iPin=6, iPin2=13; iPin<14; iPin++, iPin2–) {
digitalWrite(iPin, HIGH);
digitalWrite(iPin2, HIGH);
delay(iTime);
digitalWrite(iPin, LOW);
digitalWrite(iPin2, LOW);
delay(iTime); } delay(2_iTime);
}`
o The loop function is the part that runs continuously on the Arduino. Here, the
blinking behavior of the LEDs is controlled.
o The iTime variable is the delay period (in milliseconds) determining how long
the LEDs will stay on/off.
o Next, a for loop is initiated. This loop iterates using two variables (iPin and
iPin2). iPin increases from 6 to 13, while iPin2 decreases from 13 to 6. This
simulates the LEDs “moving” in opposite directions.
o Within the loop, both pins are set to HIGH status using the digitalWrite
function, causing the LEDs to light up. Then, the program is paused for a
certain period (iTime) with the delay function.
o The same pins are set to LOW status with digitalWrite, causing the LEDs to
turn off. Again, the program waits for a certain period with the delay function.

166
Microcontrollers for Steam-AI Students Kamil Bala

o After the complete blinking process for all LEDs, the program is paused for an
extra period with delay(2_iTime). This creates a significant “off” period
between the blinking of the LEDs. These steps demonstrate how the Arduino
manages power and creates a visual effect by ensuring the LEDs blink in
synchronization. Each cycle ensures a sequence of LEDs blinks before the next
cycle begins.

167
Microcontrollers for Steam-AI Students Kamil Bala

Questions Regarding the Circuit:

1. What is the general purpose of the program, and what kinds of scenarios could be
simulated with this type of LED arrangement?
2. What is the impact of the iTime variable on the program, and how do different values
affect this simulation?
3. What are the advantages and disadvantages of controlling each LED individually in
this code?
4. What modifications could be made to ensure the LEDs flash simultaneously in this
simulation?
5. What improvements could be made to dynamically change the blinking duration of the
LEDs in the current code?
6. If an error occurs in the code, what steps could be followed to resolve it?
7. What is the impact of this program on energy consumption, and what could be done to
increase energy efficiency?
8. How could the choice of hardware used in this project (e.g., LEDs, resistors, etc.) be
optimized?
9. How would the meaning or purpose of the simulation change if the LEDs lit up in
different colors?
10. How could we expand this simulation into a more complex system (for example, by
adding sensors or input devices), and how would this expansion change potential
usage scenarios?

168
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The program aims to create a sort of “racing” effect by controlling two sets of LEDs in
opposition. This could be used for scenarios like entertainment light shows or visual
alert systems.
2. The iTime variable determines how long the LEDs stay on (or off). As the value
increases, the LEDs stay on for a longer period, creating a slower blinking effect.
3. Controlling each LED individually provides the flexibility of managing the state of
each precisely. However, if numerous LEDs need to be controlled simultaneously, this
method could increase the complexity of the code and the potential for errors.
4. To make the LEDs flash simultaneously, the program would need to be altered to turn
on and off every LED at the same time. This is typically done using digitalWrite
commands for all pins within a loop.
5. To dynamically change the blinking duration of the LEDs, we could adjust the iTime
variable using user input or data from a sensor. For instance, we could allow users to
set the blinking speed with a potentiometer.
6. If an error occurs in the code, the first step is to diagnose the error. Then, we should
determine the source of the error by examining the code and evaluating error
messages. Finally, we can rectify the error by making appropriate corrections and
retesting the code.
7. The program’s energy consumption is directly related to the number of LEDs used and
how long they are kept on. We could increase energy efficiency by reducing the
brightness of the LEDs or shortening the on-off times.
8. The choice of hardware can be optimized based on the project’s requirements. For
instance, energy-efficient LEDs could be chosen for low power consumption, or LEDs
of different colors could be used for a specific visual effect.
9. If the LEDs light up in different colors, the purpose of the simulation might change.
For example, it could represent specific scenarios like traffic lights, warning signals,
or atmospheric lighting.
10. To expand the simulation, we could add additional components like motion sensors,
sound devices, or interactive control mechanisms. This could lead to new usage
scenarios, such as environment-responsive lighting, interactive art installations, or
educational simulations.

169
Microcontrollers for Steam-AI Students Kamil Bala

1.2.23. 3-part snake roaming LEDs 2-13 sequentially


https://www.tinkercad.com/things/katorYzPe5o-1223-3-part-snake-roaming-leds-2-13-
sequentially

/**************************************************************************
***
Program Name: 3-part snake roaming LEDs 2-13 sequentially

Program Objective: We are lighting up the LEDs in the sequence below and
ensuring they all turn off simultaneously:
2
2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 10
9 10 11
10 11 12
11 12 13
12 13
13

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int delayTime=500; // Variable 'delayTime' defining the delay

void setup()
{
// We execute the function by incrementing pins from 2 to 13
for(int iPin=2; iPin<14; iPin++)
{
// We assign the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
// We set up counters so that the snake appears to have a head at the
start and a tail at the end as it progresses

for(int iPin=-1; iPin<14; iPin++)


{

170
Microcontrollers for Steam-AI Students Kamil Bala

digitalWrite(iPin, HIGH); // We set the current iPin value to logical


HIGH
digitalWrite(iPin+1, HIGH); // We set the current iPin value to logical
HIGH
digitalWrite(iPin+2, HIGH); // We set the current iPin value to logical
HIGH
delay(delayTime); // We cause a delay for 'delayTime'

for(int jPin=2; jPin<14; jPin++)


{
digitalWrite(jPin, LOW); // We set the current iPin value to logical
LOW
digitalWrite(jPin+1, LOW); // We set the current iPin value to
logical LOW
digitalWrite(jPin+2, LOW); // We set the current iPin value to
logical LOW
}
delay(delayTime); // We cause a delay for 'delayTime'
}
}

171
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program aims to create a moving “snake” effect using a series of LEDs. The
program achieves this visual effect by sequentially lighting and extinguishing the LEDs with a
specific delay (in this case, 500 milliseconds between each movement). This makes the LEDs
move like a “snake.” Each LED represents a segment of the snake, and the program controls
the LEDs in sequence to create this effect.
The structure of the program is as follows:
1. Setup: At the start of the program, the Arduino pins to be used as output pins are
defined. In this example, pins 2 through 13 are set in output mode. These pins serve as
the connectors for the LEDs later on.
2. Main Loop (Loop): The main loop is where the snake animation occurs. In this loop,
the program sequentially lights the LEDs representing the “head” and “tail” of the
“snake.”
o The first cycle creates the “head” of the snake. As the snake moves, the LEDs
representing its head light up in sequence.
o Then, each LED stays lit for a certain time (defined by the iTime variable).
o During this, the “tail” of the snake is extinguished. This involves lighting the
next group of LEDs and extinguishing the previous group, giving the
impression that the snake is advancing.
o The program repeats this cycle to ensure the snake passes through the entire
LED array. This program can be used in various applications, such as
interactive visual effects, games, decorative lighting, or educational DIY
projects.

172
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

This program creates a “snake” effect by controlling an array of LEDs. Each step operates
independently from the previous one and is executed within a continuous loop. Here’s a step-
by-step explanation:
1. Initial Settings (Setup):
o When the program starts, the Arduino’s pins from 2 to 13 are set as outputs. These
pins are connected to the LEDs, allowing them to be controlled.
2. Main Loop (Loop):
o The main part of the program occurs here, where the snake effect is created.
First, the program initiates a loop. This loop starts at -1 and continues to 13.
However, since valid pin numbers are between 2 and 13, nothing happens at
negative indexes (i.e., no LED is lit during the first iteration).
3. Creating the Snake:
o In the second loop, two LEDs light up and go off in each iteration. This is done
using the digitalWrite function. This function lights or extinguishes the
relevant LED by raising (HIGH state) or lowering (LOW state) the voltage on
a specific pin. o The “snake” effect is created by three consecutive LEDs
lighting up and being extinguished in sequence. The first group of LEDs lights
up (HIGH), stays in this state for a certain time (defined by the iTime
variable), and then is extinguished (LOW).
o This process is repeated until the iPin variable reaches 14. This means that all
the LEDs from the beginning to the end of the snake are lit and extinguished in
sequence.
4. Delay Period:
o After each iteration, the program calls the delay(iTime) function, waiting for a
certain period. This is necessary for the snake effect to be visually perceivable.
During this time, no new LED lights up or is extinguished. o Finally, the
nested loop is used to reset the LEDs. This represents the “exit” of the snake’s
“tail” from the LED array.
5. Resetting and Restarting the Snake:
o After the entire cycle is completed, the delay(2*iTime) function pauses the
program to wait before the next “snake” effect begins.
o This process is repeated continuously until the Arduino is stopped, thereby
continuously displaying the snake effect. The program creates the illusion of
movement through the continuity of transitions between LEDs. This simple
animation technique gives the user the impression that the LEDs are moving
like a snake.

173
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. What is the main purpose of the program, and what strategies have been employed to
achieve this purpose?
2. How does the iTime variable affect the program, and what would happen if the value
of this variable is changed?
3. What is the role of the loops (loop) used in the program, and how would the behavior
of the program be affected without these loops?
4. What is the role of the digitalWrite function, and how would LED control be achieved
without this function?
5. Why were the pin numbers specified in the program chosen? What adjustments would
be needed if different pins were used?
6. What is the purpose of the delay calls in the program? How would the program behave
without these calls?
7. Is there any error management or return strategy in the event of an error in the
program? What are the advantages of such an approach?
8. What is the relationship between the setup and loop methods, and what are the effects
of these two methods on the overall operation of the program?
9. If you wanted to change certain aspects of the program, such as the order or duration
in which the LEDs light up, which parts of the code should be changed?
10. What can be said about the energy consumption of this program, and what strategies
can be applied to increase energy efficiency?

174
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. The main purpose of the program is to control LEDs in a specific pattern. This is
achieved using various digitalWrite and delay functions to follow a specific timing
sequence.
2. The iTime variable determines how long the LEDs stay on (or off). Changing this
value will directly influence the speed at which the LEDs blink.
3. The loops enable the program to execute certain tasks repeatedly. Without these loops,
each step of the code would have to be written manually, requiring much more code.
4. The digitalWrite function is used to control the electrical state of a specific pin.
Without this function, manually altering each LED’s electrical state would be
impractical.
5. Pin numbers are often chosen based on the hardware being used. Using different pins
would typically require the program to be updated to accommodate the new pin
arrangement.
6. The delay calls allow the program to “wait” for a specified amount of time. Without
these calls, the program would run continuously and uncontrollably.
7. There isn’t a distinct error management strategy in the program. Incorporating error
handling can improve the stability and reliability of the program as it can help address
potential errors or unexpected situations.
8. The setup method runs once at the start of the program to set initial conditions. The
loop method continuously runs and constitutes the main functionality of the program.
Together, they ensure the program starts and operates correctly.
9. To change the order or duration in which the LEDs light up, modifications would be
needed in the sections of the code where digitalWrite and delay functions are used.

10. The energy consumption of the program depends on the hardware used and the
duration the LEDs remain on. To enhance energy efficiency, modifications can be
made to the code to allow the LEDs to operate for shorter durations or at
lower power.

175
Microcontrollers for Steam-AI Students Kamil Bala

1.2.24. 3-part snake roaming LEDs 13-2 sequentially

https://www.tinkercad.com/things/8dY83Vstf9d-1224-3-part-snake-roaming-leds-13-2-
sequentially

/**************************************************************************
***
Program Name: 3-part snake roaming LEDs 13-2 sequentially

Program Objective: We make the LEDs light up in the sequence described


below and ensure they all turn off at the same time.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int delayTime=500; // Variable 'delayTime' defining the delay

void setup()
{
// We execute the function by incrementing pins from 2 to 13
for(int iPin=2; iPin<14; iPin++)
{
// We assign the current content of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{
// We adjust the counters so that the snake appears to have a head at the
start and a tail at the end as it progresses

for(int iPin=15; iPin>-1; iPin--)


{
digitalWrite(iPin, HIGH); // We set the current iPin value to logical
HIGH
digitalWrite(iPin-1, HIGH); // We set the current iPin value to logical
HIGH
digitalWrite(iPin-2, HIGH); // We set the current iPin value to logical
HIGH
delay(delayTime); // We cause a delay for 'delayTime'

for(int jPin=14; jPin>-2; jPin--)


{
digitalWrite(jPin, LOW); // We set the current iPin value to logical
LOW
digitalWrite(jPin-1, LOW); // We set the current iPin value to
logical LOW
digitalWrite(jPin-2, LOW); // We set the current iPin value to
logical LOW

176
Microcontrollers for Steam-AI Students Kamil Bala

}
delay(delayTime); // We cause a delay for 'delayTime'
}
}

177
Microcontrollers for Steam-AI Students Kamil Bala

1.2.25. LED Array 1 Animation

https://www.tinkercad.com/things/erqmTCJs1gS-1225led-array-1-animation

/*************************************************************************
Program Name: LED Array 1 Animation

Program Objective: To create various animations with the LED array

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int delayDuration=500; // The variable 'delayDuration' that sets the delay

int led_1[6]= {1,3,5,7,9,11};


int led_2[6]= {2,4,6,8,10,12};

void setup()
{
// We are increasing the pins from 2 to 13 by 1 to execute the function
for(int iPin=2; iPin<14; iPin++)
{
// We set the current value of iPin as the output pin
pinMode(iPin, OUTPUT);
}
}

void loop()
{

for(int i=0; i<6; i++)


{
digitalWrite(led_1[i], HIGH); // We set the current LED array value to
logical HIGH
delay(delayDuration); // We delay for 'delayDuration'
digitalWrite(led_1[i], LOW); // We set the current LED array value to
logical LOW
delay(delayDuration); // We delay for 'delayDuration'

digitalWrite(led_2[i], HIGH); // We set the current LED array value to


logical HIGH
delay(delayDuration); // We delay for 'delayDuration'
digitalWrite(led_2[i], LOW); // We set the current LED array value to
logical LOW
delay(delayDuration); // We delay for 'delayDuration'
}

for(int j=0; j<3; j++)


{
for(int i=0; i<6; i++)

178
Microcontrollers for Steam-AI Students Kamil Bala

{
digitalWrite(led_1[i], HIGH); // We set the current LED array value
to logical HIGH
}
delay(delayDuration); // We delay for 'delayDuration'

for(int i=0; i<6; i++)


{
digitalWrite(led_1[i], LOW); // We set the current LED array value to
logical LOW
}
delay(delayDuration); // We delay for 'delayDuration'
}

for(int j=0; j<3; j++)


{
for(int i=0; i<6; i++)
{
digitalWrite(led_2[i], HIGH); // We set the current LED array value
to logical HIGH
}
delay(delayDuration); // We delay for 'delayDuration'

for(int i=0; i<6; i++)


{
digitalWrite(led_2[i], LOW); // We set the current LED array value to
logical LOW
}
delay(delayDuration); // We delay for 'delayDuration'

179
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:


This Arduino program creates a visual effect similar to a “snake game” by controlling LEDs
in a specific pattern. The program generates a moving “snake” illusion using LEDs that light
up and extinguish in a particular sequence. The structure of the program is as follows:
1. Variable Definition:
o iTime: A variable indicating the delay duration (in milliseconds) to be used in
transitions between LEDs. o led_1 and led_2: Two separate arrays containing
the pin numbers of the LEDs. These arrays specify which LEDs will flash in
what sequence.
2. Setup Function:
o The initial part where pin modes are set. Digital pins from 2 to 13 are
configured as LED outputs.
3. Loop Function:
o The main cycle of the program happens here, and control of the LEDs is
provided in this section.
o In the first cycle, the LEDs specified in the led_1 and led_2 arrays light up and
extinguish in sequence. Each LED stays on for the iTime duration and then
turns off.
o The second and third cycles control the LEDs in groups. All LEDs in the led_1
array light up for a specific period (during iTime) and then turn off. The same
process is repeated for led_2.
o These cycles create the illusion of the “snake” “moving” from one point to
another.
4. Delays:
o The command delay(iTime); ensures a certain waiting period between LED
transitions, allowing the human eye to perceive the flashing of the LEDs
clearly. The program presents a visual show with LEDs flashing in a particular
pattern, creating a simple animation. This can be particularly used in
educational projects or DIY (do-it-yourself) electronic hobbies.

180
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Defining Variables:
o iTime: This variable specifies how long the LEDs will stay on (or off). In this
example, each LED will light up for 500 milliseconds (half a second).
o led_1 and led_2: These arrays contain the pin numbers of the LEDs to be
controlled. The led_1 array includes odd-numbered pins, while led_2 contains
even-numbered pins.
2. Setup Function:
o This function runs only once when the program is loaded. All pins (from 2 to
13) are set as outputs, allowing the Arduino to power the LEDs through these
pins.
3. Loop Function:
o This function is continuously repeated as long as the Arduino is operating. The
main logic of the program is located here. First Cycle (Single LEDs Flashing):
o The variable i counts from 0 to 5 (6 steps).
o At each step, power is sent to the respective elements of led_1 and led_2 arrays
(in order), lighting up the corresponding LED.
o Each LED stays on for the duration specified in the iTime variable, then turns
off. This process is carried out by sending HIGH (on) and LOW (off) signals
with the digitalWrite function. Second Cycle (All Single LEDs Flashing):
o This cycle controls all LEDs in the led_1 array simultaneously.
o Within a loop repeated 3 times, all LEDs flash on and off at the same time.
o Each LED lights up for the iTime duration again and then turns off. Third
Cycle (All Paired LEDs Flashing):
o This cycle controls all LEDs in the led_2 array simultaneously.
o Again, within a loop repeated 3 times, all LEDs flash on and off together.
o Each LED lights up for the iTime duration again and then turns off. As a result
of these steps, a visual effect is created with individual LEDs flashing,
followed by the entire group of LEDs flashing together. This process continues
repeatedly until the Arduino is stopped, meaning the loop function operates
non-stop.

181
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. If you wanted to apply a different delay time (for example, different for each LED),
how would you change the program code, and what would be the effects of these
changes on the system?
2. Assume you need to go through a debugging process in the program. What strategies
would you use, and how would you diagnose and solve potential problems?
3. If you wanted to use a set of LEDs, each with a different characteristic (color,
brightness, etc.), instead of this LED array, how would you modify the existing code?
4. If you wanted to expand this project, making the LEDs blink in response to user input
(for example, pressing a button), what additional components would you use, and what
changes would you make to your code?
5. What can you say about the energy consumption of this program? What strategies
would you recommend to optimize energy consumption in a continuously operating
system?
6. If this system were integrated with a security system and the blinking of the LEDs was
dependent on certain security alerts, what steps would you take to ensure this
integration?
7. How would you assess whether there are any security vulnerabilities in the current
system? If so, what measures would you take to address these security vulnerabilities?
8. What are your thoughts on real-world applications of this project? What beneficial
applications could be developed for society using this technology?
9. What changes would you have to make in the program to dynamically change the
sequence or pattern of the LEDs blinking in the system?
10. If you wanted to use this project as an educational tool, how would you use it to teach
students basic principles, and what teaching methods would you adopt in this process?

182
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. To apply different delay times, I could define a separate delay time variable for each
LED or group of LEDs. This allows me to create more dynamic visual effects but can
increase processor usage and program complexity.
2. In the debugging process, I would print outputs at various points in the program
using the serial monitor. This way, I can see where an error occurs. Additionally, I
would check the logic of the code and, if necessary, review the hardware to ensure the
LEDs are connected to the correct pins.
3. To use LEDs with different features, I would add additional pieces of code capable
of controlling these characteristics and possibly additional hardware modules (e.g.,
specific drivers for RGB LEDs).
4. To control LEDs in response to user input, I would integrate input devices like
buttons or sensors into the system. Then, I would add code that checks the status of
these inputs and adjusts the LEDs accordingly.
5. To reduce energy consumption, we could turn off LEDs when not needed or enable
sleep mode on the microcontroller. Also, by selecting low-power components and
employing energy-efficient coding techniques, we can optimize energy consumption.
6. Security system integration would require communication modules (e.g., Wi-Fi,
Bluetooth), and perhaps some form of authentication mechanism. I would establish a
protocol to receive alerts and add code to ensure the LEDs blink appropriately in
necessary situations.
7. Security vulnerabilities often arise from incorrect configuration, weak passwords, or
outdated software. Regularly updating the system, using strong encryption methods,
and adding extra protection layers like firewalls or VPNs can help mitigate these
vulnerabilities.
8. Real-world applications of this technology include interactive artworks, educational
tools, home automation, and even simple entertainment products. Working with LEDs
offers vast opportunities both in terms of visual appeal and technical learning.
9. To dynamically change the blinking pattern of the LEDs, I could write code that
would alter patterns during runtime, possibly using an array or a parameter-based
system. This could change depending on user inputs or specific conditions.
10. Using this project as an educational tool, I could teach students basic electronics,
coding principles, and hardware-software interaction concepts. By adopting a hands-
on learning method, I can enable students to create their own projects, thereby
reinforcing their theoretical knowledge with practical experience.

183
Microcontrollers for Steam-AI Students Kamil Bala

1.2.26. LED Array 2 Animation

https://www.tinkercad.com/things/aU2guoWuzGD-1226led-array-2-animation

/*************************************************************************
Program Name: LED Array 2 Animation

Program Objective: Creating various animations with the LED array

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
int delayTime=500; // Variable 'delayTime' defining the delay

int led[2][6]= {{3,5,7,9,11,13},{2,4,6,8,10,12}};

void setup()
{
// We increment the pins from 2 to 13 by 1 to run the function
for(int pinNumber=2; pinNumber<14; pinNumber++)
{
// We set the current value of pinNumber as the output pin
pinMode(pinNumber, OUTPUT);
}
}

void loop()
{

for(int i=0; i<2; i++)


{
for(int j=0; j<6; j++)
{
digitalWrite(led[i][j], HIGH); // We set the current LED array value
to logical HIGH
delay(delayTime); // We cause a delay for 'delayTime' duration
}
}
for(int i=0; i<2; i++)
{
for(int j=0; j<6; j++)
{
digitalWrite(led[i][j], LOW); // We set the current LED array value
to logical LOW
//delay(delayTime); // We cause a delay for 'delayTime' duration
}
}
delay(delayTime); // We cause a delay for 'delayTime' duration

for(int i=0; i<2; i++)

184
Microcontrollers for Steam-AI Students Kamil Bala

{
for(int j=0; j<6; j++)
{
digitalWrite(led[i][j], HIGH); // We set the current LED array value
to logical HIGH
delay(delayTime); // We cause a delay for 'delayTime' duration
digitalWrite(led[i][j], LOW); // We set the current LED array value
to logical LOW
delay(delayTime); // We cause a delay for 'delayTime' duration
}
}

for(int k=0; k<3; k++)


{
for(int i=0; i<2; i++)
{
for(int j=0; j<6; j++)
{
digitalWrite(led[i][j], HIGH); // We set the current LED array value
to logical HIGH
}
delay(delayTime); // We cause a delay for 'delayTime' duration

for(int j=0; j<6; j++)


{
digitalWrite(led[i][j], LOW); // We set the current LED array
value to logical LOW

}
delay(delayTime); // We cause a delay for 'delayTime' duration
}
}

185
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program controls LEDs in a specific pattern. Generally, the program
sequentially lights up a series of LEDs for a certain time interval, then turns them off,
followed by lighting all the LEDs at once and then turning them off. This process occurs
based on a delay period (iTime) set by the user. The purpose of the program is to provide a
visual display or create a simple animated lighting pattern. Here’s what the program does:
1. Initial Setup: Initially, the program initiates specific pins on the Arduino board in
output mode. These pins are connected to the LEDs.
2. LED Array: The LEDs are divided into two separate arrays (or groups) and are
contained within led[2][6]. Each array consists of LEDs that will be lit and
extinguished in a specific order.
3. Main Loop: The loop() function is the function that Arduino continually runs. Within
this loop, the program lights and extinguishes LEDs in various ways:
o First, all LEDs within both arrays are lit sequentially. Each LED stays on for
the duration of iTime. o Then, all LEDs are turned off quickly, without any
delay.
o Afterward, each LED is lit and extinguished individually. There is a delay of
iTime between the lighting and extinguishing for each LED.
o Finally, all LEDs blink three times. There is a delay of iTime between each
blinking cycle. This program provides a simple LED animation and controls
the blinking speed of the LEDs based on the iTime value set by the user. Such
a structure can be used in entertainment, visual arts, billboards, or DIY
electronic projects for educational purposes.

186
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable Definition:
o iTime: This variable holds the delay period (in milliseconds) that determines
how long the LEDs will blink.
o led: This two-dimensional array contains the pin numbers of the LEDs to be
controlled. They are organized into two separate groups.
2. Setup Function:
o The setup function runs once when the program starts. Within this function,
pins 2 to 13 are initiated in output mode. These pins are where the LEDs are
connected.
3. Main Loop - loop Function: This function is continuously run by Arduino and
controls the LEDs.
a. First Loop:
o In the first loop, LEDs in both groups within the led array are lit
sequentially.
o Each LED remains lit for the time specified in the iTime variable.
o This process is carried out for both groups.
b. Second Loop:
o Subsequently, all LEDs in both groups are quickly turned off. There is
no delay at this stage.
c. Third Loop:
o In this loop, each LED is lit and extinguished sequentially. Each LED is
controlled with a delay of iTime between the lighting and extinguishing.
o After each LED blinks, the process moves to the next LED. This occurs
for both groups.
d. Fourth Loop:
o In this final loop, all LEDs in both groups blink three times.
o Each group of LEDs lights up, waits for iTime, then extinguishes. This
process repeats the specified number of times.
o There is a delay of iTime between each blinking cycle.
4. Delay:
o After each operation, there is a wait for a specific period using the
delay(iTime) function. This controls the speed of the LEDs’ blinking and
affects the appearance of the visual effect. The program operates by repeating
these steps, creating a visual effect with the blinking LEDs. This process
continues as long as the Arduino has a power source connected.

187
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. What is the purpose of the two-dimensional array structure used in the program, and is
it possible to perform the same function without this structure?
2. If the value of the iTime variable is changed, what effect does it create on the behavior
of the LEDs blinking? What should be considered when making this change?
3. If this system is to be used as a visual alert system, how can we optimize the existing
code structure so that people can notice it more easily?
4. What optimizations can be made in terms of energy consumption during the LEDs’
blinking process, and how do these optimizations affect the system’s visuals or
performance?
5. What sensors or external devices can be integrated to expand this code, and how do
these integrations improve the overall functionality of the system?
6. In the event of any error in the program (for example, when an LED malfunctions),
what strategies can we apply for error detection and error management?
7. How can the current structure of the program be scaled with different numbers of
LEDs and different groupings? How does this scalability affect the program’s
efficiency and functionality?
8. If this program is to be used as a game or an interactive installation piece, what
features can be added to provide user interaction and feedback?
9. When a large number of LEDs need to be controlled, what are the limitations of the
existing code structure, and what approaches can be used to overcome these
limitations?
10. How can we integrate this project with a system that can be controlled wirelessly, and
what are the advantages and challenges of this integration on the overall system?

188
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. What is the purpose of the two-dimensional array structure, and is it possible to


perform the same functionality without this structure?
o The two-dimensional array is used to manage grouped LEDs in the program,
allowing each set of LEDs to be easily controlled. Functionality can be
achieved without this structure, but the code may become more complicated
because the status of each LED would have to be controlled individually.
2. If the value of the iTime variable is changed, what effect does it have on the blinking
behavior of the LEDs? What should be considered when making this change?
o iTime determines how long the LEDs stay on or off. Lowering the value
causes the LEDs to blink faster; increasing it causes them to blink slower.
Factors such as the human eye’s ability to perceive blinking lights and the
lifespan of the LEDs should be considered when making this adjustment.
3. If this system is to be used as a visual alert system, how can we optimize the existing
code structure so that it’s more easily noticeable by people?
o For visual alert systems, factors like the brightness of the LEDs, blink rate, and
color are important. Additionally, attention can be drawn or specific messages
conveyed by using different blinking patterns. For instance, faster-blinking red
LEDs could be used to indicate an emergency.
4. What optimizations can be made in terms of energy consumption during the LEDs’
blinking process, and how do these optimizations affect the visuals or performance of
the system?
o To reduce energy consumption, you could decrease the brightness of the LEDs
or shorten the time they are on. However, these changes may decrease the
visibility of the LEDs and, consequently, the effectiveness of the system.
Finding a balance is important.
5. What sensors or external devices can be integrated to expand this code, and how do
these integrations improve the system’s overall functionality?
o Motion sensors, sound sensors, light sensors, and similar devices can be
integrated. These sensors can increase the system’s usability and effectiveness
by making system activation dependent on environmental factors or user
interaction.
6. In the program, for any error situation (like an LED malfunctioning), what strategies
can we implement for error detection and error management?
o For error detection, additional sensors or circuits that can monitor the status of
the LEDs and report problems can be added to the system. For error
management, strategies could be developed such as automatically engaging
backup LEDs or bypassing malfunctioning LEDs.
7. How can the existing structure of the program be scaled with different numbers of
LEDs and different groupings? How does this scalability affect the efficiency and
functionality of the program?

189
Microcontrollers for Steam-AI Students Kamil Bala

o The program can easily be scaled using array structures and loops. Adding
more LEDs or different groupings requires just adjustments to the array
variables and loop parameters. However, controlling a large number of LEDs
may increase processor resource usage and power consumption, so hardware
capacity and power supply should also be considered.
8. If this program is to be used as a game or an interactive installation piece, what
features can be added for user interaction and feedback?
o For interactive features, physical user interfaces like touch sensors, buttons, or
motion sensors could be added. Digital feedback methods, such as auditory
feedback, displays, or integration with mobile devices, could also be used.
9. When controlling a large number of LEDs, what are the limitations of the existing
code structure, and what approaches can be used to overcome these limitations?
o The existing code is limited to a certain number of LEDs, and adding too many
LEDs can decrease performance or reach the microcontroller’s pin limitations.
To overcome these limitations, additional components like LED drivers or
multiplexers could be used, or a transition to a more powerful microcontroller
could be made.
10. How can we integrate this project with a wirelessly controllable system, and what are
the advantages and challenges of this integration on the overall system?
o For wireless control, wireless communication modules like Wi-Fi or Bluetooth
can be added to the project for remote access. This provides convenience and
flexibility but also brings new challenges such as security, connection stability,
and energy consumption.

190
Microcontrollers for Steam-AI Students Kamil Bala

1.2.27. Multiplexing 1_8 Output

https://www.tinkercad.com/things/3FXifNXRxtW-1227multiplexing-18-output

/*************************************************************************
Program Name: Port Multiplexing 1

Program Objective: We shift the entered bit to the right,


turning the LED outputs on and off,
then by shifting a newly entered pattern
to the left,
we enable the LEDs to flash.
This script is for a microcontroller setup
and involves bit manipulation to create
patterns of flashing LEDs by shifting
bits to the right and left, effectively
demonstrating a basic level of port
multiplexing.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/

const int data_pin = 2;


const int clock_pin = 3;
const int latch_pin = 4;

// Time to display each LED


int wait = 200; // milliseconds (ms)

void setup() {
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
}

void loop() {

// Shift the entered bit 'x' to the right


byte x = B10000000;
for ( int i = 0; i < 8; i++ ) {
shiftDisplay(x);
x = x >> 1;
delay(wait);
}

// Shift the entered bit 'x' to the left


x = B00000001;
for ( int i = 0; i < 8; i++ ) {

191
Microcontrollers for Steam-AI Students Kamil Bala

shiftDisplay(x);
x = x << 1;
delay(wait);
}
}

void shiftDisplay(byte data) {


digitalWrite(latch_pin, LOW);
shiftOut(data_pin, clock_pin, LSBFIRST, data);
digitalWrite(latch_pin, HIGH);
}

192
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This program uses a shift register to control a series of LEDs. Shift registers are used to
control a large number of outputs (in this case, LEDs) with a single microcontroller. This
increases the number of available pins, allowing more components to be controlled. The
purpose of the program is to create a specific LED blinking pattern. This is done by “shifting”
a specific bit pattern to the right and left, causing the LEDs to blink in sequence.
In this example, the bit pattern is shifted right and left, making the LEDs blink sequentially
from one end to the other. The program uses the following main components:
o Data Pin, Clock Pin, Latch Pin: These pins are used to send data from the
microcontroller to the shift register. The Data pin carries the bits to be sent; the
Clock pin determines when the shift register will receive the data; the Latch pin is
used to “lock” the data into the shift register, so the outputs are updated after all
the data has been sent.
o shiftDisplay function: This function sends data to the shift register. First, the latch
pin is pulled low, so the shift register collects the incoming data but does not yet
apply it to the output pins. Then, the shiftOut function sends the data. Finally, the
latch pin is raised high, applying the collected data to all output pins.
o Loops within the loop function: Here, the initially defined bit pattern is shifted
right and left. In each iteration, the shiftDisplay function is called, so the updated
bit pattern is sent to the shift register, and the LEDs are updated.
This program represents a common approach often used with microcontrollers for more
efficient resource use, especially when controlling output components like LEDs.

193
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

This Arduino program is designed to blink a series of LEDs in sequence. This is achieved
using a shift register, and the LED blinking pattern is created by shifting bits. Here’s a step-
by-step explanation of the program:
1. Definition of Variables and Pins:
o The data_pin, clock_pin, and latch_pin variables store the pin numbers of the
Arduino to which the shift register is connected.
o The wait variable defines the transition time between LEDs (in milliseconds).
2. Setup Function:
o The pinMode function sets the specified pins to output mode. This is necessary
for sending data to the shift register.
3. Loop Function:
o The first loop shifts a bit pattern (initially set with the highest bit) to the right.
In each step, the updated bit pattern is sent to the shiftDisplay function, and a
wait period is applied.
o The second loop shifts a bit pattern (initially set with the lowest bit) to the left.
The process is the same as the previous loop, but this time the bit pattern is
shifted to the left.
4. shiftDisplay Function:
o This function is used to send data to the shift register.
o First, the latch_pin is pulled low. This allows the shift register to accept new
data but not yet apply it to the output pins.
o Then, the shiftOut function sends the specified data (bit pattern) to the shift
register. This function sends the data bit by bit, and the shift register stores this
data internally.
o Finally, the latch_pin is raised high. This “locks” the data in the shift register
to all output pins, thus setting the LEDs to the appropriate state.
5. Wait Time:
o Between each LED change, the program pauses for a while (delay(wait)). This
is necessary for us to visually perceive the blinking effect of the LEDs. Each of
these steps ensures that the LEDs blink in sequence by shifting bit patterns and
sending these patterns to the shift register.
This process is continuously repeated within the loop function, so the LEDs keep blinking in
the specified pattern.

194
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the role of the shift register in this application, and what other applications can
it be used for in this project?
2. What exactly does the shiftOut function do, and how does it work?
3. What are the functions of the data_pin, clock_pin, and latch_pin, and how do they
work together?
4. What do the terms LSBFIRST and MSBFIRST mean, and why was LSBFIRST used
in this project?
5. If there is a need for debugging in this program, what steps would you follow in your
troubleshooting process?
6. What changes could you make in the program to alter this LED blinking pattern?
7. How do you use the bit-shifting process to create a specific LED sequence in the
program?
8. If you wanted to further develop this project, what additional features or functions
would you consider adding?
9. Are there any external factors or limitations that could affect the program’s
performance? If so, what are they, and how would you overcome them?
10. What are the advantages and disadvantages of using a shift register, and could there be
an alternative method to using this technology?

195
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Role of the Shift Register: The shift register allows for data to be taken in serially
and output in parallel. This simplifies the hardware and reduces the number of pins
required. Other applications could include reading a large number of sensors or
controlling more complex visual output devices like LCD screens.
2. shiftOut Function: This function sends a specific byte of data serially, bit by bit,
through a specific pin. Data is transmitted in sync with each pulse on the clock pin.
3. Functions of the Pins:
o data_pin: Carries the serial data output.
o clock_pin: Carries the clock signal that synchronizes data transmission.
o latch_pin: Controls when the serial data is transferred to the parallel output;
it’s set high after all data is sent to update the output.
4. LSBFIRST and MSBFIRST: These terms mean “least significant bit first” and
“most significant bit first,” respectively, indicating which bit of the data is sent first.
LSBFIRST is used in this scenario to maintain a specific order of data transmission.
5. Debugging Process: The first step is identifying the part that is not working correctly.
Debugging is then done by reviewing the code and connections and using tools like a
serial monitor. If necessary, narrowing down the source of the issue by breaking the
code into smaller sections or adding test outputs.
6. Changing the LED Pattern: You can alter bit patterns in the ‘x’ variable or adjust
delay times within the loop to create different blinking patterns.
7. Creating a Specific LED Sequence: You utilize the bit-shifting process by
establishing a bit pattern that dictates which LEDs will blink in what order. This is
done by changing the value of the ‘x’ variable.
8. Project Development: Additional features could include LEDs of different colors,
lights that blink in sync with music, or light patterns that change in response to user
input.
9. Potential External Factors: Factors like ambient light, power fluctuations, or
electrical noise from the hardware could affect performance. These issues can be
addressed with better shielding, stable power supplies, or capacitors to reduce noise.
10. Advantages/Disadvantages of the Shift Register: Advantages include simplicity of
the hardware and reduced pin requirement. A disadvantage might be that it may not be
suitable for high-speed requirements. Alternative methods could involve other
communication protocols like SPI or using more advanced microcontrollers.

196
Microcontrollers for Steam-AI Students Kamil Bala

1.2.28. Port Multiplexing_2_8 Output_Test

https://www.tinkercad.com/things/l8CTRicQ7MD-1228port-multiplexing28-outputtest

/*************************************************************************
Program Name: Port Multiplexing_2_8 Output_Test

Program Objective: Lighting desired LEDs with serial output

This script is for a microcontroller setup


where it controls LEDs (or similar outputs)
through a serial output method, specifically
by using a shift register or similar technique
to control multiple outputs from fewer
microcontroller pins. The 'shiftDisplay'
function is utilized to manage the state of
each LED.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/

const int data_pin = 2;


const int clock_pin = 3;
const int latch_pin = 4;

// Time to display each LED


int wait = 200; // milliseconds (ms)

void setup() {
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
}

void loop() {
byte x = B10010011;
shiftDisplay(x);
}

void shiftDisplay(byte data) {


digitalWrite(latch_pin, LOW);
shiftOut(data_pin, clock_pin, LSBFIRST, data);
digitalWrite(latch_pin, HIGH);
}

197
Microcontrollers for Steam-AI Students Kamil Bala

Explanation of the Program in General:

This Arduino program utilizes a shift register to control a series of LEDs using a specific bit
pattern. The primary purpose of the program is to serially light up and turn off LEDs with a
pre-defined bit pattern. This process employs the technique of port multiplexing to increase
the number of digital pins and emulate more output pins on the microcontroller.
The program uses three essential pins:
1. data_pin: This pin is used for sending serial data to the shift register.
2. clock_pin: This pin sends clock signals to determine when the data bits are written
into the shift register.
3. latch_pin: This pin ensures that, after all the serial data is written into the shift
register, the data is “locked” into the parallel output pins.
The operational steps of the program are as follows:
o Initially, the pins used are set to output mode.
o Within the main loop, a variable of type byte, x, represents a specific bit pattern
(B10010011). This pattern determines which LEDs will light up and which will
remain off.
o The shiftDisplay function takes this bit pattern and transfers it to the shift register.
During this process, the ‘latch’ pin is kept low so that transitions are not reflected
on the LEDs. After sending the serial data and clock signal, the ‘latch’ pin is made
high, enabling the transfer of data in the shift register to the parallel output pins.
o This process continually repeats within the loop function, but since there are no
changes in the loop function in this example, and the value of x remains constant,
there is no change in the LEDs; the same LEDs stay lit.
This program aims to create a wider range of outputs by effectively using the limited I/O pins
of microcontrollers. This is especially useful when controlling large LED matrices, displays,
or other output devices.

198
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

This program serves to control LEDs using a specific bit pattern. Here’s a step-by-step
explanation: Initial Setup (Setup)
1. Setting the Pin Modes: The setup() function runs once at the beginning of the
program. Here, the modes for the pins to be used are determined. The pins used for
data, clock, and latch are set as output.
void setup() {
pinMode(data_pin, OUTPUT); // Set the data pin as output
pinMode(clock_pin, OUTPUT); // Set the clock pin as output
pinMode(latch_pin, OUTPUT); // Set the latch pin as output }
Main Cycle (Loop)
2. Defining the Bit Pattern: The loop() function is the continuously operating section
on the Arduino. In this example, a single operation is conducted in loop(): a byte
variable, x, represents a pre-defined bit pattern.
void loop() {
byte x = B10010011; // Define the bit pattern
shiftDisplay(x); // Send the bit pattern to the shift operation function
}
3. Controlling the LEDs: The shiftDisplay(byte data) function controls which LEDs
will light up or blink. This process occurs in several steps:
a. Lowering the Latch Pin: By temporarily lowering the latch pin, the shift
register is made to accept the new data. At this point, the data written to the
shift register is not yet reflected on the LEDs.
digitalWrite(latch_pin, LOW); // Make the latch pin low
b. Shifting the Data: The shiftOut() function writes the specified bit pattern (data)
serially into the shift register. This involves sending data and clock signals at
specific timings.
shiftOut(data_pin, clock_pin, LSBFIRST, data); // Write the data serially into
the shift register
c. Raising the Latch Pin: By raising the latch pin, the data in the shift register is
activated, and it is reflected on the LEDs.
digitalWrite(latch_pin, HIGH); // Make the latch pin high With these steps
completed, the LEDs light up or turn off according to a specific bit pattern.
This process is continually repeated within the loop() function, but in this example, since the
bit pattern is constant, the status of the LEDs does not change (the same LEDs remain lit).

199
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. If you want to change the bit pattern, which part of the code would you alter, and what
could be the alternative bit patterns?
2. What exactly does the shiftOut function do in the program, and what is the principle
behind how this function works?
3. In this code, which variables or parameters would you change to make the LEDs blink
faster or slower?
4. If you want to use more than one shift register, how would you need to modify the
code?
5. What is the relationship between the commands digitalWrite(latch_pin, LOW); and
digitalWrite(latch_pin, HIGH);, and does the order of these commands matter?
6. What hardware and software modifications would you suggest to make this project
more complicated?
7. If you wanted to expand this system to display a specific sequence of light patterns,
what changes would you need to make?
8. Does this program include any error management or debugging methods? What error
control mechanisms do you think are missing?
9. What is the difference between LSBFIRST and MSBFIRST, and what is the effect of
changing these parameters on the output?
10. How energy-efficient is this code in terms of power consumption, and what
optimizations could you make to reduce energy consumption?

200
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. Changing the bit pattern: To change the bit pattern, you should alter the line byte x
= B10010011; Alternatively, you can create new patterns using different combinations
of 0s and 1s, such as B11001100.
2. Function of the shiftOut: This function sends a specific bit pattern (byte) serially to a
shift register. Using the data and clock pins, the bits are sent one by one to the shift
register, resulting in the attached LEDs lighting up in a specific pattern.
3. Adjusting the LED blinking speed: In this code, there isn’t a direct variable to
change the speed of the LEDs as they light up in a static pattern. However, if blinking
is added, you can adjust the duration of how long the LEDs stay on/off by changing
the time specified in the delay function.
4. Using multiple shift registers: It’s possible to connect multiple shift registers in
series. In this case, you should call the shiftOut function separately for each shift
register and send different data bits for each.
5. Relationship between
digitalWrite(latch_pin, LOW); and digitalWrite(latch_pin, HIGH);: These commands
trigger the transfer of data in the shift register to the output pins (i.e., the LEDs). The
LOW command indicates that the shift register is ready to receive new data, while the
HIGH command “locks” the current data onto the pins, causing the LEDs to light up.
6. Suggestions for complicating the project: To enhance the project, you could add
various light patterns, colored LEDs, music, or sound effects, or introduce interactivity
with inputs like motion sensors. Also, you could expand the code with a function to
display different patterns sequentially.
7. Displaying a specific sequence of light patterns: To show various light patterns
sequentially, you could create an array containing different bit patterns and select from
this array in a loop to pass patterns to the shiftOut function.
8. Error management and debugging: The current code does not have specific error
management or debugging methods. For error control, mechanisms could be added
that check pin connections, prevent invalid data transmission, or detect hardware
failures.
9. Difference between LSBFIRST and MSBFIRST: These parameters specify from
which bit of the data (least significant bit or most significant bit) transmission will
begin. LSBFIRST starts from the lowest significant bit (leftmost), and MSBFIRST
starts from the highest significant bit (rightmost). This could affect the sequence of the
LEDs lighting up.
10. Energy consumption and optimization: The current code maximizes power
consumption by lighting all LEDs simultaneously. To reduce energy consumption,
you could use fewer LEDs, add resistors to decrease LED brightness, or control the
power consumption of the LEDs by using techniques like PWM (Pulse Width
Modulation).

201
Microcontrollers for Steam-AI Students Kamil Bala

1.2.29. Port Multiplexing_3_8 Output_Test


https://www.tinkercad.com/things/hEgOTWaJe02-1229port-multiplexing38-
outputtest

/*************************************************************************
Program Name: Port Multiplexing_3_8 Output_Test

Program Objective: Lighting desired LEDs with serial output,


This code is for a system that controls
the lighting of LEDs using serial output.
It is written for microcontrollers and
sends a specific pattern to the LEDs by
shifting out byte values to the LED array,
turning certain LEDs on or off based on the
binary representation in the byte values.
The script includes functions for setting up
the system and looping through predefined
LED lighting sequences with specific delays.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/

const int data_pin = 2;


const int clock_pin = 3;
const int latch_pin = 4;

// Time to display each LED


int wait = 500; // milliseconds (ms)

void setup() {

pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
}

void loop() {
byte x[3] = {B00000000,B10010011,B11000011};

shiftDisplay(x[1]);
delay(wait);
shiftDisplay(x[0]);
delay(wait);
shiftDisplay(x[2]);
delay(wait);
shiftDisplay(x[0]);
delay(wait);

202
Microcontrollers for Steam-AI Students Kamil Bala

void shiftDisplay(byte data) {


digitalWrite(latch_pin, LOW);
shiftOut(data_pin, clock_pin, LSBFIRST, data);
digitalWrite(latch_pin, HIGH);
}

203
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program is written for a project called “Port Multiplexing_3_8 Output_Test.”
The main purpose of the program is to control LEDs in a specific pattern serially and to use a
shift register to turn them on and off. This code, written by Kamil Bala, serves a kind of visual
presentation or test function by displaying different LED patterns over a certain period of
time.
The basic components and operations of the program are as follows:
1. Constant Pin Definitions:
o The constants data_pin, clock_pin, and latch_pin are assigned to pins 2, 3, and
4, respectively. These pins will be used for controlling the shift register.
2. Defining the Delay Duration:
o The wait variable specifies the delay time between LED patterns. In this case,
each LED pattern is displayed for 500 milliseconds (half a second).
3. Setup Function:
o Within the setup function, the data, clock, and latch pins are set to output
mode. This allows the Arduino to send signals to the shift register through
these pins.
4. Main Loop (Loop) Function:
o The loop function contains the program’s main cycle, where different LED
patterns are turned on and off at specified time intervals.
o A byte array named x is defined, containing bit patterns that represent different
LED patterns.
o The shiftDisplay function takes a specific byte value (LED pattern) and applies
this pattern to the LEDs.
o The delay function allows waiting for a certain period after each LED pattern
is displayed, making the patterns visible to the eye.
5. shiftDisplay Function:
o This helper function manages the process of sending data to the shift register.
o First, the latch_pin is pulled low, allowing a new data set to be sent to the shift
register.
o The shiftOut function sends a specific bit pattern (data) serially to the shift
register. o Finally, the latch_pin is pulled high, triggering the transfer of data in
the shift register to the output pins (i.e., LEDs).
This program allows control of many LEDs using a shift register through serial
communication, with a limited number of pins. This method is particularly useful in situations
where the number of pins is limited or projects where a large number of LEDs need to be
controlled.

204
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Variable and Pin Definitions:


o At the start of the program, the pins and variables to be used are defined.
o data_pin, clock_pin, and latch_pin are assigned to pins 2, 3, and 4,
respectively. These are the pins that will be used for the shift register control
signals.
o The wait variable specifies the waiting time (in milliseconds) for the transition
time between LEDs.
2. Setup Function:
o This function runs once when the program is loaded and initiates the pins in
the correct mode.
o Using the pinMode function, data_pin, clock_pin, and latch_pin are set to
output mode.
3. Main Loop - Loop Function:
o This function is continuously repeated on the Arduino.
o Firstly, an array of type byte, named x, is defined and assigned three different
bit patterns. These patterns determine the sequence in which the LEDs will
light up and go out.
o The shiftDisplay function is used to display the active LED pattern. This
function takes an element from the x array and lights the LEDs according to
this pattern.
o After each LED pattern is displayed, there is a wait for a specific duration (in
this case, 500 ms) using the delay function. This ensures the LED patterns are
perceivable by the human eye.
4. shiftDisplay Function:
o This helper function controls how the LED patterns will be sent to the shift
register. o The function pulls the latch_pin low, meaning the shift register is
ready to accept new data.
o Next, the shiftOut function is called, and the bit pattern passed as the data
parameter is sent serially to the shift register via the data_pin and clock_pin.
o In the last step, the latch_pin is raised high, allowing the data inside the shift
register to be transferred to the output pins (LEDs). These steps explain the
basic flow of the program and how and when each function will be used. This
program is a fundamental example of effectively controlling a large number of
LEDs using a shift register.

205
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. If this system were to be used in a product or practical application, what features


would be added for users to interact with the system?
2. What method would be followed to make the ‘wait’ variable in the program dynamic,
so the wait time could be adjusted for different situations?
3. If we wanted to expand the system and add more LEDs, what changes would we need
to make in the existing code?
4. If we wanted to use this LED arrangement as a type of visual alert system, what kind
of real-time data would we want to integrate, and how would we carry out this
integration?
5. What strategies could we apply to optimize power consumption in the current system?
6. If we wanted to control this system wirelessly, what wireless technologies could we
use, and what would we need to change in the code to accomplish this integration?
7. How would we manage the troubleshooting (debugging) process when there is a fault
in the system? What types of logs or debugging strategies could we use?
8. Are there any security vulnerabilities in the current code? If this system were
connected to a network, how would we address potential cybersecurity risks?
9. If we wanted to add a feature allowing users to program the blinking patterns of the
LEDs in the system, how would we design this customization function?
10. If we consider this project as an instructional lab or workshop exercise, what
educational questions would we ask the students, and what creative modifications or
improvements would we expect them to make?

206
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. For user interaction, we could add an interface controlled via physical buttons or
through a mobile application. This way, users can change the wait time, patterns, and
speed of the flashing LEDs.
2. To make the ‘wait’ variable dynamic, we could read an analog input by using a
potentiometer or add a feature that allows this value to be set via a user interface.
3. If we wanted to add more LEDs, we would first need to add extra output pins or
additional shift registers. We would have to update the code to manage the extra
outputs and the new connection scheme.
4. To use it as a visual alert system, we could integrate real-time data such as weather
conditions, traffic information, or stock prices. Necessary modifications would be
made in the code for receiving this data through APIs and integrating it into the
system.
5. To optimize power consumption, we can do code optimizations that would reduce the
brightness of the LEDs or activate the LEDs only when necessary. Additionally, we
can reduce the power consumption of the microcontroller by using low-power modes
and sleep modes.
6. For wireless control, we could use wireless modules like Wi-Fi or Bluetooth. This
allows remote control via a mobile app or computer program. The code should be
updated to communicate with the wireless module and process the commands.
7. The troubleshooting process could involve steps like printing debugging messages on
the serial monitor and perhaps programming the LEDs to alert the user in case of a
fault.
8. Security-wise, in a network-connected system, precautions such as data encryption,
secure login mechanisms, and perhaps the use of firewalls or VPNs should be taken.
9. For the customization function, we can create a user interface where users can create
their patterns and upload them to the system. This could be a mobile app or a web-
based interface.
10. In an educational project, we might ask students to make modifications to the system
or solve specific problems. For instance, they could develop original solutions in areas
like energy efficiency, user interface design, or wireless control. Also, questions
directed at reinforcing their theoretical knowledge could be asked.

207
Microcontrollers for Steam-AI Students Kamil Bala

1.2.30. Port Multiplexing_16 Output

https://www.tinkercad.com/things/dxf1VhgtPO2-1230port-multiplexing16-output

/*************************************************************************
Program Name: Port Multiplexing_16 Output

Program Objective: Lighting desired LEDs with serial output


This script is for a microcontroller system
designed to control LED lights through serial
output, allowing individual LEDs to be lit as
desired. The system uses a method to write to
the LEDs, sending data serially, and sequences
through various lighting stages with a delay
between each stage.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/

const int data_pin = 2;


const int clock_pin = 3;
const int latch_pin = 4;

// Time to display each LED


int wait = 500; // milliseconds (ms)

void ledWrite(int block1, int block2){


shiftOut(data_pin, clock_pin, LSBFIRST, block1);
shiftOut(data_pin, clock_pin, LSBFIRST, block2);
digitalWrite(latch_pin, HIGH);
digitalWrite(latch_pin, LOW);

void setup() {
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
}

void loop() {
ledWrite(128,0); delay(wait);
ledWrite(64,0); delay(wait);
ledWrite(32,0); delay(wait);
ledWrite(16,0); delay(wait);
ledWrite(8,0); delay(wait);
ledWrite(4,0); delay(wait);
ledWrite(2,0); delay(wait);

208
Microcontrollers for Steam-AI Students Kamil Bala

ledWrite(1,0); delay(wait);
ledWrite(0,128); delay(wait);
ledWrite(0,64); delay(wait);
ledWrite(0,32); delay(wait);
ledWrite(0,16); delay(wait);
ledWrite(0,8); delay(wait);
ledWrite(0,4); delay(wait);
ledWrite(0,2); delay(wait);
ledWrite(0,1); delay(wait);
}

209
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program, titled “Port Multiplexing_16 Output”, is designed to control a series
of LEDs through serial communication. The aim of the program is to sequentially turn on and
off the LEDs in a specific pattern using a microcontroller. This process is achieved using the
port multiplexing method to manage 16 different outputs (e.g., 16 different LEDs). The
program uses specific pins (data, clock, and latch) for serial communication and sets them as
outputs. These pins are connected to a shift register to determine the state (on or off) of the
LEDs. The shift register enables the control of multiple outputs (in this case, LEDs) through a
single serial data line. The ledWrite function receives two blocks of data (two bytes, a total of
16 bits) and sends this data to the shift register. This process determines the sequence in
which the LEDs will blink. The program uses this function in a specific loop to sequentially
light each LED. Between each ledWrite operation, the system waits for the duration specified
in the wait variable. This controls the blinking speed of the LEDs. No changes to any LED are
made during this time; the system merely waits for the next change. This program is useful
especially in education, light shows, or situations requiring simple visual output. It’s also a
great example for learning how to program microcontrollers.

210
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-step Explanation:

This program is used to control a series of LEDs in a specific sequence. Here’s a step-by-step
explanation:
1. Defining Variables and Constants:
o Initially, the program defines pin numbers to which data, clock, and latch pins
are connected, and the delay between LEDs. This is done globally at the
beginning of the program, making these variables available throughout.
2. ledWrite Function:
o The ledWrite function sends two bytes (16 bits) of data to a shift register
through serial transmission. It first sets the data to be sent starting with the
LSB (least significant bit) and then shifts the data in a specific order. It then
triggers the transfer of values in the shift register to the connected LEDs by
setting the latch pin HIGH and then LOW. This causes the attached LEDs to
light up or turn off.
3. setup Function:
o The Arduino’s setup function runs once when the program is loaded. Here, the
data, clock, and latch pins are set as outputs, allowing these pins to send
signals.
4. loop Function:
o The loop function is the main cycle of the Arduino program, where the code
continuously runs. In this function, LEDs are activated in a specific order using
the ledWrite function. For example, the first LED lights up first, followed by
the second, and so on.
o After each LED is activated, the program pauses for a specific duration
(determined by the wait variable). This ensures each LED remains visibly on
for a while.
5. Delay Between Processes:
o After each LED blinks, there’s a specific “waiting” duration. This controls the
blink speed of the LEDs and affects how the visual effect appears. During this
time, the program waits for the next LED transition.
6. Continuation of the Loop:
o The code within the loop function keeps repeating until the Arduino device is
turned off. This results in the LEDs blinking in a specific sequence
continuously. This program is an excellent example of controlling LEDs and
creating simple animations. It also explains the basics of port multiplexing and
serial communication.

211
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. What is the general purpose of this program, and in what types of real-world
applications do you think this kind of code could be used?
2. What is the operational logic of the ledWrite function, and how could we make this
function more flexible or versatile?
3. Do you see any efficiency issues in this code? If so, how can we optimize the code’s
performance?
4. What limitations does the program have in its current state, and what features or
capabilities could we add to overcome these limitations?
5. What are the advantages and disadvantages of the continuous repetition cycle in the
loop function? What alternative methods could we use instead of this approach?
6. If the need for debugging arises in this program, what strategies would you apply to
diagnose and solve problems?
7. How could we modify the program to allow more control over the selection of pins
used? For instance, allowing users to choose which pins will be used.
8. How could we expand this program to work with different arrays of LEDs or other
types of lights (for example, RGB LEDs)?
9. How do delay times (delay) affect the way the program operates? What kind of
approach could we use to adjust delay times dynamically?
10. If this system were to be used in a product, what type of interface would you offer to
the user, and how would user interaction be facilitated? For example, control of
specific LEDs, changes in animation, or brightness settings.

212
Microcontrollers for Steam-AI Students Kamil Bala

Answers:

1. General purpose of the program: This program is designed to control a series of


LEDs using serial communication. In the real world, this type of code could be used in
various applications, such as advertising billboards, decorative/lighting systems, signal
transmission, or even simple visual alerts.
2. Operational logic of the ledWrite function: This function sends two blocks of data
(two bytes, a total of 16 bits) serially to a shift register. This simplifies matters,
especially when a large number of LEDs need to be controlled. To make the function
more flexible, we could add parameters to support different numbers of LEDs or
communicate with different devices.
3. Efficiency issues: The code is quite efficient because it controls numerous LEDs
through a single function. However, when more complex animations or responses are
required, the code may need to be optimized to add functionality. For example,
performance could be improved by using dynamic timing, as current delays are fixed.
4. Limitations of the program: Currently, the program can only perform simple on/off
operations and does not control the brightness of the LEDs. These limitations could be
overcome by adding techniques such as PWM (Pulse-Width Modulation).
5. Advantages/disadvantages of the continuous repetition cycle: This approach is
simple and understandable but lacks flexibility. For instance, adding dynamic features
such as user interaction or context-dependent changes could be challenging.
Alternatively, an event-based approach or interrupts could be used.
6. Debugging strategies: Effective methods could be to provide feedback using the
serial monitor for problem diagnosis, manually checking the expected status of LEDs,
and printing test values at specific parts of the code.
7. Control over pin selection: To allow users to make pin selections, a structure could
be created where pin numbers can be set as function parameters. This would make it
more adaptable to the hardware setup.
8. Expansion with different types of LEDs: If using multicolored LEDs, such as RGB
LEDs, different data bytes or more complex data structures may be required to control
each color. Additionally, we could expand the existing code for more sophisticated
lighting solutions, like LED strips.
9. Impact of delay times: Delays affect the speed of animations. Dynamic timing could
be used to adjust timing based on specific events or conditions. For example, we could
change delays based on user input or when a particular event occurs.
10. User interface and interaction: If this were to be used for a product, an interface
could be provided for users to interact with the system. This could be physical buttons,
a mobile app, or a web-based interface. Users could control which LEDs are active,
animations, brightness levels, and perhaps colors.

213
Microcontrollers for Steam-AI Students Kamil Bala

1.2.31. Port Çoğullama_16 Çıkış _test

https://www.tinkercad.com/things/bGlhzdS5Tl6-1231port-multiplexing16-output-test

/*************************************************************************
Program Name: Port Multiplexing_16 Output_Test

Program Objective: Lighting desired LEDs with serial output


This script is designed for a microcontroller
to control LED lights through serial output.
It involves a specific function that sends
out signals to light up LED pairs according
to a test sequence. The system cycles through
a predefined array of settings, controlling
which LEDs are lit in a sequence, with a delay
implemented between the changes.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/

const int data_pin = 2;


const int clock_pin = 3;
const int latch_pin = 4;

// Time to display each LED


int wait = 500; // milliseconds (ms)

void ledWrite(int block1, int block2){


shiftOut(data_pin, clock_pin, LSBFIRST, block1);
shiftOut(data_pin, clock_pin, LSBFIRST, block2);
digitalWrite(latch_pin, HIGH);
digitalWrite(latch_pin, LOW);

void setup() {
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
}

void loop() {

// Test sequence, each pair defines which LEDs will light up in two blocks.
int test[4][2] = {{128,0}, {8,0}, {0,16}, {0,64}};

214
Microcontrollers for Steam-AI Students Kamil Bala

// Cycle through the entire test array and light the corresponding pair of
LEDs each time
for (int i = 0; i < 4; i++) {
ledWrite(test[i][0], test[i][1]);
delay(wait);
}

215
Microcontrollers for Steam-AI Students Kamil Bala

General Explanation of the Program:

This Arduino program uses a type of “Port Multiplexing” method to control a series of LEDs
in sequence. The program’s name is “Port Multiplexing_16 Output_test,” and its primary
purpose is to use serial data output to light and extinguish LEDs in a specific order. The
program utilizes certain pins (data_pin, clock_pin, and latch_pin) for serial communication.
These pins are connected to a shift register (or a similar serial-to-parallel converter). The shift
register takes the serial data and converts it into parallel data for multiple output pins to which
the LEDs are connected. Important parts of the code include:
1. Constant Pin Definitions: The program defines which Arduino pins will be used for
data, clock, and latch. These pins are necessary to send serial data to the shift register.
2. Delay Duration: A ‘wait’ variable is defined (in milliseconds) to determine the
transition time between LEDs. This controls how long the LEDs will flash.
3. ledWrite Function: This function takes two separate data blocks (block1 and block2)
and sends them serially to the shift register. This is used to control which LEDs will
flash. The first block is sent first, followed by the second block.
4. Main Loop: The ‘loop()’ function uses a ‘test’ array to specify which LEDs will light
up. The array defines the combinations in which certain LEDs will flash. The program
calls the ‘ledWrite’ function for each pair in this test array, lighting the LEDs in a
specific order.
This program provides a simple test pattern for controlling the LEDs. The test array is used to
quickly try out different LED combinations and see the transitions between each LED
combination. This could be useful in the prototype stage of a product or in an art/technology
project.

216
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Pin Definitions and Variables:


o Three pins named data_pin, clock_pin, and latch_pin are defined. These pins
are used to send data, clock signals, and latch signals to the shift register (a
type of digital data distributor).
o The ‘wait’ variable is defined, set to 500 milliseconds. This is the anticipated
duration between transitions of LEDs.
2. ledWrite Function:
o This function takes two parameters: block1 and block2. These parameters are
bytes that determine the state (on or off) of the LEDs.
o The function first sets the latch_pin to LOW, then sends two blocks of data
serially using the shiftOut function through the data_pin and clock_pin.
o After the data is sent, the latch_pin is brought HIGH, updating the outputs in
the shift register. The latch_pin is then set to LOW again.
3. setup Function:
o This function runs once at the start of the program. It sets the data_pin,
clock_pin, and latch_pin as outputs.
4. loop Function:
o This function is the main cycle of the Arduino and runs continuously.
o It calls the ledWrite function based on a predefined test array. Each pair
represents a series of LEDs.
o After each LED combination (or “test”) is shown, the program waits for the
‘wait’ duration.
o The cycle repeats from the beginning after all LED combinations defined are
displayed.
5. Test Array and LED Display:
o The ‘test’ array is a list of various LED states. Each element corresponds to an
LED combination and is processed by the ledWrite function.
o The loop operates for each element in the test array, lighting the corresponding
LEDs for a certain period.
o After each test, there is a wait for the duration specified by the delay(wait)
function. This allows observers time to see each LED combination.
These steps help you understand how the program operates. Essentially, the program displays
a series of predefined LED states and applies a specific waiting period between each state.

217
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. How does the current structure of this program affect the integration of more LEDs or
different output devices, and what changes are necessary for this integration?
2. If you had to make any performance improvements in the current code, what methods
would you consider and why?
3. If you wanted to control how this system would respond in case of a fault, what error
detection mechanisms and safety measures would you implement?
4. If you wanted to expand this project and create more complex visual effects, what
features or functions would you consider adding?
5. What challenges might you encounter regarding timing and response times in the
current system, and what strategies would you adopt to overcome these challenges?
6. Considering the maintenance and updates of this code, what changes would you make
to the current code structure to make it more modular and scalable?
7. If this system were to interact with different sensors or input devices, what approaches
would you use to manage and integrate this interaction?
8. What test procedures and standards would you apply to assess the reliability and
robustness of the system?
9. Considering current technological solutions and market trends, what could be the
potential industrial or commercial applications of such an LED control system?
10. In terms of energy consumption and efficiency, what design changes or component
selections would you make to make this system more sustainable or energy-efficient?

218
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Integration of more LEDs or different output devices: The current structure can be
expanded at both the hardware and software levels to manage more LEDs. It is
possible to add more outputs using serial communication protocols and dynamize
output control using loops and arrays in the code.
2. Performance improvements: Methods such as code optimization, removing
unnecessary delays, and making processes asynchronous can enhance performance.
Additionally, using more efficient data structures and algorithms can improve system
speed and response time.
3. Responses in case of a fault: Various strategies can be applied for error detection, such
as monitoring, error codes, try-catch blocks, and state checks. These methods identify
when something goes wrong in the system and can trigger appropriate error recovery
procedures.
4. Complex visual effects: The complexity of visual effects can be increased by adding
features such as animations, transitions, and user interaction. This can be done by
increasing the modularity of the code using functions and classes and by adding
interactive features.
5. Timing and response times: Timing can be improved by using real-time operating
systems or implementing low-level processes like interrupts. Also, multi-threading
approaches and timers can optimize response times.
6. Modularity and scalability of the code: Making the code modular by using functions,
classes, and libraries eases its maintenance and scalability. Additionally, documenting
the code and using clear naming conventions supports this process.
7. Interaction with different sensors or input devices: Techniques such as event-driven
programming, state machines, and callback functions can help manage data from
sensors and other input devices. This regulates interaction and data flow between
systems.
8. Reliability and robustness of the system: System tests, stress tests, and simulating
failure scenarios are used to assess system reliability and robustness. This includes
understanding the system’s limits and developing precautions against potential
failures.
9. Potential industrial or commercial applications: LED control systems can be used in
various areas such as advertising billboards, entertainment and stage lighting, security
warning systems, and atmospheric lighting. Applications are shaped according to
market needs and technological trends.
10. Energy consumption and efficiency: Energy consumption can be reduced by using
low-power microcontrollers, energy-efficient LEDs, and features like sleep modes.
Also, power management strategies and selecting hardware that saves energy can help
make the system more sustainable and environmentally friendly.

219
Microcontrollers for Steam-AI Students Kamil Bala

1.2.32. Port Multiplexing_24 Outputs

https://www.tinkercad.com/things/axy8bFeo747-1232port-multiplexing24-outputs-

/*************************************************************************
Program Name: Port Multiplexing_24 Outputs

Program Objective: To light the desired LEDs with serial output

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023

***************************************************************************
***/

const int data_pin = 2;


const int clock_pin = 3;
const int latch_pin = 4;

// Time to display each LED


int wait = 500; // milliseconds (ms)

void ledWrite(int block1, int block2, int block3){


shiftOut(data_pin, clock_pin, LSBFIRST, block1);
shiftOut(data_pin, clock_pin, LSBFIRST, block2);
shiftOut(data_pin, clock_pin, LSBFIRST, block3);
digitalWrite(latch_pin, HIGH);
digitalWrite(latch_pin, LOW);

void setup() {
pinMode(data_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(latch_pin, OUTPUT);
}

void loop() {
ledWrite(128,0,0); delay(wait);
ledWrite(0,128,0); delay(wait);
ledWrite(0,0,128); delay(wait);

ledWrite(64,0,0); delay(wait);
ledWrite(0,64,0); delay(wait);
ledWrite(0,0,64); delay(wait);

ledWrite(32,0,0); delay(wait);
ledWrite(0,32,0); delay(wait);
ledWrite(0,0,32); delay(wait);

ledWrite(16,0,0); delay(wait);

220
Microcontrollers for Steam-AI Students Kamil Bala

ledWrite(0,16,0); delay(wait);
ledWrite(0,0,16); delay(wait);

ledWrite(8,0,0); delay(wait);
ledWrite(0,8,0); delay(wait);
ledWrite(0,0,8); delay(wait);

ledWrite(4,0,0); delay(wait);
ledWrite(0,4,0); delay(wait);
ledWrite(0,0,4); delay(wait);

ledWrite(2,0,0); delay(wait);
ledWrite(0,2,0); delay(wait);
ledWrite(0,0,2); delay(wait);

ledWrite(1,0,0); delay(wait);
ledWrite(0,1,0); delay(wait);
ledWrite(0,0,1); delay(wait);

221
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program, as the name suggests, uses a port multiplexing technique to control
multiple LEDs. The program executes a series of operations that turn LEDs on and off in a
specific order through serial communication. This is done without increasing the number of
available hardware ports, especially when multiple LEDs or output devices need to be
controlled.
At the start of the program, basic variables such as the pins to be used and delay durations are
defined. Then, various functions are used to determine how to control the LEDs and how to
direct the output signals.
In particular, the ledWrite function sends serial data for three separate “blocks” or groups of
LEDs. Each block represents a number that determines which LEDs will be active. These
numbers are integers that set a specific bit pattern (i.e., which LEDs are on/off). The function
sends this data serially to the relevant pins and then triggers the latch pin for the next update.
Within the main loop (loop function), the program lights up and turns off different LED
combinations in a specific sequence. This is done with a certain waiting period so that the
blinking of LEDs is visible. This loop continuously repeats until the Arduino device is turned
off.
This kind of structure is used especially in areas such as interactive displays, art projects, or
educational applications. Also, this approach can be used in various automation projects or as
a status indicator.

222
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

1. Definition of Variables:
o First, the necessary variables for the pins to be used and the delay period are
defined. o data_pin, clock_pin, and latch_pin allow the definition of pins to be
used in the multiplexing process.
o The wait variable sets the delay (in milliseconds) to be applied while switching
between LEDs.
2. Definition of the ledWrite Function:
o This function is used to control specific LED blocks. o Serial data is sent for
three blocks, and each block represents a number that determines which LEDs
will be active.
o The function sends this data serially to the pins and triggers the latch pin for
the next update by making it low and high.
3. setup Function:
o This function runs once at the beginning of the program. o pinMode functions
ensure that the pins to be used are set as output.
4. Main Loop - loop Function:
o This section operates continuously until the power of the Arduino is turned off.
o Initially, it lights up and turns off different LED combinations in a specific
sequence.
o For each LED combination, the ledWrite function is called, and it is specified
which LEDs will light up.
o Between each LED lighting operation, a defined period is waited using the
delay function. This makes the blinking of LEDs visible to the eye.
o Example: The command ledWrite(128,0,0); causes the first LED in the first
block to light up while keeping the LEDs in other blocks off. Similarly, other
ledWrite commands activate different LED combinations.
5. Control of the LEDs:
o The program continues with the LEDs blinking in a specific pattern. This
process is done with a certain waiting period so that users can see the LEDs
blinking.
o This loop continuously repeats until the Arduino device is turned off.
The purpose of the program is to create a visual output by controlling the blinking of various
LEDs. This can be used for various applications such as entertainment, education, art projects,
or status display.

223
Microcontrollers for Steam-AI Students Kamil Bala

Questions About the Circuit:

1. If this system is to be used as a visual alert, how can the blinking frequency of the
LEDs be optimized to attract people’s attention?
2. What kind of visual patterns or signals could be created using a different LED
arrangement instead of the current LED array used in the system?
3. By expanding this project, what kinds of real-world applications or interactive art
installations could be created?
4. In terms of energy consumption, what kind of energy costs would running this system
continuously incur, and what could be done to reduce these costs?
5. If one wants to use many more LEDs in the system, what changes should be made in
the current code structure, and what would be the implications on the hardware?
6. Consider how this LED control system could be modified to allow user interaction.
For example, allowing users to choose which LEDs will blink.
7. The current system offers only simple blinking functionality. How could you expand
this system to produce more complex visual effects, such as color changing or light
intensity control?
8. Are there any security risks in this project? If so, how can these risks be minimized?
9. Considering the environmental impacts, how can you increase the sustainability of
such an LED system?
10. What types of failures can be encountered in this system, and what can be proactively
done to prevent these failures?

224
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Optimization of the LEDs’ blinking frequency: In visual alert systems, high


contrast and distinguishable patterns are often used to capture human attention. The
blinking rate should be set at a frequency that is comfortable for the human eye to
follow without causing eye fatigue. In an ideal scenario, this speed should be
adjustable by the user or automatically set according to a specific situation.
2. Different LED arrangements: Various visual patterns can be created through
different LED arrangements. For example, patterns representing specific shapes like
hearts, arrows, or letters can be created. In more advanced projects, LED matrices can
even produce moving images.
3. Real-world applications: These kinds of projects can be used in various areas such as
interactive art installations, educational tools, advertising billboards, traffic signals, or
even shows in amusement parks. Creativity makes the possibilities endless for where
this system can be applied.
4. Energy consumption: Energy consumption will vary depending on the number and
brightness of the LEDs used. Selecting more efficient LEDs, utilizing low-power
modes, and activating the system only when necessary with motion-sensitive sensors
can reduce energy costs.
5. Integration of more LEDs: When adding more LEDs, modifications regarding power
requirements and management, as well as the amount of data the microcontroller can
handle, must be made. Most likely, a more advanced power management system and
possibly additional pins or extra hardware modules for controlling the LEDs will be
needed.
6. User interaction: Adding a user interface could allow people to choose which LEDs
will blink. This could be achieved through physical buttons, touch screens, or an app
on a smartphone.
7. Complex visual effects: By using color-changing LEDs (RGB LEDs) and drivers that
can change light intensity, you could create color transitions, brightness variations,
and even light shows that respond to music or external sensors.
8. Security risks: Safety risks could include electric shock, overheating, and electrical
short circuits. To minimize these, appropriate safety measures should be taken, and all
electrical components should be properly insulated.
9. Sustainability: Using energy-saving LEDs, powering from renewable energy sources
like solar, and ensuring the recyclability of materials could reduce the project’s
environmental impact.
10. Failure prevention: Failures in the system often stem from electrical errors or wear
and tear on hardware. Regular maintenance, the use of quality components, and safety
precautions like overcurrent protection can help prevent such issues.

225
Microcontrollers for Steam-AI Students Kamil Bala

1.2.33. Dynamic LED Snake Animation with Arduino 2-13


https://www.tinkercad.com/things/gkuU96BA5pb-1233-dynamic-led-snake-
animation-with-arduino-2-13

/*************************************************************************
Program Name: Dynamic LED Snake Animation with Arduino 2-13

Program Objective: We ensure that the LEDs accumulate at the end in a


cascading manner.
Written by: Kamil Bala
kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
// Array for LED states (0: off, 1: on)
// These values should be updated according to your dataset.
int led_states[][12] = {
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 1. cycle
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 2. cycle
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 3. cycle
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 4. cycle
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // 5. cycle
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // 6. cycle
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, // 7. cycle
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // 8. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // 9. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, // 10. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, // 11. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 12. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 2. cycle
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 3. cycle
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, // 4. cycle
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1}, // 5. cycle
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}, // 6. cycle
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, // 7. cycle
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1}, // 8. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1}, // 9. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1}, // 10. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // 11. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // 1. cycle
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // 2. cycle
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1}, // 3. cycle
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1}, // 4. cycle
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1}, // 5. cycle
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1}, // 6. cycle
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1}, // 7. cycle
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1}, // 8. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1}, // 9. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, // 10. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, // 1. cycle
{0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, // 2. cycle

226
Microcontrollers for Steam-AI Students Kamil Bala

{0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1}, // 3. cycle


{0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1}, // 4. cycle
{0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1}, // 5. cycle
{0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1}, // 6. cycle
{0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1}, // 7. cycle
{0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1}, // 8. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, // 9. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, // 1. cycle
{0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}, // 2. cycle
{0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1}, // 3. cycle
{0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1}, // 4. cycle
{0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1}, // 5. cycle
{0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1}, // 6. cycle
{0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1}, // 7. cycle
{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, // 8. cycle
{1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, // 1. cycle
{0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, // 2. cycle
{0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1}, // 3. cycle
{0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1}, // 4. cycle
{0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1}, // 5. cycle
{0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1}, // 6. cycle
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // 7. cycle
{1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // 2. cycle
{0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // 3. cycle
{0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1}, // 4. cycle
{0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1}, // 5. cycle
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, // 6. cycle
{1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}, // 2. cycle
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1}, // 3. cycle
{0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1}, // 4. cycle
{0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, // 5. cycle
{1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}, // 2. cycle
{0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, // 3. cycle
{0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 4. cycle
{1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 2. cycle
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 3. cycle
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 2. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0. cycle
};

void setup() {
// Set the pin modes
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT);
}
}

227
Microcontrollers for Steam-AI Students Kamil Bala

void loop() {

int total_cycles = sizeof(led_states) / sizeof(led_states[0]); // Total


number of cycles

for (int cycle = 0; cycle < total_cycles; cycle++) {


for (int led = 2; led <= 13; led++) {
// Set the LED to the relevant state
digitalWrite(led, led_states[cycle][led - 2] ? HIGH : LOW);
}

delay(200); // // Wait until the next cycle


}
delay(400); // // Wait until the next cycle

228
Microcontrollers for Steam-AI Students Kamil Bala

General Description of the Program:

This Arduino program controls the blinking of LEDs within a specific sequence. The program
relies on a predefined sequence of states determining when and how each LED will light up or
go out. Each LED is connected to a specific digital pin on the Arduino, and the program
controls the LEDs by altering the electrical state of these pins.
1. Setup Phase:
o Firstly, the setup function makes the necessary initial settings. In this case,
specific digital pins are initialized in output mode. These pins are where the
LEDs are connected, so they must be set as outputs to control them.
2. Main Loop (Loop) Phase:
o The loop function is the code that Arduino continuously runs. Within this
function, the blinking of LEDs in a specific sequence is controlled.
o A multi-dimensional array named ‘led_states’ contains a series of states
indicating the status (on or off) of each LED. These states are applied
sequentially with each cycle of the program.
o In each cycle, the program retrieves a specific set of LED states from the array
and applies these states to the corresponding LEDs. This is done using the
digitalWrite function, which sets whether a specific pin is HIGH (on) or LOW
(off).
o After each cycle, the program waits for a specific period before proceeding to
the next cycle. This is achieved with the delay function, providing a visible
delay between the blinking of the LEDs. o After all the cycles are completed,
the program returns to the beginning and repeats the process.
This program is useful for demonstrating how LEDs will blink according to a specific
scenario and can be used in interactive projects, artworks, or educational simulations. The
blinking pattern of the LEDs can be easily customized by changing the values in the
‘led_states’ array.

229
Microcontrollers for Steam-AI Students Kamil Bala

Step-by-Step Explanation:

This program creates a visual display by making the LEDs blink in a specific pattern. Here’s a
step-by-step explanation of the program:
Setup Function: setup()
1. Setting the Pin Modes:
o The ‘for’ loop is used to initialize certain pins on the Arduino (in this case,
from 2 to 13) in output mode.
o The pinMode function declares each pin to be used as an output. This means
the Arduino can send electrical signals through these pins.
Main Loop Function: loop()
1. Calculating the Total Number of Cycles:
o The ‘total_cycles’ variable is used to calculate the total number of cycles
(blinking LED patterns) in the ‘led_states’ array.
2. Main LED Control Loop:
o The outer ‘for’ loop (for each cycle) is designed to process each LED blinking
cycle in sequence.
o This loop repeats for all cycles defined in the ‘led_states’ array.
3. Setting the State of Each LED:
o The inner ‘for’ loop (for each LED) sets the state of each LED in the current
cycle
o The digitalWrite function determines whether a specific LED (pin) is HIGH
(on) or LOW (off), depending on the corresponding state in the ‘led_states’
array.
o The state of each LED is set according to its position in the cycle.
4. Adding a Delay:
o The ‘delay(200)’ statement adds a pause between cycles. This provides a
visually perceptible delay in the blinking of the LEDs, allowing the human eye
to follow the blinking LEDs.
5. Transition to the Next Cycle:
o When each iteration of the outer ‘for’ loop ends, the loop function returns to
the beginning, and the process repeats with a new set of cycles.
o The ‘delay(400)’ statement provides an additional pause before moving on to
the next set of cycles.
These steps allow the program to create a complex light show by controlling each LED. Each
“cycle” set represents the blinking of the LEDs in a specific pattern, defined within the
‘led_states’ array. The program continually repeats these cycles, presenting an ongoing LED
light show.

230
Microcontrollers for Steam-AI Students Kamil Bala

Questions about the Circuit:

1. Performance Optimization: What are the effects of delays in the program on system
performance, and how can these delays be optimized?
2. Debugging: If the LEDs do not flash in a sequential order as expected, what steps
would you follow to diagnose and solve the problem?
3. Code Expansion: How could you adapt this program to be compatible with a system
that has different flashing patterns or a different number of LEDs?
4. Memory Management: What effects could increasing the size of the led_states array
have on Arduino’s memory management, and how could you manage this situation?
5. Functional Development: If you wanted to add a new feature to the program (for
example, a button control for a specific pattern), how would you design and
implement this change?
6. Power Management: What concerns would you have regarding the energy
consumption of such a continuously operating system, and what strategies would you
consider to increase energy efficiency?
7. User Interaction: What steps would you take to make this project an interactive setup
where users can control the flashing pattern or speed of the LEDs?
8. Test Processes: What kind of test scenarios would you develop for such a program,
and how would you verify that the program works correctly?
9. Security Measures: In a physical setup, especially in public spaces, what measures
might you need to take to ensure the safety of such an LED arrangement?
10. Data Integration: How could this system be expanded or adapted to change LED
patterns based on data received from an external data source (e.g., music, weather
information, etc.)?

231
Microcontrollers for Steam-AI Students Kamil Bala

Answers

1. Performance Optimization: Delays in the program, especially in sequential LED


transitions, can cause noticeable latency. To optimize delays, a non-blocking code
structure can be used (for example, by controlling timing with the millis() function),
allowing the program to continue with other tasks, making the system more
responsive.
2. Debugging: If the LEDs are not working properly, the first step is to check the
connections and the code. Debugging can be done by measuring the voltage in the
circuit with a multimeter, physically inspecting the LEDs and connections, and adding
logs or using a serial monitor in the code.
3. Code Expansion: If you want to add different patterns or more LEDs, it would be
beneficial to make the led_states array dynamic and use a separate configuration tool
(like a function) for the patterns. This way, the user can change patterns without
altering the code.
4. Memory Management: If the size of the led_states array increases, it could fill up
Arduino’s limited RAM memory. To manage this issue, you could break data into
smaller pieces or save fixed data to flash memory using features like PROGMEM.
5. Functional Development: Adding a new feature, like a button, would change the
current structure of the code. A mechanism will be added within the main loop to
continuously check the button’s state, and a piece of code will be written to trigger a
specific function (like changing the pattern) when the button is pressed.
6. Power Management: Energy consumption is a critical factor, especially for battery-
operated systems. Strategies to increase energy efficiency could include reducing the
brightness of LEDs, shutting down unused hardware, or employing low-power modes
like sleep mode.
7. User Interaction: To facilitate user interaction, an analog input like a potentiometer
or a series of buttons could be used. This would allow the user to change the speed or
color of the LED patterns in real-time.
8. Test Processes: Test scenarios should cover different LED states, transition times,
response times, etc. The system’s correct operation can be ensured through automated
tests or manual checklists.
9. Security Measures: Safety is crucial in public spaces. Considerations could include
protective casings to prevent electric shocks, proper cable management, and quick
shutdown mechanisms for emergencies.
10. Data Integration: The system could connect to external data sources through web
scraping, API usage, or data from sensors. This data could be used to alter the behavior
of LEDs based on specific conditions, making the system more interactive and dynamic

232
Microcontrollers for Steam-AI Students Kamil Bala

1.2.34. Dynamic LED Snake Animation with Arduino 13-2

https://www.tinkercad.com/things/5bV8U1Jemim-1234-dynamic-led-snake-animation-with-
arduino-13-2

/*************************************************************************
Program Name: Dynamic LED Snake Animation with Arduino 13-2

Program Objective: We are making the LEDs accumulate at the end like in a
cascade.

Written by: Kamil Bala


kamilbala42@gmail.com
tw: @tek_elo
Yalova / 2023
***************************************************************************
***/
// Array for LED states (0: off, 1: on)

int led_states[][12] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, // 2. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, // 3. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // 4. cycle
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // 5. cycle
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, // 6. cycle
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // 7. cycle
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // 8. cycle
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 9. cycle
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 10. cycle
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 11. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 12. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, // 2. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, // 3. cycle
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // 4. cycle
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // 5. cycle
{1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, // 6. cycle
{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // 7. cycle
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // 8. cycle
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 9. cycle
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 10. cycle
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 11. cycle
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, // 2. cycle
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, // 3. cycle
{1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // 4. cycle
{1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // 5. cycle
{1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, // 6. cycle
{1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // 7. cycle
{1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // 8. cycle
{1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 9. cycle

233
Microcontrollers for Steam-AI Students Kamil Bala

{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 10. cycle


{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0}, // 2. cycle
{1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0}, // 3. cycle
{1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // 4. cycle
{1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // 5. cycle
{1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0}, // 6. cycle
{1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0}, // 7. cycle
{1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // 8. cycle
{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, // 9. cycle
{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0}, // 2. cycle
{1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0}, // 3. cycle
{1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0}, // 4. cycle
{1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0}, // 5. cycle
{1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0}, // 6. cycle
{1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0}, // 7. cycle
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, // 8. cycle
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1}, // 1. cycle
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0}, // 2. cycle
{1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0}, // 3. cycle
{1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0}, // 4. cycle
{1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0}, // 5. cycle
{1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0}, // 6. cycle
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // 7. cycle
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1}, // 1. cycle
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0}, // 2. cycle
{1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0}, // 3. cycle
{1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0}, // 4. cycle
{1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0}, // 5. cycle
{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 6. cycle
{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1}, // 1. cycle
{1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0}, // 2. cycle
{1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0}, // 3. cycle
{1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0}, // 4. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0}, // 5. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1}, // 1. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0}, // 2. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0}, // 3. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, // 4. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1}, // 1. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0}, // 2. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, // 3. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, // 1. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}, // 2. cycle
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 1. cycle
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 0. cycle

};

void setup() {
// Set up pin modes
for (int i = 2; i <= 13; i++) {
pinMode(i, OUTPUT);

234
Microcontrollers for Steam-AI Students Kamil Bala

}
}

void loop() {
int total_cycles = sizeof(led_states) / sizeof(led_states[0]); // Total
number of cycles

for (int cycle = 0; cycle < total_cycles; cycle++) {


for (int led = 2; led <= 13; led++) {
// Set the LED to the corresponding state
digitalWrite(led, led_states[cycle][led - 2] ? HIGH : LOW);
}

delay(200); // Wait until the next cycle


}
delay(400); // Wait until the next cycle

235

You might also like