setup()
function.digitalRead()
statement to check whether the switch is currently open or closed, and add code statements that should be performed depending on the result.setup()
function:int
stands for integer (whole number). Photon pin numbers are always treated as int
values (even though they have letters).magSwitch
. You can change the variable name, but choose a name that will make sense to anyone reading the code.D0
. If necessary, modify this value to match the actual I/O pin number that your switch is connected to.switch()
is a reserved keyword in the Wiring programming language.setup()
function:pinMode()
method requires two parameters inside its parentheses (in this order):D0
, etc.) or a variable that stores a pin number. In this example, a variable named magSwitch
is listed. If necessary, change this to match the variable name for your magnetic switch.INPUT_PULLUP
for a magnetic switch.digitalRead()
method is used to check whether a magnetic switch is currently open or closed.loop()
function or a custom function:switchState
is declared that will have a data type of int
(integer). This variable is made equal to whatever value is returned by the digitalRead()
method. You can change the name of this variable, but it will make sense if it's similar to the variable name used for the switch pin number.digitalRead()
method requires one parameter insides its parentheses:D2
, etc.) or a variable that stores a pin number. In this example, the variable named magSwitch
is listed. If necessary, change this to match the variable name for your switch's pin number.digitalRead()
method will return a value of either HIGH
or LOW
(which are treated as if they were int
values):HIGH
indicates that the magnetic switch is currently open.LOW
indicates that the magnetic switch is currently closed.switchState
is equivalent to HIGH
:if
statement will be performed. You will need to add code statements within the curly braces that perform the actions you want when the magnetic switch is open.switchState
is LOW
), the code within the curly braces of the else
statement will be performed. You will need to add code statements within the curly braces that perform the actions you want when the magnetic switch is closed.HIGH
or LOW
) without including an else
statement to perform actions for the opposite condition.