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

When  verifying  the  firmware  the  compiler  will  tell  you  that  it  has  successfully
        compiled your code and that everything was acceptable to the C language standard. At the
        bottom of the compile if you click the little icon for information, it will also tell you how

        much of the flash memory has been used.

             The Photon has a total of 1MB internal flash memory, which is divided into three main
        areas.  Beginning  at  the  top  of  the  memory  space  is  where  the  bootloader  is  saved  and
        locked. The second region is reserved for storing system flags, and the third region holds

        the actual user firmware.

             Let’s take a closer look at our setup and loop functions that will always be the starting

        point of every firmware that we write. We can start by using the word void before setup
        and loop followed by a pair of curly braces.

             The line void setup () means that we are defining a function called setup within our

        code. Some functions are already defined for us, such as digitalWrite and delay. Setup
        and loop are two functions that we must define for ourselves in every program we write.

             We are not calling setup or loop like we do with digitalWrite or delay, but we are

        actually creating these functions so that the Photon itself can call them. This might sound
        a little bit confusing, but the best way to think of it is that we are trying to shorten our
        code. Using void with both setup and loop allows us to not return any values within the
        function, unlike other functions, so we have to say that these are void.


             After  the  word  void  comes  the  function’s  name  and  parentheses  containing  any
        arguments. In our case both setup and loop do not contain any arguments, but we still
        have to include the parentheses. Because we are defining a function within our code and

        not calling a function, we do not have to close it with a semicolon. Instead, we use the
        curly braces, which is where our code sits between in the function—this is known as a
        block  of  code.  Just  because  we  define  the  functions  setup  and  loop  that  does  not

        necessarily mean we have to use those functions to hold any block of code—we simply
        just need to define them in every firmware we write, although in reality this may not ever
        happen.

             Let’s go back to our example sketch in Chapter 2:


        //D7 LED Flash Example
        int LED = D7;



        void setup() {
            pinMode(LED, OUTPUT);
        }



        void loop() {
            digitalWrite(LED, HIGH);
   51   52   53   54   55   56   57   58   59   60   61