Bird
0
0
Arduinoprogramming~10 mins

IR sensor for obstacle detection 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 read the IR sensor value.

Arduino
int sensorValue = analogRead([1]);
Drag options to blanks, or click blank then click option'
A13
BA0
C5
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using a digital pin number instead of an analog pin.
Forgetting to use analogRead for analog sensors.
2fill in blank
medium

Complete the code to set the IR sensor pin as input in setup().

Arduino
pinMode([1], INPUT);
Drag options to blanks, or click blank then click option'
AA0
B13
C7
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the wrong pin as input.
Using digital pins instead of the analog pin connected to the sensor.
3fill in blank
hard

Fix the error in the if statement to detect obstacle when sensor value is less than threshold.

Arduino
if (sensorValue [1] threshold) {
  Serial.println("Obstacle detected");
}
Drag options to blanks, or click blank then click option'
A<
B==
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than operator which triggers detection incorrectly.
Using equality operator which rarely matches exactly.
4fill in blank
hard

Fill both blanks to complete the code that reads sensor and prints "No obstacle" when value is above threshold.

Arduino
sensorValue = analogRead([1]);
if (sensorValue [2] threshold) {
  Serial.println("No obstacle");
}
Drag options to blanks, or click blank then click option'
AA0
B<
C>
D13
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pin number.
Using less than operator which means obstacle detected.
5fill in blank
hard

Fill all three blanks to create a dictionary-like structure that maps sensor states to messages.

Arduino
const char* messages[] = {"[1]", "[2]"};
int sensorValue = analogRead(A0);
Serial.println(sensorValue [3] threshold ? messages[0] : messages[1]);
Drag options to blanks, or click blank then click option'
AObstacle detected
BNo obstacle
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the messages order.
Using wrong comparison operator.