Question

5. Given the board setup in the figure, read the sketch. There are errors in the sketch. Correct them. You might add, delete, or modify. (15 points) 220 ohm 3V3 5V Vin Power 220 chm AST D13 AREF D12 Arduino D10 Potentiameser A1 D1- GND
media%2F842%2F8427acd6-02f9-42df-9896-50
Correct the errors in the code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

a). Modification required in Code:

1. LED pins should be 'OUTPUT' mode. Code should write logic high or low to ON or OFF LED.

2. analogRead should be used for reading sensor value instead of analogwrite

3. 'int thisLed' global variable is not required.

b). Optional modifications required for better code readibility:

1. Braces { } 'for' loop in setup function

2. Braces { } if & else in loop function

c). Modified Code:

const int analogPin = A0;
const int ledCount = 10;
int ledPins[] = {2,3,4,5,6,7,8,9,10,11};

void setup() {

int thisLed;
for(thisLed = 0; thisLed < ledCount; thisLed++)
pinMode(ledPins[thisLed],OUTPUT);
  
}

void loop() {
int sensorReading = analogRead(analogPin);
int ledLevel = map(sensorReading,0,1023,0,ledCount);
for(int thisLed = 0; thisLed < ledCount; thisLed++){

if(thisLed < ledLevel)
digitalWrite(ledPins[thisLed],HIGH);
else
digitalWrite(ledPins[thisLed],LOW);
  
}
  
}

Add a comment
Know the answer?
Add Answer to:
Correct the errors in the code 5. Given the board setup in the figure, read the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT