What will the output of the code be if the object is continuously moving towards the sensor? [code] int op = 6; int isObstacle = HIGH; void

Trần Vũ

New member
What will the output of the code be if the object is continuously moving towards the sensor?
Code:
int op = 6;
int isObstacle = HIGH;
void setup() {
    pinMode(op, INPUT);
    Serial.begin(9600);
} 
void loop() { 
    isObstacle = digitalRead(op);
    if (isObstacle == LOW) { 
        Serial.println("1+"); 
    } 
    else {
        Serial.print("clear+"); 
    } 
    delay(200);
}
A. clear+clear+1+1
B. 1+1+clear+clear
C. clear+clear+clear+clear
D. 1+1+clear+1
 
Answer: A
Explanation: If the object is moving continuously towards the sensor, then within the four cycles of the given output, the first half of the set sees the object beyond the range so it will print a “clear+”, while within the second half of the code, the object has already moved inside the range of the sensor so it gives the “1+” signal.
 

Tìm kiếm

Back
Top