You are on page 1of 22

EE 260

Lecture 3
Arduino Overview II
Arduino Overview II
• Programming IDE!
• Serial Communications

EE 260 Lecture 3: Arduino Overview II


Software: Arduino IDE
• Based on Wiring—based on C/C++!
• The basic function has two required subroutines:!
• void  setup()  
• void  loop()  
• Includes and define as defined in C.

EE 260 Lecture 3: Arduino Overview II


Programing in C
• Refresh on the C
Language!

• Learn the C Language!

• In this class, it is
assumed that you have
a working knowledge of
C
http://wuweida.u.qiniudn.com/C%20Primer%20Plus%205th%20Edition(ENG).pdf
EE 260 Lecture 3: Arduino Overview II
Simplest Program: Do Nothing
void  setup()  
{  
}  
void  loop()  
{  
}

But it compiles without error!

EE 260 Lecture 3: Arduino Overview II


The Arduino IDE Commands
arduino.cc

Click on the !
Reference!
tab.

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Constants

Digital:
HIGH  |  LOW (logic level)
true  |  false (boolean)
GPIO Configuration:
INPUT  |  OUTPUT  |  INPUT_PULLUP
Numeric:
Integer: B11010101,  -­‐123,  123uL,  0x3C,  0123
Float: -­‐1.2,  1.7e5,  -­‐62E-­‐12
EE 260 Lecture 3: Arduino Overview II
Arduino IDE: Data Types
C C C C

int long char float


16b signed 32b signed 8b ‘’ signed 32b signed

unsigned  int unsigned  long boolean double


16b unsigned 32b unsigned 1b/8b binary 32b signed
C C S S

word byte string array


16b unsigned 8b unsigned 8b*nchar “  ” signed 8b*nelem unsigned

short unsigned  char String void


16b signed 8b signed ?? signed 0b signed

EE 260 Lecture 3: Arduino Overview II


Team Work
Which data types are equivalent? (show all)
C C C C

int long char float


16b signed 32b signed 8b ‘’ signed 32b signed

unsigned  int unsigned  long boolean double


16b unsigned 32b unsigned 1b/8b binary 32b signed
C C S S

word byte string array


16b unsigned 8b unsigned 8b*nchar “  ” signed 8b*nelem unsigned

short unsigned  char String void


16b signed 8b signed ?? signed 0b signed

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: I/O Functions

Digital Digital Analog

INPUT  
OUTPUT   pinMode tone analogReference
INPUT_PULLUP 0,1,2,3,4,5,6,7,8,9,10,11,12,13, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,
A0,A1,A2,A3,A4,A5 A0,A1,A2,A3,A4,A5

HIGH  
LOW digitalWrite noTone analogRead
0,1,2,3,4,5,6,7,8,9,10,11,12,13, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,
A0,A1,A2,A3,A4,A5 A0,A1,A2,A3,A4,A5 A0,A1,A2,A3,A4,A5

HIGH  
LOW digitalRead pulseIn analogWrite
0,1,2,3,4,5,6,7,8,9,10,11,12,13, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,
A0,A1,A2,A3,A4,A5 A0,A1,A2,A3,A4,A5 A0,A1,A2,A3,A4,A5

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Interrupts

• attachInterrupt  
• detachInterrupt  
!

• interrupts()  
• noInterrupts()

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Bits & Bytes
• lowByte!
• highByte!
• bitRead!
• bitWrite!
• bitSet!
• bitClear!
• bit

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Communications

• shiftOut  
• shiftIn  
• Serial

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Serial.xxx
(Serial) flush() print()

available() parseFloat() println()

begin() parseInt() write()

end() peek() read()

find() setTimeout() readBytes()

findUntil() serialEvent() readBytesUntil()

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Basic Operators
Arithmetic Comparison Logic/Pointer Bitwise Assignment

 =    == && & ++

+  != || | -­‐-­‐

-­‐  < ! ^ +=

*  > ~ -­‐=

/  <= *var << *=,  /=

%  >= &var >> &=,  |=

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Functions
MATH MATH MATH Timing

min() constrain() sqrt() millis()

max() map() random() micros()

abs() pow() randomSeed() delay()

sin() cos() tan() delayMicroseconds()

EE 260 Lecture 3: Arduino Overview II


Arduino IDE: Flow Control
• if   • do...  while  

• if...else   • break  

• for   • continue  

• switch  case   • return  

• while
 • goto

EE 260 Lecture 3: Arduino Overview II


Speed Test: LED Flash
Arduino IDE C/C++
void  setup()     void  setup()    
{
 {

  pinMode(13,  OUTPUT);
   pinMode(13,  OUTPUT);

}   }  
void  loop()     void  loop()    
{
 {

digitalWrite(13,  HIGH);
    while(1)  
digitalWrite(13,  LOW);
    {

}        PORTB  |=  0×20;  //  ON

       PORTB  &=  ~0×20;//  OFF

   }

Frequency = 121 kHz }
EE 260 Lecture 3: Arduino Overview II Frequency = 2.66 MHz
Other Benchmarks
 DigitalRead  3.737  us
 DigitalWrite  3.923  us
 pinMode  4.648  us
 multiply  (volatile  byte)  0.654  us
 divide  (volatile  byte)  12.915  us
 multiply  (volatile  integer)  1.472  us
 divide  (volatile  integer)  15.245  us
 multiply  (volatile  long)  4.552  us
 multiply  (single  float)  7.132  us
 multiply  (double  float)  7.132  us
 divide  (double  float)  80.000  us
 random()  137.475  us
 bitSet()  (with  volatile)  0.591  us
 analogRead()  112.000  us
 analogWrite()  11.630  us
 delay(1)  1004.000  us
 delay(100)  99975.000  us
 delayMicroseconds(1)  0.906  us
 delayMicroseconds(5)  4.679  us
 delayMicroseconds(100)  100.250  us
EE 260 Lecture 3: Arduino Overview II
Serial Communication
void  setup()  
{  
   byte  data  =  100;  
   Serial.begin(9600);  
   while(!Serial){  ;  }  
   Serial.print(data,  BIN);  
   Serial.print(",  ");  
   Serial.print(data,  OCT);  
   Serial.print(",  ");  
   Serial.print(data,  DEC);  
   Serial.print(",  ");  
   Serial.print(data,  HEX);  
   Serial.print(",  ");  
   Serial.write(data);  
   Serial.println("  ");  
}  
void  loop()  
{  
}
EE 260 Lecture 3: Arduino Overview II
Arduino Serial: Latency
a measure of the time delay experienced by a system

EE 260 Lecture 3: Arduino Overview II


Arduino Serial: Bandwidth
the rate of data transfer, in kilobits per second (kB/s)

EE 260 Lecture 3: Arduino Overview II

You might also like