2. Completely describe the purpose of the following program (4pts) ch? r6A§ cons
ID: 3706270 • Letter: 2
Question
2. Completely describe the purpose of the following program (4pts) ch? r6A§ const int ledPin 13: const int sensorPin -0: // the next two lines set the nin and max delay betireen blinks const int minDuration-100; const int naxDuration -1000 void setup ) pinMode (ledPin, OUTPUT) enable output on the led pin Serial.begin (9600): // initialize Serial void loop ) int rate-analogRead (sensorPin): 11 read the analog input // the next line scales the blink rate between the nin and max values rate = map (rate, 200,800,mìnDuration, maxDuration); Serial.printin (rate): digitalWrite (ledPin, HIGH): delay (rate): digitalWrite (ledPin, LOW) delay (rate)?Explanation / Answer
This program implements led blinking by inserting different delay intervals in each iteration/operation of blinking.
const int ledPin=13; // pin 13 will be set as output pin
const int sensorPin=0; // pin 0 is set as input pin
const int minDuration = 100; // minimum delay (used to scale input)
const int maxDuration = 1000; // maximum delay (used to scale input)
// Setup function is the starting point of this program and all the initialization are done in this function
// Setup function is executed only once in the beginning of execution
void setup() {
pinMode(ledPin, OUTPUT); // Set pin 13 as output pin
Serial.begin(9600); // Set data rate for serial data transmission to 9600
}
// This is main loop executed after setup(), statements in loop() will be executed continuously until board is turned off or reset
void loop() {
int rate = analogRead(sensorPin); // Read data from pin 0
// This function remaps a variable(1st argument) from some old range(200-800) to a new range(minDuration-maxDuration)
// It is used for scaling the range which rate can take value from
rate = map(rate, 200,800,minDuration,maxDuration);
Serial.println(rate); // Print new rate value over serial output
digitalWrite(ledPin,HIGH); // Turn on pin 13
delay(rate); // Sleep for time "rate"
digitalWrite(ledPin,LOW); // Turn off pin 13
delay(rate); // Sleep for time "rate"
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.