Embedded Systems: Assist. Prof. Rassim Suliyev - SDU 2018 Week 3
Embedded Systems: Assist. Prof. Rassim Suliyev - SDU 2018 Week 3
Click on
“Serial
Monitor”
button to see
output
Arduino Communications
Is just serial communications
Arduino doesn’t really do USB
It really is “serial”, like old RS-232 serial
All microcontrollers can do serial
Not many can do USB
Serial is easy, USB is hard
Output is:
The number is 0
The number is 1
The number is 2
Baud rate
strcmp("left", "leftcenter") == 0)
// this will evaluate to false
strncmp("left", "leftcenter", 4) == 0)
// this will evaluate to true
String Object
charAt(n) or [n] - Access a particular character of the String
concat(parameter) or + - Appends the parameter to a String
endsWith(string2) - Tests whether or not a String ends with string2
equals(string2) or == - Compares two strings for equality (case sensitive)
indexOf(val, [strt]) – locates val in a String by searching forward starting from strt
index. To search backward use lastIndexOf(val,[strt])
length() - Returns the length of the String, in characters
remove(index,[count]) – remove all characters (or count caracters if given) from a
String starting from index
replace(substring1, substring2) – Replace all instances of substring1 in a String to
substring2
setCharAt(index, c) - Sets a character to c at index of the String
startsWith(string2) - Tests whether or not a String starts with the string2
substring(from, [to]) - Get a substring of a String, from - inclusive, to – exclusive
toInt() or toFloat() - Converts a valid String to an integer or float
toLowerCase() or toUpperCase() - Get a lower-case or upper-case version of a String
trim() - Get a version of the String with any leading and trailing whitespace removed
Mathematical Operators
Comparing Character and Numeric
Values
Logical and Bitwise operators
Logical
operators
Bitwise
operators
Combining Operations and
Assignment
Advanced Mathematical Operators
Other Useful Operators
Functions
type functionName(parameters)
{
statements;
}
Functions are declared by first declaring the function type. This is the type of
value to be returned by the function such as 'int' for an integer type function. If
no value is to be returned the function type would be void. After type, declare the
name given to the function and in parenthesis any parameters being passed to the
function.
int delayVal()
{
int v; // create temporary variable 'v'
v = analogRead(pot); // read potentiometer value
v /= 4; // converts 0-1023 to 0-255
return v; // return final value
}
Receiving Serial Data in Arduino
Serial.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)
Serial.read() - Reads incoming serial data
Serial.readBytes(buffer, length) - reads characters
from the serial port into a buffer. The function
terminates if the determined length has been read,
or it times out
Controlling Arduino
int ledPin = 13; // choose a pin for LED
int val = 0; // variable to store the data received via Serial port
void setup() {
pinMode(ledPin,OUTPUT); // make ledPin an output
Serial.begin(9600); // initialize the Serial port
}
void loop () {
// Serial.available() – is a method to see whether something is
// received or not via Serial port without pausing the main program
if( Serial.available() ) {
val = Serial.read(); // read the value received via Serial port
if( val == 'H' ) { // if ‘H’, then blink
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
}
}