Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please help me to write a code of this The Lab Our goal is to make an LED and a piezo element blink and click,
please help me to write a code of this
The Lab Our goal is to make an LED and a piezo element blink and click, respectively, for 10ms at a varying interval. The functions we need for this lab are pinMode, analogRead, digitalWrite, and delay - delay simply accepts an integer specifying how many milliseconds to wait. delay(10) will stop the code from running for 10 milliseconds, analogRead works just like digitalRead worked in the last lab, except it will return an integer approximation instead of a boolean true / false. For example, int value = analogRead(); will read the value from Ao into value. You've already used digitalWrite and pinMode, but to recap, digitalWrite sets a pin to either 5 volts or 0 volts. pinMode will let you set a pin to be an INPUT or an OUTPUT. Pin Ao is an analog input, so you won't set the pinMode on it. It cannot be anything else, at least as far as we're concerned. There are advanced ways to re-use analog inputs, but don't worry about them here. Therefore, all you have to worry about is making the LED and Piezo into outputs Useful tip: HIGH is another way to write 1, and low is another way to write o on the Arduino. Try to avoid magic numbers as much as possible, which are numbers that seem to come from nowhere. digitalWrite(firstLED, HIGH) is much easier to understand than digitalWrite(10, 1). Before the void setup() function, you can declare "global variables" simply by writing code like int firstLED = 10; . Try it! It will make your code better. To do this lab, we want to analogRead the value of the potentiometer, turn on the LED and the Piezo, delay for 10ms, then turn off the LED and the Piezo. We then want to delay for however many milliseconds the value of the potentiometer was. If the potentiometer is about halfway, we'll be delaying for about 500ms, or half a second, for example. Now, we want to compress the range of values. Having it wait a full second between blinking and clicking is boring! So, if you divide the analog value by 20, the longest the system will wait is one twentieth of a second, which is much better. Try it out, and notice how you have much finer control over the higher-speed clicking and blinking, and there is no way to slow it down too much. Let's try dividing by 80. Do that, and you'll notice that as you move the potentiometer back and forth, you just seem to be adjusting the brightness of the LED! This is related to a concept called pulse width modulation, but we won't talk about that today. Feel free to research it though! The final step in this lab is to make it so that the LED is still visibly blinking, but we have our fine control over the speed. So, let's do the division by 40 this time and let's add 20 to the value after the division and run it. Troubleshooting Tips 1. Make sure that the potentiometer is connected to ao! The Lab Our goal is to make an LED and a piezo element blink and click, respectively, for 10ms at a varying interval. The functions we need for this lab are pinMode, analogRead, digitalWrite, and delay - delay simply accepts an integer specifying how many milliseconds to wait. delay(10) will stop the code from running for 10 milliseconds, analogRead works just like digitalRead worked in the last lab, except it will return an integer approximation instead of a boolean true / false. For example, int value = analogRead(); will read the value from Ao into value. You've already used digitalWrite and pinMode, but to recap, digitalWrite sets a pin to either 5 volts or 0 volts. pinMode will let you set a pin to be an INPUT or an OUTPUT. Pin Ao is an analog input, so you won't set the pinMode on it. It cannot be anything else, at least as far as we're concerned. There are advanced ways to re-use analog inputs, but don't worry about them here. Therefore, all you have to worry about is making the LED and Piezo into outputs Useful tip: HIGH is another way to write 1, and low is another way to write o on the Arduino. Try to avoid magic numbers as much as possible, which are numbers that seem to come from nowhere. digitalWrite(firstLED, HIGH) is much easier to understand than digitalWrite(10, 1). Before the void setup() function, you can declare "global variables" simply by writing code like int firstLED = 10; . Try it! It will make your code better. To do this lab, we want to analogRead the value of the potentiometer, turn on the LED and the Piezo, delay for 10ms, then turn off the LED and the Piezo. We then want to delay for however many milliseconds the value of the potentiometer was. If the potentiometer is about halfway, we'll be delaying for about 500ms, or half a second, for example. Now, we want to compress the range of values. Having it wait a full second between blinking and clicking is boring! So, if you divide the analog value by 20, the longest the system will wait is one twentieth of a second, which is much better. Try it out, and notice how you have much finer control over the higher-speed clicking and blinking, and there is no way to slow it down too much. Let's try dividing by 80. Do that, and you'll notice that as you move the potentiometer back and forth, you just seem to be adjusting the brightness of the LED! This is related to a concept called pulse width modulation, but we won't talk about that today. Feel free to research it though! The final step in this lab is to make it so that the LED is still visibly blinking, but we have our fine control over the speed. So, let's do the division by 40 this time and let's add 20 to the value after the division and run it. Troubleshooting Tips 1. Make sure that the potentiometer is connected to aoStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started