+ All Categories
Home > Documents > 3/2/2014 The Arduino Briefing - National University of...

3/2/2014 The Arduino Briefing - National University of...

Date post: 25-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
10
3/2/2014 1 C.S. Yee 1 The Arduino Briefing Mr. Yee Choon Seng Email : [email protected] Design Project resources – http://guppy.mpe.nus.edu.sg/me3design.html One-Stop robotics shop A-Main Objectives Pte Ltd, Block 1 Rochor Road, #02-608, Rochor Centre, S180001, Tel: 63960745 • http://www.amainobj.com – http://shop.rotor.com.sg/ – http://www.singaporehobby.com/ – http://www.sgbotic.com/ – http://www.roboworld.com.sg/ – http://www.robot-r-us.com/ C.S. Yee 2 The Arduino Briefing Layout of ports & connectors on Arduino Leonardo
Transcript
Page 1: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

1

C.S. Yee 1

The Arduino Briefing

• Mr. Yee Choon Seng• Email : [email protected]• Design Project resources

– http://guppy.mpe.nus.edu.sg/me3design.html• One-Stop robotics shop

– A-Main Objectives Pte Ltd, Block 1 Rochor Road, #02-608, Rochor Centre, S180001, Tel: 63960745

• http://www.amainobj.com

– http://shop.rotor.com.sg/– http://www.singaporehobby.com/– http://www.sgbotic.com/– http://www.roboworld.com.sg/– http://www.robot-r-us.com/

C.S. Yee 2

The Arduino Briefing

• Layout of ports & connectors on Arduino Leonardo

Page 2: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

2

C.S. Yee 3

The Arduino Briefing

Arduino Leonardo Features• Microcontroller: ATmega32u4• Operating Voltage: 5V• Input Voltage: (recommended) 7-12V (limits) 6-20V • Digital I/O Pins 20• PWM Channels 7• Analog Input Channels 12• DC Current per I/O Pin 40 mA (for 3.3V Pin 50 mA)• Flash Memory: 32 KB (ATmega32u4, 4 KB bootloader) • SRAM 2.5 KB, EEPROM 1 KB (ATmega32u4) • Clock Speed: 16 MHz

C.S. Yee 4

The Arduino Briefing

• Arduino Leonardo - I/O configurationDigital I/O

– There are 20 INPUT/OUTPUT pins altogether– 14 are default digital (0 to 13)– 6 are designated analog INPUT pins (A0 to A5)– Pin function can be configured individually using pinMode

