Modify the DigitalReadSerial program such that your program response to the push
ID: 638533 • Letter: M
Question
Modify the DigitalReadSerial program such that your program response to the push button
in the following way:
(1) when not pressed, you program should repeatedly print out
number from 0 to 100, increase by 1 per loop. The output should go to
the serial port so that it can be observed from arduino. use proper delay
(2) when push bottom is pressed, the counter should be reset to 0, and the future
loop should count up to the value of the counter at the time the bottom is pressed.
Example output:
0 1 2 3 4 ...100 0 1 2 3 ... 43 (button pressed) 0 1 2 ... 43 0 1 2 ... 43 0 1 2 ... 29 (button pressed again) 0 1 2 ... 29 ....
//Code Starts Here -------------------------------------------------------------------------
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
//End of Program------------------------------------------------------------------------------------------
Picture of Code
Explanation / Answer
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.