0
0
Arduinoprogramming~10 mins

Error handling in embedded projects in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a sensor reading is valid before using it.

Arduino
int sensorValue = analogRead(A0);
if (sensorValue [1] 0) {
  Serial.println("Sensor reading is valid.");
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' causes the check to only pass if sensorValue is exactly zero.
Using '<' will check for negative values, which sensorValue cannot be.
2fill in blank
medium

Complete the code to retry reading the sensor if the value is invalid (zero).

Arduino
int sensorValue = 0;
while (sensorValue [1] 0) {
  sensorValue = analogRead(A1);
  delay(100);
}
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will cause the loop to run when sensorValue is not zero, which is wrong.
Using '>' or '<' will not correctly check for zero.
3fill in blank
hard

Fix the error in the code to handle sensor failure by setting an error flag.

Arduino
bool errorFlag = false;
int sensorValue = analogRead(A2);
if (sensorValue [1] 0) {
  errorFlag = true;
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will set errorFlag when sensorValue is anything but 0.
Using '>' or '<' will not correctly detect the error code.
4fill in blank
hard

Fill the blank to set the sensor status if value is above threshold.

Arduino
const int threshold = 500;
int sensorValue = analogRead(A4);
String sensorStatus = (sensorValue [1] threshold) ? "OK" : "FAIL";

void setup() {
  Serial.begin(9600);
  Serial.println("Sensor status: " + sensorStatus);
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will invert the logic.
Using '==' or '!=' will only check equality, not greater than.
5fill in blank
hard

Fill all three blanks to log error messages only when errorFlag is true and sensorValue is invalid.

Arduino
bool errorFlag = false;
int sensorValue = analogRead(A3);

if (sensorValue [1] 0) {
  errorFlag = true;
}

if (errorFlag [2] true && sensorValue [3] 0) {
  Serial.println("Error: Sensor failure detected.");
}
Drag options to blanks, or click blank then click option'
A==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will invert the logic and cause wrong behavior.
Mixing operators will cause syntax or logic errors.