
SunFounder uno-and-mega-kit
•
Serial.println()
: Takes the same forms as Serial.print(), but it is followed by a carriage return character
(ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’).
Map the values
outputValue
=
map
(inputValue,
0
,
1023
,
0
,
255
);
//Convert from 0-1023 proportional to
˓
→
the number of a number of from 0 to 255
map(value, fromLow, fromHigh, toLow, toHigh)
re-maps a number from one range to another. That
is, a
value
of
Fromm
would get mapped to one of
to Low
, and a value of
from High
to one of
thigh
, values in-between
to values in-between, etc.
As the range of
led Pin
(pin 9) is 0-255, we need to map 0-1023 with 0-255.
Display the output value in Serial Monitor in the same way. If you are not so clear about the
map()
functions, you can
observe the data in the Serial Monitor and analyze it.
Serial
.
(
"Output: "
);
//print "Output"
Serial
.
println
(outputValue);
//print outputValue
Write the value of the potentiometer to LED
analogWrite
(ledPin, outputValue);
//turn the LED on depending on the output value
Write the output value to
led Pin
and you will see that the luminance of LED changes with your spinning of the
potentiometer knob.
analog Write()
: Writes an analog value (PWM wave) to a pin. It has nothing to do with an analog pin, but is just for
PWM pins. You do not need to call the
incommode()
to set the pin as output before calling
analog Write()
.
6.8.7 Experiment Summary
This experiment can also be changed to others as you like. For example, use the potentiometer to control the time
interval for the LED blinking. It is to use the value read from the potentiometer for delaying, as shown below. Have a
try!
inputValue
=
analogRead
(analogPin);
digitalWrite
(ledPin,
HIGH
);
delay
(inputValue);
digitalWrite
(ledPin,
LOW
);
delay
(inputValue);
122
Chapter 6. For Mega2560 User
Summary of Contents for R3
Page 1: ...SunFounder uno and mega kit www sunfounder com Jan 19 2022 ...
Page 2: ......
Page 6: ...iv ...
Page 8: ...SunFounder uno and mega kit 2 CONTENTS ...
Page 9: ...CHAPTER ONE COMPONENTS LIST 3 ...
Page 10: ...SunFounder uno and mega kit 4 Chapter 1 Components List ...
Page 11: ...SunFounder uno and mega kit 5 ...
Page 12: ...SunFounder uno and mega kit 6 Chapter 1 Components List ...
Page 13: ...SunFounder uno and mega kit 7 ...
Page 14: ...SunFounder uno and mega kit 8 Chapter 1 Components List ...
Page 27: ...SunFounder uno and mega kit 2 7 Capacitor 2 7 Capacitor 21 ...
Page 62: ...SunFounder uno and mega kit 56 Chapter 2 Components Introduction ...
Page 64: ...SunFounder uno and mega kit 58 Chapter 3 Download the Code ...
Page 82: ...SunFounder uno and mega kit 76 Chapter 5 Add Libraries ...
Page 198: ...SunFounder uno and mega kit 6 21 2 Components 192 Chapter 6 For Mega2560 User ...
Page 218: ...SunFounder uno and mega kit 212 Chapter 7 For Uno User ...
Page 221: ...SunFounder uno and mega kit 7 1 Lesson 1 Blinking LED 215 ...
Page 231: ...SunFounder uno and mega kit 7 3 Lesson 3 Controlling LED by Button 225 ...
Page 233: ...SunFounder uno and mega kit 7 3 Lesson 3 Controlling LED by Button 227 ...
Page 237: ...SunFounder uno and mega kit 7 4 Lesson 4 Doorbell 231 ...
Page 253: ...SunFounder uno and mega kit indigo and purple 7 7 Lesson 7 RGB LED 247 ...
Page 260: ...SunFounder uno and mega kit 254 Chapter 7 For Uno User ...
Page 262: ...SunFounder uno and mega kit 256 Chapter 7 For Uno User ...
Page 327: ...SunFounder uno and mega kit 7 20 Lesson 20 Simple Creation Stopwatch 321 ...
Page 350: ...SunFounder uno and mega kit 344 Chapter 7 For Uno User ...
Page 378: ...SunFounder uno and mega kit Breadboard LED Resistor 372 Chapter 8 Play with Scratch ...
Page 385: ...SunFounder uno and mega kit 8 2 Projects 379 ...
Page 386: ...SunFounder uno and mega kit Breadboard Resistor RGB LED 380 Chapter 8 Play with Scratch ...
Page 393: ...SunFounder uno and mega kit Breadboard LCD1602 Potentiometer 8 2 Projects 387 ...
Page 402: ...SunFounder uno and mega kit Breadboard 396 Chapter 8 Play with Scratch ...
Page 408: ...SunFounder uno and mega kit Breadboard Button 402 Chapter 8 Play with Scratch ...
Page 414: ...SunFounder uno and mega kit Breadboard Tilt Switch 408 Chapter 8 Play with Scratch ...
Page 419: ...SunFounder uno and mega kit Breadboard Thermistor 8 2 Projects 413 ...
Page 425: ...SunFounder uno and mega kit Breadboard Photoresistor Resistor 8 2 Projects 419 ...
Page 436: ...SunFounder uno and mega kit Breadboard Servo 430 Chapter 8 Play with Scratch ...
Page 446: ...SunFounder uno and mega kit Breadboard DC Motor 440 Chapter 8 Play with Scratch ...
Page 461: ...SunFounder uno and mega kit Breadboard Button 8 2 Projects 455 ...
Page 481: ...SunFounder uno and mega kit Breadboard 8 2 Projects 475 ...
Page 493: ...SunFounder uno and mega kit Breadboard Button 8 2 Projects 487 ...
Page 504: ...SunFounder uno and mega kit Set the size to 50 498 Chapter 8 Play with Scratch ...
Page 522: ...SunFounder uno and mega kit 516 Chapter 8 Play with Scratch ...
Page 524: ...SunFounder uno and mega kit 518 Chapter 9 FAQ ...
Page 526: ...SunFounder uno and mega kit 520 Chapter 10 Thank You ...