• pinMode(pin#, IO);– Where pin# can be from 0 to 13, I for INPUT, O for OUTPUT.

RC Servo Motor pins– 6 pins designated for PWM output, namely 3,5,6 9, 10 & 13– All 6 pins are marked with ~ at digital I/O segment.– Can be controlled using analogWrite(pin#, value);– Value must be between 0 and 255.– Can also use Servo Library, through

• #include <Servo.h>

Page 3: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

3

C.S. Yee 5

The Arduino Briefing

• Arduino Leonardo - I/O configurationAnalog INPUT pins

– Default onboard pins are A0 to A5 (6 pins)– Additional pins on digital pins 4, 6, 8, 9, 10 & 12, named A6 to A11.– Reads 0 to 5V in 10 bits, giving value between 0 to 1023 in integer form.

• Int val = analogRead(pin#);Onboard LED Display

– Already connected to digital output pin 13.– Output HIGH to light up LED, LOW to turn off LED.

C.S. Yee 6

The Arduino Briefing• Programming using Arduino (or Wiring)

setup() function

void setup( ) // Declaring a void function{

// Initialisation instructions, runs once only when// starting program from RESET or power-up.

}

loop() function

void loop( ) // Declaring a void function{

// Program instructions, runs over and over again// until power is turned off.

}

Page 4: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

4

C.S. Yee 7

The Arduino Briefing

• Declaring functions

int square (int n){

return (n * n); // return the value of n * n to the calling point.

}

void music(){

// routine to play some sound in sequence.// No need to return any value or token.

}

C.S. Yee 8

The Arduino Briefing• Data Objects

– int : 16-bit integer number, -32,768 to +32,767– char: 8-bit character or value, 0 to 255, eg ‘x’– Decimal number : 1234– Hexadecimal : 0x1F (which is decimal 31) – Binary : 0b00011111 (decimal 31)

• Expansion of data objects:– Arrays, homogeneous list of any type– Structure (struct), non-homogeneous group of related data.

• Operators (Integers only)– Arithmetic +, -, *, / – Comparison >, <, ==, >=, <=– Bitwise arithmetic | (OR), & (AND), ^ (XOR), ~ (NOT)– Boolean Arithmetic || (OR), && (AND), ! (NOT)

Page 5: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

5

C.S. Yee 9

The Arduino Briefing

• Operators for Long Integers– Limited to +, -, *, >, <, ==, >= and <=

• Operators for float– Maths & compare +, -, *, /, >, <, ==, >= and <=– Some trigonometric, logarithmic and exponential functions (try not to

use them.) • Expressions

Simple maths expression:a = a + 3;

is the same as:a += 3;

a++; is the same as a += 1;

How about ++a ?

C.S. Yee 10

The Arduino Briefing

• Control Flow 1 : if – else Syntax:

if (expression or condition)<statement or list of statements in { }>;

else<alternative statement(s) in { }>;

Example 1:if (i < 10)

j = j + i; else

PORTA = j;Example 2:

if (i < 10) i++;

Page 6: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

6

C.S. Yee 11

The Arduino Briefing

• Control Flow 2 : while Syntax:

while ( expression or condition ){

<statement(s)>;}

Example:while ( i < 10)

{i = i + 1;

}or

while (i < 10) i++;

C.S. Yee 12

The Arduino Briefing

• Control Flow 3 : for loopSyntax:

for ( <expr-1>, <expr-2>, <expr-3>){

<statement(s)>;}

<expr-1> : assignment / initial condition<expr-2> : relational expression<expr-3> : increment / decrement steps

Example:for (i=0; i<10; i++)

{PORTA = i; }

or for (i=0; i<10; i++) PORTA = i;

Page 7: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

7

C.S. Yee 13

The Arduino Briefing

• Control Flow 4: switchSyntax:

switch( variable) {Case value1:

// Do this if variable has value1break;

Case value2:// Do this if variable has value2break;

// And so on, until the last known case value. default:

// Do this if doesn’t fit any of the above cases.break;

}

C.S. Yee 14

The Arduino Briefing

• Control Flow 5 : break

The break statement is used to get out of the while, for and switch loop without finishing the full number of cycles.

• PreprocessorPre-conditions for the compiler before creating executable program for the

MicroController.

Declaring constants:#define PI 3.14159#define FORWARD 0#define BACKWARD 1

Adding header files:#include <Servo.h>

Page 8: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

8

C.S. Yee 15

The Arduino Briefing• Useful Standard Arduino functions

pinMode(pin#, IO); // Configures digital pin to Input or Outputpin# is digital pin numberIO can be I for INPUT or O for OUTPUTNormally placed in setup().

int val = digitalRead(pin#); // Read from Digital input pinpin# is digital pin numbervalue should be 0 or 1.

digitalWrite(pin#, value); // Write to Digital output pin.pin# is digital pin numbervalue can be 0 or 1, LOW or HIGH

int val = analogRead(pin#); // Read from Analog input pinpin# is analog Input pin numbervalue will be from 0 to 1023 (corresponding to 0 to 5V analog input)

C.S. Yee 16

The Arduino Briefing• Useful Standard Arduino functions

analogWrite(pin#, value); // Write to control PWM outputpin# is digital pin number of PWM pinvalue can be from 0 (for 0% duty cycle) to 255 (100% duty cycle)

delay(value); // delay in millisecondsvalue can be 0 to 32767, in ms.

delayMicroseconds(value); // delay in microsecondsvalue can be in LONG format, 12345678UL

int val = min(x, y); // Returns smaller valueint val = max(x, y); // Returns bigger valueint val = abs(x, y); // Returns absolute value, only positive

Page 9: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

9

C.S. Yee 17

The Arduino Briefing• Servo Library for RC Servo Motors

#include <Servo.h>

Routines• attach() Attach the Servo variable to a pin. • write() Writes a value to the servo, controlling the shaft

accordingly. (0 to 180, 90 being the neutral position.)• writeMicroseconds() Writes a value in microseconds (uS) to the

servo, controlling the shaft accordingly. (typically 1000 to 2000, 1500 being the neutral position.)

• read() Read the current angle of the servo. (0 to 180)• attached() Check whether the Servo variable is attached to a pin.• detach() Detach the Servo variable from its pin.

C.S. Yee 18

The Arduino Briefing• Servo Library Sample

#include <Servo.h>

Servo myservo;

void setup() { myservo.attach(9); // Attach servo to pin 9myservo.write(90); // set servo to mid-point

}

void loop() {// Main part of the program}

Page 10: 3/2/2014 The Arduino Briefing - National University of ...guppy.mpe.nus.edu.sg/me3/Arduino_Programming1.pdf · • Arduino Leonardo - I/O configuration Analog INPUT pins – Default

3/2/2014

10

C.S. Yee 19

The Arduino Briefing• Arduino Resources

http://arduino.cc/ // Arduino Home Page

This is the Arduino Home page, where Arduino related information can be found.

The latest Arduino Software can also be downloaded here, and it is freeware.

Tutorials and references to the programming or Arduino boards can be found under Learning.

Join the Forum under Support to mingle with other Arduino experimenters to discuss problems and search for solutions, or sample programs. Don’t trust everything you read, but try them out yourselves!!

You may download the essential tutorial, reference and software files from

http://guppy.mpe.nus.edu.sg/me3design.htmlFrom 11 January 2014 onwards.


Recommended