ARDUINO
The Arduino Environment
@ Dr Javaid, M.H Fidai
2 / 25
Board Selection
@ Dr Javaid, M.H Fidai
3 / 25
Serial Port/Com Port
@ Dr Javaid, M.H Fidai
4 / 25
The Environment
SKETCH NAME
SAVE OPEN NEW UPLOAD VERIFY
SERIAL MONITOR
@ Dr Javaid, M.H Fidai
5 / 25
Setup() and Loop()
SETUP:
The setup function is a default function type of the arduino environment It is mandatory to include this function. It runs only once at the start of the program. It is used for initialization of the different features of the controller, e.g. serial, pins direction etc.
LOOP:
The loop function is also a mandatory function. It contains the body of your code. It repeats itself over and over. (Analogous to a while(1) in the body of main() )
6 / 25
@ Dr Javaid, M.H Fidai
Setup() and Loop()
@ Dr Javaid, M.H Fidai
7 / 25
PINS
@ Dr Javaid, M.H Fidai
8 / 25
PINS
@ Dr Javaid, M.H Fidai
9 / 25
PINS
@ Dr Javaid, M.H Fidai
10 / 25
PINS
@ Dr Javaid, M.H Fidai
11 / 25
PINS
@ Dr Javaid, M.H Fidai
12 / 25
PINS
@ Dr Javaid, M.H Fidai
13 / 25
Digital Pin Control in Arduino
pinMode (pinnumber, state);
pinnumber State State
: Number of the digital pin : Output or Input : High or Low
digitalwrite(pinnumber, state);
Int x = digitalRead(pinnumber);
Int x : user defined variable to store value of the pin state
@ Dr Javaid, M.H Fidai
14 / 25
Digital Pin Control in Arduino
@ Dr Javaid, M.H Fidai
15 / 25
Delay
Unsigned long millis();
Returns the number of milli seconds since the current program has been running Pauses the program for the amount of milliseconds mentioned. Pauses the program for the amount of microseconds mentioned.
delay(ms);
delaymicroseconds(us);
@ Dr Javaid, M.H Fidai
16 / 25
Delay
@ Dr Javaid, M.H Fidai
17 / 25
Analog Functions
Int x = analogread(pinanalog);
Pinanalog : Number assigned to the analog pins of the arduino This function uses the adc of the avr to convert the analog inputs. It returns a 10-bit(0 - 1023) integer value. This functions utilizes the pwm generation capability of the avr. Pinnumber is the number of the pin that can be used for pwm. Value ranges from 0 255. It is representation of the duty cycle of the output wave.
analogwrite(pinnumber, value);
@ Dr Javaid, M.H Fidai
18 / 25
Analog Functions
@ Dr Javaid, M.H Fidai
19 / 25
Analog Functions
@ Dr Javaid, M.H Fidai
20 / 25
External Interrupts
attachinterrupt(interrupt, function, mode);
Interrupt : The number of the interrupt, i-e, 0,1, 2 etc Function : user defined to be called when the interrupt occurs Mode : The state at which interrupt should occur, e.g. LOW, CHANGE, RISING, FALLING
detachinterrupt(interrupt); The interrupts in the arduinomega2560 are on the following pins, with the interrupt number in the brackets:
2 (interrupt 4), 3 (interrupt 5), 18 (interrupt 3), 19 (interrupt 2), 20 (interrupt 1), and 21(interrupt 0).
@ Dr Javaid, M.H Fidai
21 / 25
External Interrupts
int pin = 13; volatile int state = LOW; void setup() { pinMode(pin, OUTPUT); attachInterrupt(0, blink, CHANGE); } void loop() { digitalWrite(pin, state); } void blink() { state = !state; }
@ Dr Javaid, M.H Fidai
22 / 25
Serial
[Link](baudrate);
Initializes the serial hardware for communication at the baud rate mentioned
Transmits the data. [Link] adds a carriage return (\r\n) to the data. Reads the data received. Returns a value that indicates how many unread bytes are there.
[Link](data); and [Link](data);
Int [Link]();
Int [Link]();
[Link]();
Clears the buffer registers.
@ Dr Javaid, M.H Fidai
23 / 25
Serial
@ Dr Javaid, M.H Fidai
24 / 25