programming with arduino

19
Programming of Arduino

Upload: makers-of-india

Post on 12-Apr-2017

47 views

Category:

Education


4 download

TRANSCRIPT

Page 1: Programming with arduino

Programming of Arduino

Page 2: Programming with arduino

Arduino programs can be divided in three main parts:

STRUCTURE VALUES -VARIABLES -CONSTANTS FUNCTIONS

Page 3: Programming with arduino

STRUCTURE The basic structure of the arduino programming language

is fairly simple and runs in atleast two parts. These two required parts,or functions, enclose blocks of

statement.

Page 4: Programming with arduino

Setup() Setup() function is called when a sketch starts Use to initialize variables , pin modes , start

using libraries etc… It will only run once,after each powerup or reset. So generally it is used to prepare your ardruino serial

communication and pinmode(). It must be included in program even if no lines are

there.

Page 5: Programming with arduino

Exampleint buttonPin = 3;void setup(){ Serial.begin(9600); pinMode(buttonPin, INPUT);}void loop(){ // ...}

Page 6: Programming with arduino

Loop() After calling the setup() function, the loop() function

does precisely what its name suggests, loops consecutively

i.e it allows us to execute a statement or group of statements multiple times.

Allows program to change, respond and control the Arduino board.

Page 7: Programming with arduino

Examplevoid loop(){

digitalWrite( pin ,HIGH ); //turns ‘pin’ ondelay(1000); //pause for one

seconddigitalWrite( pin ,LOW ); //turns ‘pin’ offdelay(1000); //pause for one

second}

Page 8: Programming with arduino

Variables A variable is a way of naming and storing a numerical value for later use. It can be continuously changed as opposed to constants whose value

never changes. Data types says the size of variable. Eg: int a,b; The above statement declares a variable ‘a’ whose size can be of 16 bits. Initializations can be done while declaring or in further part of program. Eg: a=20; b=10 //initializing variable

a=b; //changing variable value You can use same variable name in same program but it depends on scope of variable.

Page 9: Programming with arduino

Functions A function is a block of code that has a name and a block of

statements that are executed when the function is called. Custom functions can be written to perform repetitive tasks

and to reduce clutter in program. Functions are declared by first declaring the function type. Function type tells the type of value to be written by the

function. Eg: int a() //for integer type to be returned If no values is to be returned the function type would be void. After name of function name we pass can parameters of

function in parenthesis.

Page 10: Programming with arduino

Exampleint delayVal(){

int v; //create temporary variable ‘v’

v = analogRead(pot); //read potentiometer valuev /= 4; //converts

0-1024 to 0-255return v; //return final

value}

Page 11: Programming with arduino

Basic Functions Of ArduinoDigital I/O

pinMode()digitalWrite()digitalRead()

Analog I/OanalogReference()analogRead()analogWrite() - PWM

Timemillis()micros()delay()delayMicroseconds()

Page 12: Programming with arduino

LED BLINK

Description: In this lesson we will,

1. Build and wire a device with one LED, resistor, and Arduino board.

2. Program the device to “Blink the Light” 3. Identify the flow of electricity / signal in this

computing unit.4. Use the Blockly Programming system to control

speed of Blink.

Page 13: Programming with arduino

You will need:

1. LED Light Bulb (Any Colour) 2. 330 Ohm Resistor 3. Red Wire 4. White Wire 5. Black (Or dark colored Wire)

Page 14: Programming with arduino

Process:

1. Run a red Wire from the 5V Pin to the Red Rail on the Breadboard. This will connect the current side of the circuit.

2. Run a black (or dark coloured wire) from the Gnd Pin of the Arduino to the Blue Rail on the far side of the Breadboard.

Page 15: Programming with arduino
Page 16: Programming with arduino

3. Plug an LED bulb into the circuit. Note that the longer pin will face the Arduino Board and the pins should cross the “gap” in the breadboard.

4. This step is VERY IMPORTANT!!! Plug a 330 OHM resistor from Port B5 to Ground (The blue rail).

An LED bulb must ALWAYS have a Resistor in the circuit. If we do not use a Resistor, we will burn out the bulb or the Arduino

Page 17: Programming with arduino

5. Now we will run the signal wire. Wire Pin 13 on the Arduino to the Breadboard. This will carry the current from the Arduino Pin to the LED and allow the Arduino to switch on and off the LED.

Page 18: Programming with arduino

Sketch Programming:

1. Start your Arduino Sketch Program by clicking on the Sketch Icon.

2. Save the code by selecting “File-Save” and naming the program “lastnameBlink”.

Page 19: Programming with arduino