What is the value of “val” in the following program if the pin 12 is given 5V 3 times? [code] int val=0; void setup() { Serial.begin(9600);

What is the value of “val” in the following program if the pin 12 is given 5V 3 times?
Code:
int val=0;
void setup() {
    Serial.begin(9600);
    PinMode(13,INPUT);
}
void loop() {
    int s = digitalRead(13);
    if(s==1){
        val=val+1;
    }
}
A. null
B. 113
C. 3
D. 2
 
Answer: C
Explanation: In the program the variable “val” is declared as a global variable, I.e. its value can be accessed by all functions in the current program. This variable is then setup to 1 higher than itself every time the digitalRead() function detects a HIGH or a 5V signal at its end. Hence raising the value every time a 5V signal is applied.
 

Tìm kiếm

Back
Top