Assume that a speaker is connected to pin 2 of the Arduino microcontroller that
ID: 2084205 • Letter: A
Question
Assume that a speaker is connected to pin 2 of the Arduino microcontroller that you can generate an alarm sound by cycling this bit on and off, at a particular period controlled by a delay loop. Write a main program that uses for loop(s) and calls the Delay Subroutine available in the Arduino with the appropriate parameter to generate the alarm sound that has the characteristics shown in Figure 1: For the problem above give the following The flow diagram for the program? The number of repetitions of the For-Loop(s)? The main program in C language instructionsExplanation / Answer
1) As I don't have pen and paper to show in image form, I am explaining in text form:
Process Flow Diagram:
1. Given period of the pulse as 5microsecond. We have 1s=10^6microsecond,
hence calculate the number of 5microsecond pulses required to get 1s.
i.e. n=10^6/5=2,00,000 number of 5microsecond pulses to become 1s.
2.Now declare variable i as long instead of int datatype as it has to hold a number which is greater than 32-bit.
3. In setup() set pin 2 mode as OUTPUT.
4. In loop() take a for loop with length from 1 to 200000.
5. Within for loop set pin 2 output has HIGH and give a delay of 2microseconds and again set pin 2 output as LOW and give a delay of 3microseconds such that total pulse period is 5microseconds.
6. When these 5microseconds pulse is executed 200000 times using for loop then it becomes 1s.
2) Number of for loops:
We have 1s=10^6microsecond,
hence calculate the number of 5microsecond pulses required to get 1s.
i.e. n=10^6/5=2,00,000 number of 5microsecond pulses to become 1s.
n is number of required for loops in void loop() function.
3) Program in C language instructions:
long i;
void setup() {
// put your setup code here, to run once:
pinMode(2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for(i=1;i<200001;i++){
digitalWrite(2, HIGH);
delayMicroseconds(2);
digitalWrite(2, LOW);
delayMicroseconds(3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.