programming with arduino
TRANSCRIPT
Programming of Arduino
Arduino programs can be divided in three main parts:
STRUCTURE VALUES -VARIABLES -CONSTANTS FUNCTIONS
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.
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.
Exampleint buttonPin = 3;void setup(){ Serial.begin(9600); pinMode(buttonPin, INPUT);}void loop(){ // ...}
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.
Examplevoid loop(){
digitalWrite( pin ,HIGH ); //turns ‘pin’ ondelay(1000); //pause for one
seconddigitalWrite( pin ,LOW ); //turns ‘pin’ offdelay(1000); //pause for one
second}
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.
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.
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}
Basic Functions Of ArduinoDigital I/O
pinMode()digitalWrite()digitalRead()
Analog I/OanalogReference()analogRead()analogWrite() - PWM
Timemillis()micros()delay()delayMicroseconds()
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.
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)
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.
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
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.
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”.