SunFounder uno-and-mega-kit
(continued from previous page)
int
sensorValue
=
0
;
// value read from the sensor
int
ledLevel
=
0
;
// sensor value converted into LED 'bars'
The 8 LEDs are connected to pin5-pin12, in this code, use a array to store the pins, ledPins[0] is equal to 5, ledPins[1]
to 6 and so on.
Set 8 pins to OUTPUT
for
(
int
led
=
0
; led
<
NbrLEDs; led
++
)
{
pinMode
(ledPins[led],
OUTPUT
);
// make all the LED pins outputs
}
Using the for() statement set the 8 pins to OUTPUT. The variable led is added from 0 to 8, and the pinMode() function
sets pin5 to pin12 to OUTPUT in turn.
Read the analog value of the photoresistor
sensorValue
=
analogRead
(photocellPin);
// read the value of A0
Read the analog value of the
photocellPin(A0
) and store to the variable
sensorValue.
analogRead():
Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog
to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into
integer values between 0 and 1023.
Serial
.
(
"SensorValue: "
);
Serial
.
println
(sensorValue);
// Print the analog value of the photoresistor
Use the Serial.print()function to print the analog value of the photoresistor. You can see them on the Serial Monitor.
Serial.print():
Prints data to the serial port as human-readable ASCII text. This command can take many forms.
Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting
to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is.
Serial.println():
Thiscommand 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 analog value to 8 LEDs
ledLevel
=
map
(sensorValue,
0
,
1023
,
0
, NbrLEDs);
// map to the number of LEDs
Serial
.
(
"ledLevel: "
);
Serial
.
println
(ledLevel);
The map() command is used to map 0-1023 to 0-NbrLEDs(8), (1023-0)/(8-0)=127.875
0-12
7.875
128-2
55.75
2
56-38
3.625
384-
511.5
5
12-63
9.375
640-7
67.25
7
68-89
5.125
896
-1023
0
1
2
3
4
5
6
7
If sensorValue is 560, then the ledLevel is 4.
7.9. Lesson 9 Photo resistor
263
Содержание R3
Страница 1: ...SunFounder uno and mega kit www sunfounder com Jan 19 2022 ...
Страница 2: ......
Страница 6: ...iv ...
Страница 8: ...SunFounder uno and mega kit 2 CONTENTS ...
Страница 9: ...CHAPTER ONE COMPONENTS LIST 3 ...
Страница 10: ...SunFounder uno and mega kit 4 Chapter 1 Components List ...
Страница 11: ...SunFounder uno and mega kit 5 ...
Страница 12: ...SunFounder uno and mega kit 6 Chapter 1 Components List ...
Страница 13: ...SunFounder uno and mega kit 7 ...
Страница 14: ...SunFounder uno and mega kit 8 Chapter 1 Components List ...
Страница 27: ...SunFounder uno and mega kit 2 7 Capacitor 2 7 Capacitor 21 ...
Страница 62: ...SunFounder uno and mega kit 56 Chapter 2 Components Introduction ...
Страница 64: ...SunFounder uno and mega kit 58 Chapter 3 Download the Code ...
Страница 82: ...SunFounder uno and mega kit 76 Chapter 5 Add Libraries ...
Страница 135: ...SunFounder uno and mega kit 6 10 3 Schematic Diagram The schematic diagram 6 10 Lesson 10 Servo 129 ...
Страница 163: ...SunFounder uno and mega kit 6 15 3 Schematic Diagram The schematic diagram 6 15 Lesson 15 Humiture Sensor 157 ...
Страница 198: ...SunFounder uno and mega kit 6 21 2 Components 192 Chapter 6 For Mega2560 User ...
Страница 200: ...SunFounder uno and mega kit 6 21 4 Experimental Procedures Step 1 Build the circuit 194 Chapter 6 For Mega2560 User ...
Страница 218: ...SunFounder uno and mega kit 212 Chapter 7 For Uno User ...
Страница 221: ...SunFounder uno and mega kit 7 1 Lesson 1 Blinking LED 215 ...
Страница 231: ...SunFounder uno and mega kit 7 3 Lesson 3 Controlling LED by Button 225 ...
Страница 233: ...SunFounder uno and mega kit 7 3 Lesson 3 Controlling LED by Button 227 ...
Страница 237: ...SunFounder uno and mega kit 7 4 Lesson 4 Doorbell 231 ...
Страница 253: ...SunFounder uno and mega kit indigo and purple 7 7 Lesson 7 RGB LED 247 ...
Страница 260: ...SunFounder uno and mega kit 254 Chapter 7 For Uno User ...
Страница 262: ...SunFounder uno and mega kit 256 Chapter 7 For Uno User ...
Страница 327: ...SunFounder uno and mega kit 7 20 Lesson 20 Simple Creation Stopwatch 321 ...
Страница 350: ...SunFounder uno and mega kit 344 Chapter 7 For Uno User ...
Страница 370: ...SunFounder uno and mega kit Finally click the Upload Code button 364 Chapter 8 Play with Scratch ...
Страница 378: ...SunFounder uno and mega kit Breadboard LED Resistor 372 Chapter 8 Play with Scratch ...
Страница 385: ...SunFounder uno and mega kit 8 2 Projects 379 ...
Страница 386: ...SunFounder uno and mega kit Breadboard Resistor RGB LED 380 Chapter 8 Play with Scratch ...
Страница 390: ...SunFounder uno and mega kit 4 Ball3 sprite red 5 Ball4 sprite green 384 Chapter 8 Play with Scratch ...
Страница 393: ...SunFounder uno and mega kit Breadboard LCD1602 Potentiometer 8 2 Projects 387 ...
Страница 402: ...SunFounder uno and mega kit Breadboard 396 Chapter 8 Play with Scratch ...
Страница 408: ...SunFounder uno and mega kit Breadboard Button 402 Chapter 8 Play with Scratch ...
Страница 414: ...SunFounder uno and mega kit Breadboard Tilt Switch 408 Chapter 8 Play with Scratch ...
Страница 419: ...SunFounder uno and mega kit Breadboard Thermistor 8 2 Projects 413 ...
Страница 425: ...SunFounder uno and mega kit Breadboard Photoresistor Resistor 8 2 Projects 419 ...
Страница 436: ...SunFounder uno and mega kit Breadboard Servo 430 Chapter 8 Play with Scratch ...
Страница 446: ...SunFounder uno and mega kit Breadboard DC Motor 440 Chapter 8 Play with Scratch ...
Страница 448: ...SunFounder uno and mega kit Adjust the size and position of the sprite appropriately 442 Chapter 8 Play with Scratch ...
Страница 461: ...SunFounder uno and mega kit Breadboard Button 8 2 Projects 455 ...
Страница 481: ...SunFounder uno and mega kit Breadboard 8 2 Projects 475 ...
Страница 493: ...SunFounder uno and mega kit Breadboard Button 8 2 Projects 487 ...
Страница 504: ...SunFounder uno and mega kit Set the size to 50 498 Chapter 8 Play with Scratch ...
Страница 522: ...SunFounder uno and mega kit 516 Chapter 8 Play with Scratch ...
Страница 524: ...SunFounder uno and mega kit 518 Chapter 9 FAQ ...
Страница 526: ...SunFounder uno and mega kit 520 Chapter 10 Thank You ...