Arduino Commands

Ace your homework & exams now with Quizwiz!

loop()

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board

String - object

An object is a construct that contains both data and functions. A String object can be created just like a variable and assigned a value or string. The String object contains functions (which are called "methods" in object oriented programming (OOP)) which operate on the string data contained in the String object. Syntax: String(val) String(val, base) String(val, decimalPlaces)

byte

Bytes store an 8-bit number, from 0 to 255. byte is an unsigned data type, meaning that it does not store negative numbers. Syntax: byte var = val;

serialEvent()

Called when data is available. Use Serial.read() to capture this data. Syntax: void serialEvent() { //statements } For boards with additional serial ports Ex. void serialEvent1() { //statements }

bitClear()

Clears (writes a 0 to) a bit of a numeric variable. Syntax: bitClear(x, n)

bit()

Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.). Syntax: bit(n)

analogReference()

Configures the reference voltage used for analog input Syntax: analogReference(type) Options on: www.arduino.cc/reference/en/language/functions/analog-io/analogreference/

pinMode()

Configures the specified pin to behave either as an input or an output Syntax: pinMode(pin, mode)

constrain()

Constrains a number to be within a range. Syntax: constrain(x, a, b)

byte()

Converts a value to the byte data type Syntax: byte(x) (byte)x (C-style type conversion)

char()

Converts a value to the char data type. char - A data type used to store a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC"). Characters are stored as numbers however. This means that it is possible to do arithmetic on characters, in which the ASCII value of the character is used Syntax: char(x) (char)x (C-style type conversion)

float()

Converts a value to the float data type. float - Datatype for floating-point numbers, a number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information. Syntax: float(x) (float)x (C-style type conversion)

int()

Converts a value to the int data type. int - Integers are your primary data-type for number storage. On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). int's store negative numbers with a technique called (2's complement math). The highest bit, sometimes referred to as the "sign" bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added. The Arduino takes care of dealing with negative numbers for you, so that arithmetic operations work transparently in the expected manner. Syntax: int(x) (int)x (C-style type conversion)

long()

Converts a value to the long data type. long - Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647. If doing math with integers, at least one of the numbers must be followed by an L, forcing it to be a long Syntax: long(x) (long)x (C-style type conversion)

word()

Converts a value to the word data type. word - A word can store an unsigned number of at least 16 bits (from 0 to 65535). Syntax: word(x) word(h, l) (word)x (C-style type conversion)

noInterrupts()

Disables interrupts (you can re-enable them with interrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code. Syntax: noInterrupts()

end()

Disables serial communication, allowing the RX and TX pins to be used for general input and output. To re-enable serial communication, call Serial.begin(). Syntax: Serial.end()

highByte()

Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type). Syntax: highByte(x)

lowByte()

Extracts the low-order (rightmost) byte of a variable (e.g. a word). Syntax: lowByte(x)

tone()

Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones. Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency Syntax: tone(pin, frequency) tone(pin, frequency, duration)

available()

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). Syntax: Serial.available()

availableForWrite()

Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation. Syntax: Serial.availableForWrite()

if(Serial)

Indicates if the specified Serial port is ready. On the boards with native USB, if (Serial) (or if(SerialUSB) on the Due) indicates whether or not the USB CDC serial connection is open. For all other boards, and the non-USB CDC ports, this will always return true. Syntax: if (Serial)

parseInt()

Looks for the next valid integer in the incoming serial. The function terminates if it times out (see Serial.setTimeout()). Serial.parseInt() inherits from the Stream utility class. In particular: Parsing stops when no characters have been read for a configurable time-out value, or a non-digit is read; If no valid digits were read when the time-out (see Serial.setTimeout()) occurs, 0 is returned; Syntax: Serial.parseInt() Serial.parseInt(lookahead) Serial.parseInt(lookahead, ignore)

word

On the Uno and other ATMEGA based boards, a word stores a 16-bit unsigned number. On the Due and Zero, it stores a 32-bit unsigned number. Syntax: word var = val;

delayMicroseconds()

Pauses the program for the amount of time (in microseconds) specified by the parameter. There are a thousand microseconds in a millisecond and a million microseconds in a second. Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead. Syntax: delayMicroseconds(us)

delay()

Pauses the program for the amount of time (in milliseconds) specified as parameter. (There are 1000 milliseconds in a second.) Syntax: delay(ms)

