Page 100 - Programming the Photon Getting Started With the Internet of Things
P. 100

}
            else {

                digitalWrite(led, LOW);
            }
            delay(100);
        }


             We can look at the code in the following sections to make it easier for us to understand
        what it is doing:


               Set the push button (pin D0) as an output and the LED (pin D7) as an input.

               Read the state of the push button and store it in a variable called state.

               If the push-button state is HIGH or connected to 3V3, turn the LED on or HIGH.
               If the push-button state is LOW, turn the LED off or LOW.

               Pause the program for 100 milliseconds to slow down the output.


             When you upload this program to the Photon and push the switch on your breadboard,

        you should see the on-board LED light up. When you release the push-button switch, the
        LED will switch off.





        digitalRead ()



        The main function that we can learn from our sketch is the use of digitalRead(). This
        checks the value of the pin, which is referenced in the parameters. In this example we
        refer to digital pin D0 and we call pushbutton to check if the push button is connected to
        3V3 or to ground. digitalRead() returns a value of either HIGH or LOW, and we store

        this value in a variable called state.

             You may also note that we used pinMode() to set the digital pin 0 as an input in the

        setup function. This is required so that the Photon knows how to treat that pin and allows
        us to use the function digitalRead() on that particular pin.

             The syntax for digitalRead() is


                   digitalRead(pin);


             The parameter of digitalRead is


                   pin, refers to the digital pin number
                   digitalRead() returns HIGH or LOW




        Local and Global Variables
   95   96   97   98   99   100   101   102   103   104   105