Document1 |Compatibility Mode] - Word es Mailings Review View Tell me what you w
ID: 3754280 • Letter: D
Question
Document1 |Compatibility Mode] - Word es Mailings Review View Tell me what you want to do ··-11Normal|No Spac Heading 1 Heading 2 Title Paragraph Styles Hw Troubleshoot the following Arduino code and comment out each line to desecribe what they mean const int LED1 13 const int buttonpn-2; void setup t//initialize digital pin as an output. pinMode (LEDI, OUTPUT) pnMode (but tonpin, INPUT); Serial.begin (9600) void loop t//runs loop forover static int state, old state, counteri if (digita 1 Read (buttonPin)-1) state = 1; else if (state-1) olse if state0: digitalWrite (LED1, digita I write(LEDI, HIGH) LOR); (old-state ! state) counter++i Serial-print ("counts")a Serial-printlin (counter) i old state statei e,Explanation / Answer
This is a program developed in Aurdino which turns on and off a pin based on the input on another pin and prints the number of alternate times the pin is turned ON. If this was your requirement, then the code is perfectly fine.
Let us understand the program line by line:
Line1: const int LED1 = 13;
Digital pin 13 has been given name as LED1
Line 2: const int buttonPin = 2;
Digital pin 2 has been given name as buttonPin
Line 3: void setup()
The code between the curly braces runs only once for setup()
Line 4: { pinMode (LED1, OUTPUT);
Initialize the digital pin as an output
Line 5: pinMode (buttonPin, INPUT);
Initialize the digital pin as an Input
Line 6: Serial.begin(9600)
Initialize serial communication at 9600 bits per second
}
Line 7: void loop()
Whatever code you put here is executed over and over
{
Line 8: static int state, old_state, counter;
Flag fields to determine state and a field to count
Line 9: if (digitalRead(buttonPin) == 1)
Reads the value from a specified digital pin (here buttonPin value is being checked, 0 is for Low and 1 is for High), either HIGH or LOW
Line 10: state = 1;
If High, set state as 1
else
Line 11: state = 0;
If Low, set state as 1
Line 12: if (state == 1)
If value of state is 1
Line 13: digitalWrite(LED1, HIGH);
It is used to write a HIGH or a LOW value to a digital pin, i.e. if a pin is set high, the led connected to it will glow [If value of state is 1, Turn pin 13 as High]
else
Line 14: digitalWrite(LED1, Low);
[If value of state is 1, Turn pin 13 as High]
Line 15: if (old_state != state)
This flag is set below, i
{
Line 16: counter++;
It will increase the value of counter by 1.
Line 17: Serial.print("counts");
Serial is used to set communication between the Arduino board and a computer or other devices. Serial.print prints data to the serial port as human-readable ASCII text. Here it will print the word "counts"
Line 18: Serial.println(counter);
Serial.println prints data to the serial port as human-readable ASCII text followed by a new line. Here it will print the value of
"counter" in next line.
Line 19: old_state = state;
It will set the value of old_state flag equal to state
}
}
Now, for better understanding as a coder, I will provide it as a comment in the code itself as mentioned below:
const int LED1 = 13; // Digital pin 13 has been given name as LED1
const int buttonPin = 2; // Digital pin 2 has been given name as buttonPin
void setup() // the code between the curly braces runs only once for setup()
{ pinMode (LED1, OUTPUT); // Initialize the digital pin as an output
pinMode (buttonPin, INPUT); // Initialize the digital pin as an Input
Serial.begin(9600) // Initialize serial communication at 9600 bits per second
} //
void loop() // whatever code you put here is executed over and over
{
static int state, old_state, counter; // Flag fields to determine state and a field to count
if (digitalRead(buttonPin) == 1) // Reads the value from a specified digital pin (here buttonPin value is being checked, 0 is for Low and 1 is for High), either HIGH or LOW
state = 1; // If High, set state as 1
else
state = 0; // If Low, set state as 1
if (state == 1)// If value of state is 1
digitalWrite(LED1, HIGH); // It is used to write a HIGH or a LOW value to a digital pin, i.e. if a pin is set high, the led connected to it will glow [If value of state is 1, Turn pin 13 as High]
else
digitalWrite(LED1, Low); // [If value of state is 1, Turn pin 13 as High]
if (old_state != state) // This flag is set below, i
{
counter++; // It will increase the value of counter by 1.
Serial.print("counts"); // Serial is used to set communication between the Arduino board and a computer or other devices. Serial.print prints data to the serial port as human-readable ASCII text. Here it will print the word "counts"
Serial.println(counter); // Serial.println prints data to the serial port as human-readable ASCII text followed by a new line. Here it will print the value of
"counter" in next line.
old_state = state; // It will set the value of old_state flag equal to state
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.