Friday 27 September 2013

Getting started with my Arduino. Making a LED flash.

I recently bought myself an Arduino Uno board after hearing what can be done with one of these boards. Having no idea on how to program or the syntax of the Processing language.
I have got a copy of "Getting started with Arduino, 2nd Edition" by Massimo Banzi, ISBN: 978-1-449-309879, that makes learning the language easy explaining what code does what.

The 1st lesson in the book, Making a LED flash.

The 1st lesson is a simple introduction to using the hardware/software and a simple sketch (program) to make the LED flash. The hardware setup is very simple just an optional LED between pin 13 & ground is needed.
LED attached. Green light on, ready to go.
The sketch for the flashing LED is a introduction into the Processing language with a simple sketch that sets up an output and then runs a loop to turn the LED on and off as specified in the sketch.

The Sketch & my understanding of it

The first  sketch in the book was this simple sketch.

                    // Example 01 : Blinking LED
const int LED = 13;                    // LED connected to
                                                    // digital pin 13
void setup()
{
pinMode(LED, OUTPUT);        // sets the digital
                                                    // pin as output
}
void loop()
{
digitalWrite(LED, HIGH);         // turns the LED on
delay(1000);                             // waits for a second
digitalWrite(LED, LOW);        // turns the LED off
delay(1000);                            // waits for a second
}

How the sketch works.
 // - These are comments not read by the Arduino but are human readable notes, in this case used to describe the code.

const int LED = 13 - This means that LED is a number that can't be changed (constant integer), in this case LED is bound to digital pin 13.

void setup() - This tells the Arduino that the next block of code will be called setup()

{ } - Curly brackets are used to begin and end blocks of code

pinMode(LED,OUTPUT) - This sets the LED pin to be an output.

void loop() - This is for specifying the behaviour of the output, in this case it will repeat the code block until the Arduino is turned off. 

digitalWrite(LED, HIGH) - This turns on any pin that has been configured as an OUTPUT.

delay(1000) - This adds a delay of 1000ms before executing next command

digitalWrite(LED, LOW) - This turns off any pin that has been configured as an OUTPUT.


The sketch working.

I changed the on/off time to 50ms on/off as the 1 second flash rate was too slow. I just changed the delay command to delay(50) to speed it up.

The sketch in action.


Next Lesson: Using a push button switch to control the led.

No comments:

Post a Comment