Basic arduino programming

Arduino IDE uses C++ language for programming the microcontroller. In this page we shall see basics of programming Arduino board.

it requires Arduino IDE downloadable from Arduino website.

Captureee

STRUCTURE

The basic structure of the Arduino programming language is fairly simple and runs in at least two parts. These two required parts, or functions, enclose blocks of statements.

void setup()

{

statements;  //execute one time

}

void loop()

{

statements;  //execute in a loop until the board is reset or power off

}


Constants

The Arduino language has a few predefined values, which are called constants. They are used to make the program easier to read.

TRUE/FALSE: these are Boolean constants that defines logic levels. FALSE is defined as 0(zero) while TRUE is often defined as 1, but also be anything else except zero.

if (b == TRUE)

{

statement;

}

HIGH/LOW: These constants define pin levels as HIGH or LOW and are used when reading or writing to digital pins. HIGH is defined as logic level 1, ON or 5V while LOW is logic level 0, OFF or 0V.

digitalWrite (13, HIGH); // Makes the pin 13 high of 5v out.

INPUT/OUTPUT: These are used with the pinMode () functions to define the mode of a digital pin as either input or output

pinMode ( 13, OUTPUT);


FUNCTIONS

pinMode ()

These functions are commonly used in void setup() to configure a specified pin to behave as an INPUT or an OUTPUT.

pinMode (pin, OUTPUT);  // sets ‘pin’ to output

Arduino digital pins are default sets to input, so they don’t need to be explicitly declared as inputs with pinMode(). Pins  configured as INPUT are said to be in a high impedance state. There are alse convenient 20kohm pullup resistors built into Atmega chip that can be accessed from software. These built-in resistor are accessed in the following manner.

pinMode (pin, INPUT);  // Sets ‘pin’ to input

digitalWrite (pin, HIGH);  // turn on pullup resistor

Pullup resistors would normally be used for connecting inputs like switches. Notice in the above example it does not convert pin to an output, it is merely a method for activating for activating the internal pull-ups.

digitalRead(pin)

Reads the value from a specified digital pin with the result either HIGH of LOW. the pin can be specified as either a variable of constant (0 – 13 & A0 – A5).

value = digitalRead ( 11); // read the pin and sets ‘value’ either 0 or 1.

digitalWrite(pin, value)

Output either logic level high or low  (turns on or off).

digitalWrite (13, HIGH) // make pin 13 to high (5v).

sample code:

//The following example reads da pushbutton connected to a digital input and turns //on an LED connected to a digital output when has been pressed.

int led = 13;  // LED is connected to pin 13

int pin = 7;   // Push button is connected to the pin 7

int value =0; // Variable to store the read value

void setup() // sets the pin mode

{

pinMode ( led, OUTPUT);  // sets pin 13 as output

pinMode ( pin, INPUT);     // sets pin 7 as input

}

void loop()

{

value = digitalRead ( pin); // sets ‘value’ equal to the input pin (0 or 1)

digitalWrite ( led, value);  // sets ‘led’ to the button value

}

analogRead(pin)

Reads the value from a specified analog pin with a 10-bit resolution. this function only works on the analog  pins (A0-A5). The resulting integer values ranges from 0 to 1023.

value = analogRead ( A1); // sets the ‘value’ equal to ‘pin’

if value is 0 then it is 0 volt & if value is 1023 then pin is at 5 volt.

NOTE: Analog pins unlike digital ones, do not need to be first declared as INPUT or OUTPUT.

analogWrite (pin, value)

Writes a pseudo-analog value using the hardware enabled pulse width modulation (PWM) to an output pin marked PWM. on newer Arduinos with ATmega 168 chip, this function works on pins 3,5,6,9,10 and 11. the values can be specified as a variable or constant with a value 0-255.

analogWrite (pin, value);

A value of 0 generates a steady 0 volt output at specified pin. A value of 255 generate a steady 5 volt output at the specified pin. For values in between o and 255, the pin rapidly alternates between 0 and 5 volts

arduino-pwm-wave-signal

delay (ms)

Pause your program for the amount of times as specified in millisecoonds, Where 1000 equals to 1 second.

delay ( 2000); wait for two seconds

millis()

Returns the number of milliseconds since the Arduino board running the current program as an unsigned long value.

value = millis();

NOTE: This number will over flow  (reset back to zero), after approximately 9 hours.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s