millis()
Description
Returns the number of milliseconds since the Arduino board began running the current program.
This number will overflow (go back to zero), after approximately 50 days.
Parameters
None
Returns
Number of milliseconds since the program started (unsigned long)
Note:
Please note that the return value for millis() is an unsigned long, logic errors may occur if a
programmer tries to do arithmetic with smaller data types such as int's. Even signed long may
encounter errors as its maximum value is half that of its unsigned counterpart.
Example
unsigned long time;
void setup(){
[Link](9600);
}
void loop(){
[Link]("Time: ");
time = millis();
//prints time since program started
[Link](time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
delay() function
The way the delay() function works is pretty simple. It accepts a single integer (or
number) argument. This number represents the time (measured in milliseconds). The
program should wait until moving on to the next line of code when it encounters this
function. However, the problem is, the delay() function is not a good way to make your
program wait, because it is known as a “blocking” function.
delay() function Syntax
delay (ms) ;
where, ms is the time in milliseconds to pause (unsigned long).
Example:
Flashing LED
* ------------
* Turns on and off a light emitting diode(LED) connected to a digital
* pin, in intervals of 2 seconds. *
*/
int ledPin = 13; // LED connected to digital pin 13
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
delayMicroseconds() function
The delayMicroseconds() function accepts a single integer (or number) argument. This number
represents the time and is measured in [Link] are a thousand microseconds in a
millisecond, and a million microseconds in a second. Currently, the largest value that can produce
an accurate delay is 16383. This may change in future Arduino releases. For delays longer than a
few thousand microseconds, you should use the delay() function instead.
delay() function
Syntax
delayMicroseconds (us) ;
Math Function
The Arduino Math library (math.h) includes a number of useful mathematical functions for
manipulating floating-point numbers.
min(x, y)
Description
Calculates the minimum of two numbers.
Parameters
x: the first number, any data type
y: the second number, any data type
Returns
The smaller of the two numbers.
max(x, y)
Description
Calculates the maximum of two numbers.
Parameters
x: the first number, any data type
y: the second number, any data type
Returns
The larger of the two parameter values.
constrain(x, a, b)
Description
Constrains a number to be within a range.
Parameters
x: the number to constrain, all data types
a: the lower end of the range, all data types
b: the upper end of the range, all data types
Returns
x: if x is between a and b
a: if x is less than a
b: if x is greater than b
Random Functions
randomSeed(seed)
Description
randomSeed() initializes the pseudo-random number generator, causing it to start at an arbitrary
point in its random sequence. This sequence, while very long, and random, is always the same.
If it is important for a sequence of values generated by random() to differ, on subsequent executions
of a sketch, use randomSeed() to initialize the random number generator with a fairly random input,
such as analogRead() on an unconnected pin.
Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This
can be accomplished by calling randomSeed() with a fixed number, before starting the random
sequence.
Parameters
long, int - pass a number to generate the seed.
Returns
no returns
Example
long randNumber;
void setup(){
[Link](9600);
randomSeed(analogRead(0));
}
void loop(){
randNumber = random(300);
[Link](randNumber);
delay(50);
}
long random(max) // it generate random numbers from 0 to max
long random(min, max) // it generate random numbers from min to max
random()
Description
The random function generates pseudo-random numbers.
Syntax
random(max)
random(min, max)
Parameters
min - lower bound of the random value, inclusive (optional)
max - upper bound of the random value, exclusive
Returns
a random number between min and max-1 (long)
Note:
If it is important for a sequence of values generated by random() to differ, on subsequent executions
of a sketch, use randomSeed() to initialize the random number generator with a fairly random input,
such as analogRead() on an unconnected pin.
Conversely, it can occasionally be useful to use pseudo-random sequences that repeat exactly. This
can be accomplished by calling randomSeed() with a fixed number, before starting the random
sequence.
Example
long randNumber;
void setup(){
[Link](9600);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}
void loop() {
// print a random number from 0 to 299
randNumber = random(300);
[Link](randNumber);
// print a random number from 10 to 19
randNumber = random(10, 20);
[Link](randNumber);
delay(50);
}
Serial Communication
[Link]()
Description
Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the
computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400,
57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0
and 1 with a component that requires a particular baud rate.
An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no
parity, one stop bit.
Syntax
[Link](speed)
[Link]()
Description
Get the number of bytes (characters) available for reading from the serial port. This is data that's
already arrived and stored in the serial receive buffer (which holds 64 bytes). available() inherits
from the Stream utility class.
Syntax
[Link]()
Arduino Mega only:
[Link]()
[Link]()
[Link]()
Parameters
none
Returns
the number of bytes available to read
Example
int incomingByte = 0; // for incoming serial data
void setup() {
[Link](9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if ([Link]() > 0) {
// read the incoming byte:
incomingByte = [Link]();
// say what you got:
[Link]("I received: ");
[Link](incomingByte, DEC);
}
}
println()
Description
Prints data to the serial port as human-readable ASCII text followed by a carriage return character
(ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms
as [Link]().
Syntax
[Link](val)
[Link](val, format)
Parameters
val: the value to print - any data type
format: specifies the number base (for integral data types) or number of decimal places (for floating
point types)
Returns
size_t (long): println() returns the number of bytes written, though reading that number is optional
Example:
/*
Analog input
reads an analog input on analog in 0, prints the value out.
created 24 March 2006
by Tom Igoe
*/
int analogValue = 0; // variable to hold the analog value
void setup() {
// open the serial port at 9600 bps:
[Link](9600);
}
void loop() {
// read the analog input on pin 0:
analogValue = analogRead(0);
// print it out in many formats:
[Link](analogValue); // print as an ASCII-encoded decimal
[Link](analogValue, DEC); // print as an ASCII-encoded decimal
[Link](analogValue, HEX); // print as an ASCII-encoded hexadecimal
[Link](analogValue, OCT); // print as an ASCII-encoded octal
[Link](analogValue, BIN); // print as an ASCII-encoded binary
// delay 10 milliseconds before the next reading:
delay(10);
}
[Link]()
Description
Prints data to the serial port as human-readable ASCII text. This command can take many forms.
Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII
digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings
are sent as is. For example:
• [Link](78) gives "78"
• [Link](1.23456) gives "1.23"
• [Link]('N') gives "N"
• [Link]("Hello world.") gives "Hello world."
An optional second parameter specifies the base (format) to use; permitted values are 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:
• [Link](78, BIN) gives "1001110"
[Link]()
Description
Reads incoming serial data. read() inherits from the Stream utility class.
Syntax
[Link]()
Arduino Mega only:
[Link]()
[Link]()
[Link]()
Parameters
None
Returns
the first byte of incoming serial data available (or -1 if no data is available) - int
Example
int incomingByte = 0; // for incoming serial data
void setup() {
[Link](9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if ([Link]() > 0) {
// read the incoming byte:
incomingByte = [Link]();
// say what you got:
[Link]("I received: ");
[Link](incomingByte, DEC);
}
}
flush()
Description
Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead
removed any buffered incoming serial data.)
flush() inherits from the Stream utility class.
Syntax
[Link]()
Arduino Mega only:
[Link]()
[Link]()
[Link]()
Parameters
none
Returns
nothing