Friday, March 15, 2013

Gizduino lesson 1 blinking light

After the familiarization and the necessary preparations of the Gizduino, you can now write sketches to program your Gizduino. In the succeding entries I that I will post, I will discuss basic commands to program your Gizduino. For references, I will use sample sketches pre-set in the Arduino IDE and sketches that I have written.

In this lesson, I will use the blink sketch sample.



You will need the following:













Schematic diagram:










Before plugging in the Gizduino to the computer, make sure all connections are correct. Use the breadboard and jumper wires to make the connections easier. Connect the positive end of the LED (longer leg) to any digital pin, I used digital pin 13. Then, connect the negative end of the LED (shorter leg) to the resistor. Then connect the resistor to ground (GND). The resistor is used to prevent damage on the LED in case of wrong placements of components. The resistor is not polarized, so you can connect it any direction unlike the LED.


Sketch, Compile, then Upload:

From the Arduino IDE, open then upload the blink sketch sample.

File > Examples > Digital > Blink

or copy and paste the code below to your Arduino IDE.

/*
  Blink
 
 Turns on an LED on for one second, then off for one second, repeatedly.
 
 The circuit:
 * LED connected from digital pin 13 to ground.
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 Created 1 June 2005
 By David Cuartielles
 
 http://arduino.cc/en/Tutorial/Blink
 
 based on an orginal by H. Barragan for the Wiring i/o board
 
 */

int ledPin = 13; // LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup() {
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
  digitalWrite(ledPin, HIGH); // set the LED on
  delay(1000); // wait for a second
  digitalWrite(ledPin, LOW); // set the LED off
  delay(1000); // wait for a second
}

When you successfully upload the code above, The LED will turn on and off every second.

Code by Code Discussion:

/*
  Blink
 
 Turns on an LED on for one second, then off for one second, repeatedly.
 
 The circuit:
 * LED connected from digital pin 13 to ground.
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 Created 1 June 2005
 By David Cuartielles
 
 http://arduino.cc/en/Tutorial/Blink
 
 based on an orginal by H. Barragan for the Wiring i/o board
 
 */

 This is a block comment. All texts between /* and */ are ignored by the Gizduino.


         // The setup() method runs once, when the sketch starts
 
This is a line comment. Texts after // in the line are ignored by the Gizduino. Comments are useful for reminding what a particular code does.


      int ledPin =  13;
 
This is an example of a variable function. A variable is place to store data. There are several types  of variables available, the int or integer type of variable is one of the most commonly used. A integer  is a number between -32768 and 32767. “ledPin” is the name assigned to the variable. Any group of  characters can be used to name a variable, but has to be started with a letter. Variable names cannot be keywords used in the Arduino language; such as, while, HIGH, INPUT, else, etc. “13″ is the number assigned to the variable, the number can be any number between -32768 and 32767.


        void setup()
 
For a Gizduino program to function properly, it requires two structure commands, one of which is the void setup(). The void setup runs only once every time you turn on the Gizduino, then proceeds to the next structure command. The void setup contains the codes for the preparation of the program, such as setting pin modes.


        pinMode(ledPin, OUTPUT);
 
When you are going to use a digital pin, you will need to notify the Gizduino of what type of pin it is going to be. pinMode is the code, followed by the settings enclosed in the parentheses. The first part in the settings area for the pinMode is the digital pin used. In this case, ledPin was used as the pin number. Recall that ledPin was used as a variable, which is equal to 13. The code can also be read aspinMode(13, OUTPUT);. The next part of the code sets the pin as INPUT or OUTPUT, in this case it is an OUTPUT.


      void loop()
 
The main part of the program is the void loop(). It is in a continous loop, performing every command step-by-step.
  

        digitalWrite(ledPin, HIGH);
 
The digitalWrite function controls the passage of voltage to a certain digital pin. The settings for the function is written inside the parentheses. The first setting is the pin number, in this case ledPin is the pin number. The second setting is the state of the pin, set either HIGH or LOW. When the pin is set as HIGH, 5V of energy is allowed to go to the digital pin; in effect, turning the pin on. When the pin is set to LOW, no voltage is allowed to the digital pin, digital pin is off.


      delay(1000);
 
The delay function pauses the program for a certain amount of time before going to the step of the program. The amount of time entered to the function is in milliseconds; so, delay(1000); means pause the program for 1 second.


            ;
 
Notice the semi-colon after every code. The semi-colon is used to terminate a struction, this is to separate one instruction to the other.



      {}
 
Curly braces are use to mark blocks of code.
 
 
 
 
You can experiment by adding more LEDs and changing the timing of the response of the LED. 


 

 

   

No comments:

Post a Comment