User Tools

Site Tools


arduino:arduino_crash_course:program_structure

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
arduino:arduino_crash_course:program_structure [2012/09/14 01:16] – created mithatarduino:arduino_crash_course:program_structure [2017/12/06 01:01] (current) mithat
Line 3: Line 3:
 ===== Main loop ===== ===== Main loop =====
  
-All Arduino programs (called "sketches") have the same basic structure: ''setup'' function and a ''loop'' functionWhatever you write in the body of the ''setup'' function will happen only once--when the program starts (e.g. after you power up the Arduino, press the reset button, or load new program). Whatever you write in the body of the ''loop'' function will happen over and over as fast as possible until you halt the program (e.g., by removing the power, pushing the reset button, or loading a new program).+Arduino programs are called **sketches**. The source code for sketch is stored in a text file that has a ''ino'' file extension. 
 + 
 +Arduino sketches all have the same basic structure, consisting of ''setup'' function and a ''loop'' function.
  
 <file c program_structure.ino> <file c program_structure.ino>
Line 11: Line 13:
  
 void loop() { void loop() {
-  // Stuff in here gets run over and over and over again (until you turn the power off).+  // Stuff in here gets run over and over and over again  
 +  // (until you turn the power off).
 } }
 </file> </file>
 +Whatever you write in the body of the ''setup'' function will happen only once---when the program starts (e.g. after you power up the Arduino, press the reset button, or load a new program). Whatever you write in the body of the ''loop'' function will happen over and over as fast as the Arduino can manage until you halt the program (e.g., by removing the power, pushing the reset button, or loading a new program).
  
 ===== Blink an LED ===== ===== Blink an LED =====
  
-Blinking an LED is the microcontroller equivalent to a "[[https://en.wikipedia.org/wiki/Hello_world|Hello world]]" program in general programming.+Blinking an LED is the microcontroller equivalent to a "[[wp>Hello_world|Hello world]]" program in general programming. Here is how to blink an LED in Arduino:
  
 <file c BlinkMe.ino> <file c BlinkMe.ino>
Line 26: Line 30:
  
 void setup() {                 void setup() {                
-  // make pin 13 an output +  pinMode(13, OUTPUT);       // make pin 13 an output
-  pinMode(13, OUTPUT);     +
 } }
  
Line 50: Line 53:
  
 void setup() {                 void setup() {                
-  // make ledPin an output +  pinMode(ledPin, OUTPUT);      // make ledPin an output 
-  pinMode(ledPin, OUTPUT);     +
 } }
  
Line 62: Line 64:
 </file> </file>
  
-Now if we need to change the pin numberwe only need to change it in one place.+One of the advantages of using a named value for the pin number is that if we want to change the pin number we only need to change it in one place.
  
 Here we use variables also to define delay times: Here we use variables also to define delay times:
Line 74: Line 76:
 // use variable ledPin to store the pin number that drives the LED // use variable ledPin to store the pin number that drives the LED
 int ledPin = 13; int ledPin = 13;
 +
 // define the LED's on and off times in milliseconds // define the LED's on and off times in milliseconds
 int onTime = 1000; int onTime = 1000;
Line 79: Line 82:
  
 void setup() {                 void setup() {                
-  // make ledPin an output +  pinMode(ledPin, OUTPUT);      // make ledPin an output
-  pinMode(ledPin, OUTPUT);     +
 } }
  
arduino/arduino_crash_course/program_structure.1347585417.txt.gz · Last modified: 2012/09/14 01:16 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki