0
0
Arduinoprogramming~5 mins

Rising, falling, and change triggers in Arduino - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Rising, falling, and change triggers
O(n)
Understanding Time Complexity

When using rising, falling, or change triggers in Arduino, we want to know how the program's work grows as input signals change.

We ask: How often does the code run when signals trigger events?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


volatile int count = 0;

void setup() {
  pinMode(2, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), countRising, RISING);
}

void loop() {
  // main code runs here
}

void countRising() {
  count++;
}
    

This code counts how many times a signal rises on pin 2 using an interrupt trigger.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The interrupt function countRising() runs each time the signal rises.
  • How many times: It runs once per rising edge detected on the input pin.
How Execution Grows With Input

The number of times countRising() runs grows directly with the number of rising signals.

Input Size (n)Approx. Operations
10 rising signals10 calls to countRising()
100 rising signals100 calls to countRising()
1000 rising signals1000 calls to countRising()

Pattern observation: The work grows linearly with the number of signal changes.

Final Time Complexity

Time Complexity: O(n)

This means the program's work increases directly with the number of signal triggers.

Common Mistake

[X] Wrong: "The interrupt function runs constantly like a loop."

[OK] Correct: The interrupt only runs when the signal changes, not continuously, so work depends on signal events, not time.

Interview Connect

Understanding how event-driven code runs helps you explain how programs handle real-world signals efficiently.

Self-Check

"What if we changed the trigger from RISING to CHANGE? How would the time complexity change?"