Difference between revisions of "Tutorial: Introduction to the Arduino"

From dftwiki3
Jump to: navigation, search
(An Introduction to C in the Arduino Context)
Line 37: Line 37:
 
* Use '''Serial.begin()''' to set the baud rate (# of bits per second)
 
* Use '''Serial.begin()''' to set the baud rate (# of bits per second)
 
* Then output strings with '''Serial.[http://arduino.cc/en/Serial/Println println]( ... )''' or '''Serial.[http://arduino.cc/en/Serial/Print print]( ... )'''
 
* Then output strings with '''Serial.[http://arduino.cc/en/Serial/Println println]( ... )''' or '''Serial.[http://arduino.cc/en/Serial/Print print]( ... )'''
 +
 +
void setup() {               
 +
  Serial.begin( 38400 );
 +
  Serial.println( "Hello there!" );
 +
}
 +
 +
void loop() {
 +
  delay(1000);              // wait for a second
 +
  Serial.println( "hello again!" );
 +
}
 +
 +
====Output numbers====
 +
 +
* from http://arduino.cc/en/Serial/Print
 +
**Serial.print(78) gives "78"
 +
**Serial.print(1.23456) gives "1.23"
 +
**Serial.print(byte(78)) gives "N" (whose ASCII value is 78)
 +
**Serial.print('N') gives "N"
 +
**Serial.print("Hello world.") gives "Hello world."
 +
::An optional second parameter specifies the base (format) to use; permitted values are BYTE, BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example:
 +
**Serial.print(78, BYTE) gives "N"
 +
**Serial.print(78, BIN) gives "1001110"
 +
**Serial.print(78, OCT) gives "116"
 +
**Serial.print(78, DEC) gives "78"
 +
**Serial.print(78, HEX) gives "4E"
 +
**Serial.println(1.23456, 0) gives "1"
 +
**Serial.println(1.23456, 2) gives "1.23"
 +
**Serial.println(1.23456, 4) gives "1.2346"

Revision as of 14:29, 20 April 2011

--D. Thiebaut 15:18, 20 April 2011 (EDT)


The Reference Pages

Good Tutorials on the Web

Notes for Mac Users

An Introduction to C in the Arduino Context

Comments

// this is a comment
/* and so is this */
/* and
    this 
    as 
    well */

Setup() and Loop()

I/O

Output strings from the Arduino to Laptop

  • Done with the Serial library
  • Use Serial.begin() to set the baud rate (# of bits per second)
  • Then output strings with Serial.println( ... ) or Serial.print( ... )
void setup() {                
 Serial.begin( 38400 );
 Serial.println( "Hello there!" );
}

void loop() {
 delay(1000);              // wait for a second
 Serial.println( "hello again!" );
}

Output numbers

  • from http://arduino.cc/en/Serial/Print
    • Serial.print(78) gives "78"
    • Serial.print(1.23456) gives "1.23"
    • Serial.print(byte(78)) gives "N" (whose ASCII value is 78)
    • Serial.print('N') gives "N"
    • Serial.print("Hello world.") gives "Hello world."
An optional second parameter specifies the base (format) to use; permitted values are BYTE, BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example:
    • Serial.print(78, BYTE) gives "N"
    • Serial.print(78, BIN) gives "1001110"
    • Serial.print(78, OCT) gives "116"
    • Serial.print(78, DEC) gives "78"
    • Serial.print(78, HEX) gives "4E"
    • Serial.println(1.23456, 0) gives "1"
    • Serial.println(1.23456, 2) gives "1.23"
    • Serial.println(1.23456, 4) gives "1.2346"