0
0
Embedded Cprogramming~10 mins

Start and stop conditions in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Start and stop conditions
Initialize variables
Check start condition
Yes
Start main operation
Check stop condition
No
Continue operation
Check stop condition
Yes
Stop operation and exit
The program waits for a start condition to begin operation, then repeatedly checks a stop condition to know when to end.
Execution Sample
Embedded C
int start = 0;
int stop = 0;

// Wait for start
while(!start) {
  start = check_start_signal();
}

// Run until stop
while(!stop) {
  perform_task();
  stop = check_stop_signal();
}
This code waits until a start signal is true, then runs a task repeatedly until a stop signal is true.
Execution Table
StepstartstopCondition CheckedAction TakenOutput
100start == 0Check start signalstart remains 0, loop continues
200start == 0Check start signalstart becomes 1, loop exits
310stop == 0perform_task() calledstop remains 0
410stop == 0perform_task() calledstop remains 0
510stop == 0perform_task() calledstop becomes 1, loop exits
611Loop endsStop operationProgram ends
exit11stop == 1Stop condition metExit main loop
💡 stop becomes 1, so stop == 0 condition is false and loop ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
start0011111
stop0000011
Key Moments - 2 Insights
Why does the program wait in the first loop before starting the main operation?
Because the start variable is 0 initially, the condition 'while(!start)' keeps the program waiting until start becomes 1, as shown in execution_table rows 1 and 2.
Why does the main loop stop running when stop becomes 1?
The main loop condition is 'while(!stop)', so when stop is 1, '!stop' is false, causing the loop to exit as shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'start' after step 2?
A1
B0
CUndefined
D2
💡 Hint
Check the 'start' column in execution_table row 2.
At which step does the stop condition become true, causing the main loop to exit?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'stop' column and 'Condition Checked' in execution_table rows.
If the start signal never becomes true, what happens to the program?
AIt runs the main loop anyway
BIt waits forever in the first loop
CIt exits immediately
DIt crashes
💡 Hint
Refer to execution_table rows 1 and 2 where start is checked.
Concept Snapshot
Start and stop conditions control program flow.
Use a loop to wait for start condition (e.g., while(!start)).
After start, run main loop until stop condition is true (e.g., while(!stop)).
Check conditions each iteration to decide when to start or stop.
This pattern ensures controlled operation start and safe stop.
Full Transcript
This example shows how a program waits for a start signal before running its main task. Initially, the variable 'start' is zero, so the program stays in the first loop checking for the start signal. Once 'start' becomes one, the program exits the waiting loop and enters the main loop. In the main loop, it repeatedly checks the 'stop' variable. As long as 'stop' is zero, the program performs its task. When 'stop' becomes one, the main loop ends and the program stops. This pattern helps control when a program begins and ends its operation safely.