
Lexi L.
asked 03/18/17Can you tell me line by line what my code does?
PinMode (5, INPUT);
PinMode (10, OUTPUT);
PinMode (13, OUTPUT);
Serial.begin (9600);
}
VoidLoop (){
Bool moistureDetected = digitalRead (5);
Serial.Println ("moisture reading: ");
Serial.Println (moisureDetected);
If (moistureDetected == false);
digitalWrite (10, HIGH) {
DigitalWrite (13, HIGH);
Delay (1000);
digitalWrite (13, LOW);
Delay (1000);
} else {
DigitalWrite (1, LOW);
}
Delay (300);
}
1 Expert Answer
Spencer B. answered 07/22/23
Computer Science and Cybersecurity Researcher
Assuming that this is written for the Arduino programming environment, Setup() and Loop() are the top-level functions which will be called after the microcontroller performs initialization and sets up the C call stack.
Setup() will be called once, following the reset of the microcontroller.
In this instance, it configures some pin states and initializes the serial UART peripheral.
Loop() runs continuously, it executes the function as quickly as it can and as soon as control flow reaches the last instruction in the function it returns to the start of the function and executes again. This happens until the microcontroller is reset or loses power. It can also be paused temporarily if an interrupt occurs.
This one appears to have some errors.
The intent seems to be that a digital pin is read periodically, and then based on that value some other pins are set. Digital pin 13 is set HIGH for a period of approximately 1 second, digital pin 10 is forever set to HIGH if there is ever moisture detected.
However, that is not what the program as-written does.
It has several syntax errors which will result in failure to compile. First, the if() is followed by a semicolon on the same line, rather than the intended open/left bracket. That results in the if statement essentially doing nothing, and the following lines to always be executed. Additionally, because there is a closing-bracket and then an else statement, that bracketed else statement doesn't have a matching if statement preceding it, so it will throw a syntax error at compile-time.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Tim C.
03/18/17