Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
We check if the sensor value is greater than 0 to ensure it is valid.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The loop continues while sensorValue equals zero, retrying the read.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We check if sensorValue equals 0, which indicates a failure.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will invert the logic.
Using '==' or '!=' will only check equality, not greater than.
✗ Incorrect
The ternary operator checks if sensorValue is greater than threshold to set status.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will invert the logic and cause wrong behavior.
Mixing operators will cause syntax or logic errors.
✗ Incorrect
All conditions check equality to detect error and print message.