A street lamp uses a light sensor to detect when to turn
the lights on at night.
Circuit 2
Arduino Code:
6
Troubleshooting:
LED Remains Dark
This is a mistake we continue to make time and time
again, if only they could make an LED that worked both
ways. Pull it up and give it a twist.
It Isn't Responding to Changes in Light
Given that the spacing of the wires on the photo-resistor
is not standard, it is easy to misplace it. Double check it’s
in the right place.
Still Not Quite Working
You may be in a room which is either too bright or dark.
Try turning the lights on or off to see if this helps. Or if
you have a flashlight near by give that a try.
You should see the LED grow brighter
or dimmer in accordance with how
much light your photoresistor is
reading. If it isn't working, make sure
you have assembled the circuit
correctly and verified and uploaded
the code to your board or see the
troubleshooting tips below.
Code to Note:
Real World Application:
Open Arduino IDE
//
File > Examples > SIK Guide >
Circuit # 6
What You Should See:
When we read an analog signal using analogRead(), it will be
a number from 0 to 1023. But when we want to drive a
PWM pin using analogWrite(), it wants a number from 0 to
255. We can "squeeze" the larger range into the smaller range
using the map() function.
lightLevel = map(lightLevel, 0, 1023, 0, 255);
Because map() could still return numbers outside the "to"
range, we'll also use a function called constrain() that will
"clip" numbers into a range. If the number is outside the
range, it will make it the largest or smallest number. If it is
within the range, it will stay the same.
lightLevel = constrain(lightLevel, 0, 255);
IOREF
RES
ET
RESET
7-15V
SCL
SDA
AREF
GND
13
12
~11
~10
~9
8
7
~6
~5
4
~3
2
1
0
TX
RX
13
3.3V
5V
GND
GND
VIN
A0
A1
A2
A3
A4
A5
POWER
ANALOG IN
DIGIT
AL (PWM~)
ON
ISP
TX
RX
IOREF
RES
ET
RESET
7-15V
SCL
SDA
AREF
GND
13
12
~11
~10
~9
8
7
~6
~5
4
~3
2
1
0
TX
RX
13
3.3V
5V
GND
GND
VIN
A0
A1
A2
A3
A4
A5
POWER
ANALOG IN
DIGIT
AL (PWM~)
ON
ISP
TX
RX
LEARN. SHARE. HACK.
See
http://arduino.cc/en/reference/map
for more info.
See
http://arduino.cc/en/reference/constrain
for more info.
Parameters
map(value, fromLow, fromHigh, toLow, toHigh)
Parameters
constrain(x, a, b)
x:
the number to constrain, all data types
a:
the lower end of the range, all data types
b:
the upper end of the range, all data types
value:
the number to map
fromLow:
the lower bound of the value's current range
fromHigh:
the upper bound of the value's current range
toLow:
the lower bound of the value's target range
toHigh:
the upper bound of the value's target range
Page 44
Содержание RedBoard
Страница 13: ...Page 11...