Show work please Problem2 Consider the following example code, which assumes a p
ID: 3348747 • Letter: S
Question
Show work please
Problem2 Consider the following example code, which assumes a potentiometer is attached to analog pin A0 of the Arduino I/ These constants won' t change const int analogPinA0; const int ledPin 13 const int threshold 400 Ian arbitrary threshold level that's in // pin that the sensor is attached to // pin for the Arduino built-in LED // the range of the analog input void setup) // initialize the LED pin as an output pinMode(ledPin, OUTPUT) // initialize serial communications: Serial.begin(9600) void loop() // read the value of the potentiometer int analogValueanalogRead(analogPin): // if the analog value is high enough, turn on the LED: if (analogValue> threshold) digitalWrite(ledPin, HIGH) else f digitalWrite(ledPin, LOW) /I print the analog value Serial.println(analogValue); delay(1); // delay in between reads for stability (a) The analogRead() function returns a value from 0 to 1023. Change the code so that pins 2 to 9 are set to output mode. Use the map function to change the output from the range (0, 1023) to the range (0, 8). Use this result to turn on the corresponding LED. For example, if the results is 0, then LED 2 should turn on, if it is 1, then LED 3 should turn on, etc.The result should be an 8-level analog display of the voltage output from the potentiometerExplanation / Answer
const int analogPin= A0;
const int ledPin = 13;
const int threshold = 400;
void setup()
{
for (int apin=2; apin<=9; apin++)
{ pinMode(apin,OUTPUT);}
Serial.begin(9600);
}
voidloop()
{
int analogValue = analogRead(analogPin);
int outputValue= map(analogValue,0,1023,0,8);
analogWrite(analogValue,outputValue);
for (int i=0,i<9,i++)
{
if (analogValue==i)
{ digitalWrite(i+2,HIGH);
Serial.println(analogvalue);}
i++;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.