OUTPUT

Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. Atmega pins can source (provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current to other devices/circuits. This makes them useful for powering LED's but useless for reading sensors Pins configured as outputs can also be damaged or destroyed if short circuited to either ground or 5 volt power rails. The amount of current provided by an Atmega pin is also not enough to power most relays or motors, and some interface circuitry will be required.

println()

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 Serial.print(). Syntax: Serial.println(val) Serial.println(val, format)

print()

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 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. Syntax: Serial.print(val) Serial.print(val, format)

interrupts()

Re-enables interrupts (after they've been disabled by nointerrupts(). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code. Syntax: interrupts()

map()

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc. Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The constrain() function may be used either before or after this function, if limits to the ranges are desired Syntax: map(value, fromLow, fromHigh, toLow, toHigh)

bitRead()

Reads a bit of a number. Syntax: bitRead(x, n)

pulseIn()

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout. The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length. Syntax: pulseIn(pin, value) pulseIn(pin, value, timeout)

read()

Reads incoming serial data. Syntax: Serial.read()

digitalRead()

Reads the value from a specified digital pin, either HIGH or LOW. Syntax: digitalRead(pin)

analogRead()

Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023. Syntax: analogRead(pin)

peek()

Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer. That is, successive calls to peek() will return the same character, as will the next call to read(). Syntax: Serial.peek()

micros()

Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes. On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds. Syntax: time = micros()

millis()

Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days. Syntax: time = millis()

find()

Serial.find() reads data from the serial buffer until the target is found. The function returns true if target is found, false if it times out. Syntax: Serial.find(target) Serial.find(target, length)

findUntil()

Serial.findUntil() reads data from the serial buffer until a target string of given length or terminator string is found. The function returns true if the target string is found, false if it times out. Syntax: Serial.findUntil(target, terminal)

parseFloat()

Serial.parseFloat() returns the first valid floating point number from the Serial buffer. parseFloat() is terminated by the first character that is not a floating point number. The function terminates if it times out (see Serial.setTimeout()). Syntax: Serial.parseFloat() Serial.parseFloat(lookahead) Serial.parseFloat(lookahead, ignore)

readBytes()

Serial.readBytes() reads characters from the serial port into a buffer. The function terminates if the determined length has been read, or it times out (see Serial.setTimeout()). Serial.readBytes() returns the number of characters placed in the buffer. A 0 means no valid data was found. Syntax: Serial.readBytes(buffer, length)

readBytesUntil()

Serial.readBytesUntil() reads characters from the serial buffer into an array. The function terminates (checks being done in this order) if the determined length has been read, if it times out (see Serial.setTimeout()), or if the terminator character is detected (in which case the function returns the characters up to the last character before the supplied terminator). The terminator itself is not returned in the buffer. Serial.readBytesUntil() returns the number of characters read into the buffer. A 0 means that the length parameter <= 0, a time out occurred before any other input, or a termination character was found before any other input. Syntax: Serial.readBytesUntil(character, buffer, length)

readString()

Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out Syntax: Serial.readString()

setTimeout()

Serial.setTimeout() sets the maximum milliseconds to wait for serial data. It defaults to 1000 milliseconds. Syntax: Serial.setTimeout(time)

bitSet()

Sets (writes a 1 to) a bit of a numeric variable. Syntax: bitSet(x, n)

begin()

Sets the data rate in bits per second (baud) for serial data transmission. For communicating with Serial Monitor, make sure to use one of the baud rates listed in the menu at the bottom right corner of its screen. 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: Serial.begin(speed) Serial.begin(speed, config)

shiftIn()

Shifts in a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. For each bit, the clock pin is pulled high, the next bit is read from the data line, and then the clock pin is taken low. If you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the first call to shiftIn(), e.g. with a call to digitalWrite(clockPin, LOW). Syntax: byte incoming = shiftIn(dataPin, clockPin, bitOrder)

shiftOut()

Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available. Note- if you're interfacing with a device that's clocked by rising edges, you'll need to make sure that the clock pin is low before the call to shiftOut(), e.g. with a call to digitalWrite(clockPin, LOW). Syntax: shiftOut(dataPin, clockPin, bitOrder, value)

noTone()

Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated. Syntax: noTone(pin)

PROGMEM

Store data in flash (program) memory instead of SRAM. There's a description of the various types of memory available on an Arduino board. The PROGMEM keyword is a variable modifier, it should be used only with the datatypes defined in pgmspace.h. It tells the compiler "put this information into flash memory", instead of into SRAM, where it would normally go. Syntax: const dataType variableName[] PROGMEM = {data0, data1, data3...​}; const dataType variableName[] PROGMEM = {}; // use this form const PROGMEM dataType variableName[] = {}; // or this one const dataType PROGMEM variableName[] = {}; // not this one

attachInterrupt()

The first parameter to attachInterrupt() is an interrupt number. Normally you should use digitalPinToInterrupt(pin) to translate the actual digital pin to the specific interrupt number. For example, if you connect to pin 3, use digitalPinToInterrupt(3) as the first parameter to attachInterrupt(). Syntax: attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended) attachInterrupt(interrupt, ISR, mode) (not recommended) attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

Keyboard

The keyboard functions enable 32u4 or SAMD micro based boards to send keystrokes to an attached computer through their micro's native USB port.

LOW

The meaning of LOW also has a different meaning depending on whether a pin is set to INPUT or OUTPUT. When a pin is configured as an INPUT with pinMode, and read with digitalRead, the microcontroller will report LOW if a voltage of 2 volts or less is present at the pin. When a pin is configured to OUTPUT with pinMode, and set to LOW with digitalWrite, the pin is at 0 volts. In this state it can sink current, i.e. light an LED that is connected through a series resistor to, +5 volts, or to another pin configured as an output, and set to HIGH.

Mouse

The mouse functions enable 32u4 or SAMD micro based boards to control cursor movement on a connected computer through their micro's native USB port. When updating the cursor position, it is always relative to the cursor's previous location.

random()

The random function generates pseudo-random numbers. Syntax: random(max) random(min, max)

detachInterrupt()

Turns off the given interrupt. Syntax: detachInterrupt(digitalPinToInterrupt(pin)) (recommended) detachInterrupt(interrupt) (not recommended) detachInterrupt(pin) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

Serial

Used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.

flush()

Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.) Syntax: Serial.flush()

