Let us take a closer look at the Arduino's IDE:
The bare minimum that a program needs to avoid a compiler error are the setup() and loop() functions. Even if you aren't using them, they still need to be placed in the sketch for structure's sake.
Found by going to File>Examples>Basics>BareMinimum inside of the Arduino IDE
The setup() function is used to initialize pins, registers, variables, and peripherals. It is the first function that is called when the Arduino resets or is powered up, and is only run once. Oddly enough, in all of the example code that I've seen no one actually initializes variables inside of the setup() function even though the Arduino reference suggests it. Most variables are normally declared at the beginning of the program to assure that they are global.
The loop() function replaces the main() function that one would normally see when programming in C. More specifically, it would replace the while loop inside of the main function. As you probably can infer from the name, the loop() function continuously loops until you either place the Arduino into sleep mode or shut it off.
You can still create other functions that would be called from inside of loop(), just place them outside of the loop function as you would when programming in C.
If your program is error free, you will see a message at the bottom of the IDE letting you know when the compiler is done.
Now you can program your Arduino, assuming you have the USB cable (or a printer to steal it from) by clicking on the upload button.
The best way to learn how to use a new program is to try it out for yourself, so up next... Blinking an LED
No comments:
Post a Comment