Data Sheet

29 : circuit 1c
CODE TO NOTE
IF ELSE STATEMENTS:
if(logic statement){
//run if true
}
else{
//run if false
}
The if else statement lets your code react to the world by
running one set of code when the logic statement in the
round brackets is true and another set of code when the
logic statement is false. For example, this sketch uses an
if statement to turn the LED on when it is dark, and else
statement to turn the LED off when it is light.
LOGICAL OPERATORS:
(photoResistor <
threshold)
Programmers use logic statements to translate things
that happen in the real world into code. Logic statements
use logical operators like ‘equal to’ ==, ‘greater than’
>
and ‘less than’ <, to make comparisons. When the
comparison is true (e.g., 4 < 5), then the logic statement
is true. When the comparison is false (e.g., 5 < 4) then the
logic statement is false. This example is asking whether
the variable photoresistor is less than the variable
threshold.
CODING CHALLENGE
RESPONSE PATTERN: Right now your if statement turns the LED on when it
gets dark, but you can also use the light sensor like a no-touch button. Try using
digitalWrite()
and
delay()
to make the LED blink a pattern when the light level
drops, then calibrate the threshold variable in the code so that the blink pattern triggers
when you wave your hand over the sensor.
REPLACE 10KΩ RESISTOR WITH AN LED: Alter the circuit by replacing the
10KΩ resistor with an LED (the negative leg should connect to GND). Now what happens
when you place your finger over the photoresistor? This is a great way to see Ohm’s
law in action by visualizing the effect of the change in resistance on the current flowing
through the LED.