digitalWrite()

Write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the Digital Pins tutorial for more information. If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor. Syntax: digitalWrite(pin, value)

bitWrite()

Writes a bit of a numeric variable. Syntax: bitWrite(x, n, b)

analogWrite()

Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin Syntax: analogWrite(pin, value)

write()

Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead. Syntax: Serial.write(val) Serial.write(str) Serial.write(buf, len)

analogReadResolution()

analogReadResolution() is an extension of the Analog API for the Arduino Due, Zero and MKR Family. Sets the size (in bits) of the value returned by analogRead(). It defaults to 10 bits (returns values between 0-1023) for backward compatibility with AVR based boards. Syntax: analogReadResolution(bits)

analogWriteResolution()

analogWriteResolution() is an extension of the Analog API for the Arduino Due. analogWriteResolution() sets the resolution of the analogWrite() function. It defaults to 8 bits (values between 0-255) for backward compatibility with AVR based boards. Due - 12 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution. 2 pins with 12-bit DAC (Digital-to-Analog Converter) By setting the write resolution to 12, you can use analogWrite() with values between 0 and 4095 to exploit the full DAC resolution or to set the PWM signal without rolling over. Zero - 10 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution. 1 pin with 10-bit DAC (Digital-to-Analog Converter). By setting the write resolution to 10, you can use analogWrite() with values between 0 and 1023 to exploit the full DAC resolution Syntax: analogWriteResolution(bits)

boolean variables

boolean variables are hold one of two values, true and false.

setup()

called when your program starts. Use it to initialize your variables, pin modes, start using libraries, etc. The setup function will only run once, after each power-up or reset of the Arduino board.

HIGH

different depending on whether a pin is set to an INPUT or OUTPUT. When a pin is configured as an INPUT with pinMode, and read with digitalRead, the microcontroller will report HIGH if a voltage of 3 volts or more is present at the pin. When a pin is configured to OUTPUT with pinMode, and set to HIGH with digitalWrite, the pin is at 5 volts. In this state it can source current, e.g. light an LED that is connected through a series resistor to ground, or to another pin configured as an output, and set to LOW.

true

often said to be defined as 1, which is correct, but true has a wider definition. Any integer which is non-zero is TRUE, in a Boolean sense. So -1, 2 and -200 are all defined as true, too, in a Boolean sense

INPUT

pins configured as INPUT make extremely small demands on the circuit that they are sampling, say equivalent to a series resistor of 100 Megohms in front of the pin. This makes them useful for reading a sensor, but not powering an LED.

randomSeed()

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. Syntax: randomSeed(seed)

readStringUntil()

readStringUntil() reads characters from the serial buffer into a String. The function terminates if it times out Syntax: Serial.readStringUntil(terminator)

false

the easier of the two to define. false is defined as 0 (zero)


Related study sets

Introduction to Computing using Matlab

View Set

ASEN 3113 Exam 2 - Clickers & Quiz Questions

View Set

accounting chapter 10 - Reporting and analyzing LIABILITIES

View Set

Major Quiz #1, PBHL 2663 Major Quiz #2 (Modules 6-8), PBHL 2663 Major Quiz #3 (Modules 9-12), Major Quiz #4 (Modules 13-15)

View Set

Exam: 01.02 What is Economics Quiz

View Set