0% found this document useful (0 votes)
3 views

Arduino

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Arduino

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Types & Built-in Constants

Data Types:

●​ int: A 16-bit integer (e.g., -32,768 to 32,767).


●​ unsigned int: A 16-bit unsigned integer (e.g., 0 to 65,535).
●​ long: A 32-bit integer (e.g., -2,147,483,648 to 2,147,483,647).
●​ unsigned long: A 32-bit unsigned integer (e.g., 0 to 4,294,967,295).
●​ float: A single-precision floating-point number.
●​ double: A double-precision floating-point number (same as float on Arduino Uno).
●​ char: An 8-bit ASCII character.
●​ boolean: A true/false value.

Built-in Constants:

●​ HIGH: Represents a high digital value (usually 5V).


●​ LOW: Represents a low digital value (usually 0V).
●​ INPUT: Defines a pin as an input.
●​ OUTPUT: Defines a pin as an output.
●​ INPUT_PULLUP: Enables internal pull-up resistor for an input pin.

Example Program:

void setup() {
Serial.begin(9600);

// Data Types
int integerVar = 100;
unsigned int unsignedVar = 50000;
long longVar = 100000L;
unsigned long ulongVar = 4000000000UL;
float floatVar = 3.14;
double doubleVar = 3.14159;
char charVar = 'A';
boolean boolVar = true;

// Built-in Constants
pinMode(13, OUTPUT); // Set pin 13 as output
digitalWrite(13, HIGH); // Set pin 13 to high (LED on)
Serial.print("Integer: ");
Serial.println(integerVar);
Serial.print("Unsigned Integer: ");
Serial.println(unsignedVar);
Serial.print("Long: ");
Serial.println(longVar);
Serial.print("Unsigned Long: ");
Serial.println(ulongVar);
Serial.print("Float: ");
Serial.println(floatVar);
Serial.print("Double: ");
Serial.println(doubleVar);
Serial.print("Char: ");
Serial.println(charVar);
Serial.print("Boolean: ");
Serial.println(boolVar);
}

void loop() {
// Your code here
}

Output:

Integer: 100
Unsigned Integer: 50000
Long: 100000
Unsigned Long: 4000000000
Float: 3.14
Double: 3.14159
Char: A
Boolean: 1
Operators

Arithmetic Operators:

●​ +: Addition
●​ -: Subtraction
●​ *: Multiplication
●​ /: Division
●​ %: Modulus

Bitwise Operators:

●​ &: Bitwise AND


●​ |: Bitwise OR
●​ ^: Bitwise XOR
●​ ~: Bitwise NOT
●​ <<: Bitwise shift left
●​ >>: Bitwise shift right

Compound Operators:

●​ +=: Addition assignment


●​ -=: Subtraction assignment
●​ *=: Multiplication assignment
●​ /=: Division assignment
●​ %=: Modulus assignment

Comparison Operators:

●​ ==: Equality
●​ !=: Inequality
●​ >: Greater than
●​ <: Less than
●​ >=: Greater than or equal to
●​ <=: Less than or equal to

Boolean Operators:

●​ &&: Logical AND


●​ ||: Logical OR
●​ !: Logical NOT
Program:

void setup() {
Serial.begin(9600);

// Arithmetic Operators
int a = 10;
int b = 5;
Serial.print("Sum: ");
Serial.println(a + b); // 15
Serial.print("Difference: ");
Serial.println(a - b); // 5
Serial.print("Product: ");
Serial.println(a * b); // 50
Serial.print("Quotient: ");
Serial.println(a / b); // 2
Serial.print("Remainder: ");
Serial.println(a % b); // 0

// Bitwise Operators
int x = 12; // Binary: 1100
int y = 7; // Binary: 0111
Serial.print("Bitwise AND: ");
Serial.println(x & y); // 4 (Binary: 0100)
Serial.print("Bitwise OR: ");
Serial.println(x | y); // 15 (Binary: 1111)
Serial.print("Bitwise XOR: ");
Serial.println(x ^ y); // 11 (Binary: 1011)
Serial.print("Bitwise NOT: ");
Serial.println(~x); // -13 (Binary: 0011)
Serial.print("Shift Left: ");
Serial.println(x << 2); // 48 (Binary: 110000)
Serial.print("Shift Right: ");
Serial.println(x >> 2); // 3 (Binary: 0011)

// Compound Operators
int z = 10;
z += 5; // z = z + 5
Serial.print("Compound Addition: ");
Serial.println(z); // 15

// Comparison Operators
Serial.print("Is a equal to b? ");
Serial.println(a == b); // false
Serial.print("Is a not equal to b? ");
Serial.println(a != b); // true
Serial.print("Is a greater than b? ");
Serial.println(a > b); // true
Serial.print("Is a less than b? ");
Serial.println(a < b); // false

// Boolean Operators
bool cond1 = true;
bool cond2 = false;
Serial.print("Logical AND: ");
Serial.println(cond1 && cond2); // false
Serial.print("Logical OR: ");
Serial.println(cond1 || cond2); // true
Serial.print("Logical NOT: ");
Serial.println(!cond1); // false
}

void loop() {
// Your code here
}

Output:

Sum: 15
Difference: 5
Product: 50
Quotient: 2
Remainder: 0
Bitwise AND: 4
Bitwise OR: 15
Bitwise XOR: 11
Bitwise NOT: -13
Shift Left: 48
Shift Right: 3
Compound Addition: 15
Is a equal to b? 0
Is a not equal to b? 1
Is a greater than b? 1
Is a less than b? 0
Logical AND: 0
Logical OR: 1
Logical NOT: 0
Control Statements and Loops

Control Statements:

1.​ if Statement
○​ Executes a block of code if its condition is true.

Structure:

if (condition) {

// code to execute

2. else: Executes code if if condition is false

Structure:

if (condition) {

// code if true

} else {

// code if false

3. else if: Checks additional conditions

Structure:

if (condition1) { // code if condition1 is true

} else if (condition2) {// code if condition2 is true}

else {// code if all conditions are false

}
Program:

void setup() {
Serial.begin(9600);
int temperature = 25;

if (temperature > 30) {


Serial.println("Hot");
} else if (temperature > 20) {
Serial.println("Warm");
} else {
Serial.println("Cold");
}
}

void loop() {
// Your code here
}

Output:

Warm
Loop:

●​ for Loop: Repeats code a specified number of times

for (initialization; condition; increment)

{ // code to execute }

●​ while Loop: Repeats code while a condition is true

while (condition) { // code to execute }

●​ do...while Loop: Repeats code at least once, then while condition is true

do { // code to execute } while (condition)


Functions and Library Functions

User-Defined Functions:

●​ Definition: Functions created to perform specific tasks.

Syntax:

returnType functionName(parameters) {

// code to execute

return value; // if needed

Example:

int add(int a, int b) {

return a + b;

}
I/O Functions

1.​ digitalRead(pin)
○​ Reads the value from a specified digital pin (HIGH or LOW).

Example Program:​
void setup() {

Serial.begin(9600);

pinMode(2, INPUT); // Set pin 2 as input

void loop() {

int buttonState = digitalRead(2); // Read the state of pin 2

Serial.print("Button State: ");

Serial.println(buttonState);

delay(1000); // Wait for 1 second

Expected Output:​

Button State: 0

Button State: 1

2.​ digitalWrite(pin, value)


○​ Writes a HIGH or LOW value to a specified digital pin.

Example Program:​
void setup() {

pinMode(13, OUTPUT); // Set pin 13 as output

}
void loop() {

digitalWrite(13, HIGH); // Turn on LED

delay(1000); // Wait for 1 second

digitalWrite(13, LOW); // Turn off LED

delay(1000); // Wait for 1 second

3.​ Expected Output:


○​ The LED on pin 13 will blink on and off every second.
4.​ pinMode(pin, mode)
○​ Configures a pin to behave either as an input or an output.

Example Program:​

void setup() {

pinMode(9, OUTPUT); // Set pin 9 as output

void loop() {

// No additional code needed for this example

5.​ analogRead(pin)
○​ Reads the value from an analog pin (0 to 1023).

Example Program:​

void setup() {

Serial.begin(9600);
}

void loop() {

int sensorValue = analogRead(A0); // Read value from analog pin


A0

Serial.print("Analog Value: ");

Serial.println(sensorValue);

delay(1000); // Wait for 1 second

Expected Output:​

Analog Value: [value between 0 and 1023]

6.​ analogWrite(pin, value)


○​ Writes an analog value (PWM wave) to a pin.

Example Program:​

void setup() {

pinMode(9, OUTPUT); // Set pin 9 as output

void loop() {

for (int value = 0; value < 255; value++) {

analogWrite(9, value); // Fade LED brightness

delay(10);

for (int value = 255; value > 0; value--) {


analogWrite(9, value); // Fade LED brightness

delay(10);

7.​ Expected Output:


○​ The LED connected to pin 9 will fade in and out.
8.​ analogReference(type)
○​ Configures the analog reference voltage.

Example Program:​

void setup() {

analogReference(EXTERNAL); // Use external reference voltage

void loop() {

// No additional code needed for this example

Char Functions

1.​ isAlpha(c)
○​ Checks if a character is an alphabetic letter.

Example Program:​

void setup() {

Serial.begin(9600);

char myChar = 'A';


Serial.print("Is '");

Serial.print(myChar);

Serial.print("' an alpha character? ");

Serial.println(isAlpha(myChar));

void loop() {

// Your code here

Expected Output:​
Is 'A' an alpha character? 1

2.​ isAlphaNumeric(c)
○​ Checks if a character is an alphanumeric letter.

Example Program:​
void setup() {

Serial.begin(9600);

char myChar = '1';

Serial.print("Is '");

Serial.print(myChar);

Serial.print("' an alphanumeric character? ");

Serial.println(isAlphaNumeric(myChar));

}
void loop() {

// Your code here

Expected Output:​

Is '1' an alphanumeric character? 1

3.​ isDigit(c)
○​ Checks if a character is a digit.

Example Program:​
void setup() {

Serial.begin(9600);

char myChar = '7';

Serial.print("Is '");

Serial.print(myChar);

Serial.print("' a digit? ");

Serial.println(isDigit(myChar));

void loop() {

// Your code here

Expected Output:​

Is '7' a digit? 1
4.​ isHexadecimalDigit(c)
○​ Checks if a character is a hexadecimal digit.

Example Program:​
void setup() {

Serial.begin(9600);

char myChar = 'F';

Serial.print("Is '");

Serial.print(myChar);

Serial.print("' a hexadecimal digit? ");

Serial.println(isHexadecimalDigit(myChar));

void loop() {

// Your code here

Expected Output:​
Is 'F' a hexadecimal digit? 1

5.​ isSpace(c)
○​ Checks if a character is a whitespace character.

Example Program:​

void setup() {

Serial.begin(9600);

char myChar = ' ';


Serial.print("Is '");

Serial.print(myChar);

Serial.print("' a whitespace character? ");

Serial.println(isSpace(myChar));

void loop() {

// Your code here

Expected Output:​
Is ' ' a whitespace character? 1

6.​ isWhitespace(c)
○​ Checks if a character is a whitespace character (same as isSpace).

Example Program:​
void setup() {

Serial.begin(9600);

char myChar = '\t';

Serial.print("Is '");

Serial.print(myChar);

Serial.print("' a whitespace character? ");

Serial.println(isWhitespace(myChar));

}
void loop() {

// Your code here

Expected Output:​
Is '​ ' a whitespace character? 1

7.​ isUpperCase(c)
○​ Checks if a character is an uppercase letter.

Example Program:​
void setup() {

Serial.begin(9600);

char myChar = 'G';

Serial.print("Is '");

Serial.print(myChar);

Serial.print("' an uppercase letter? ");

Serial.println(isUpperCase(myChar));

void loop() {

// Your code here

}
Expected Output:​


Is 'G' an uppercase letter? 1

8.​ isLowerCase(c)
○​ Checks if a character is a lowercase letter.

Example Program:​
void setup() {

Serial.begin(9600);

char myChar = 'm';

Serial.print("Is '");

Serial.print(myChar);

Serial.print("' a lowercase letter? ");

Serial.println(isLowerCase(myChar));

void loop() {

// Your code here

Expected Output:​
Is 'm' a lowercase letter? 1
Math Functions

1.​ abs(x)
○​ Returns the absolute value of x.

Example Program:​
void setup() {

Serial.begin(9600);

int value = -50;

Serial.print("Absolute value of ");

Serial.print(value);

Serial.print(" is ");

Serial.println(abs(value));

void loop() {

// Your code here

Expected Output:​

Absolute value of -50
constrain(x, a, b)
The constrain() function limits the value of x to the range between a and b. If x is smaller
than a, it returns a. If x is larger than b, it returns b. Otherwise, it returns x.​
Syntax:​

constrain(x, a, b)

●​
○​ x: the value to constrain.
○​ a: the lower bound.
○​ b: the upper bound.

Example Program:​
void setup() {
Serial.begin(9600);
int value = 150;
int constrainedValue = constrain(value, 10, 100);

Serial.print("Original value: ");


Serial.println(value);
Serial.print("Constrained value: ");
Serial.println(constrainedValue);
}

void loop() {
// Your code here
}
Expected Output:​
Original value: 150
Constrained value: 100

2. max(x, y)
The max() function returns the larger of the two values, x or y.​
Syntax:​
max(x, y)
Example Program:​
void setup() {
Serial.begin(9600);
int a = 10;
int b = 20;
int maximum = max(a, b);

Serial.print("Maximum of ");
Serial.print(a);
Serial.print(" and ");
Serial.print(b);
Serial.print(" is ");
Serial.println(maximum);
}

void loop() {
// Your code here
}
Expected Output:​
Maximum of 10 and 20 is 20

3. min(x, y)
The min() function returns the smaller of the two values, x or y.​
Syntax:​
min(x, y)
Example Program:​
void setup() {
Serial.begin(9600);
int a = 10;
int b = 20;
int minimum = min(a, b);

Serial.print("Minimum of ");
Serial.print(a);
Serial.print(" and ");
Serial.print(b);
Serial.print(" is ");
Serial.println(minimum);
}

void loop() {
// Your code here
}
Expected Output:​
Minimum of 10 and 20 is 10
4. pow(base, exponent)
The pow() function calculates the power of a number. It returns base raised to the power
of exponent.​
Syntax:​
pow(base, exponent)
Example Program:​

void setup() {
Serial.begin(9600);
int base = 2;
int exponent = 3;
double result = pow(base, exponent);

Serial.print(base);
Serial.print("^");
Serial.print(exponent);
Serial.print(" = ");
Serial.println(result);
}

void loop() {
// Your code here
}
Expected Output:​

2^3 = 8

5. sqrt(x)
The sqrt() function calculates the square root of a number.​
Syntax:​
sqrt(x)
Example Program:​
void setup() {
Serial.begin(9600);
int number = 16;
double squareRoot = sqrt(number);

Serial.print("Square root of ");


Serial.print(number);
Serial.print(" is ");
Serial.println(squareRoot);
}

void loop() {
// Your code here
}

Expected Output:​
Square root of 16 is 4

Serial Communication Functions

1.​ Serial.begin(baudRate)
○​ Initializes serial communication at a specified baud rate.

Example Program:​
void setup() {
Serial.begin(9600); // Start serial communication at 9600 baud
}

void loop() {
Serial.println("Hello, World!"); // Send "Hello, World!" over serial
delay(1000); // Wait for 1 second
}
Expected Output:​
Hello, World!
Hello, World!
(Repeats every 1 second)

2.​ Serial.print(data)
○​ Prints data to the serial monitor without a newline.

Example Program:​
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.print("Temperature: ");
Serial.print(25); // Prints 25 without moving to the next line
Serial.print("C");
delay(1000);
}
Expected Output:​

Temperature: 25C
3.​ Serial.println(data)
○​ Prints data to the serial monitor followed by a newline (new line after each
output).

Example Program:​
void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("Arduino is awesome!"); // Sends data with a new line after each print
delay(1000);
}
Expected Output:​

Arduino is awesome!
Arduino is awesome!
(Repeats every 1 second)

4.​ Serial.available()
○​ Returns the number of bytes available to read from the serial port.

Example Program:​

void setup() {
Serial.begin(9600); // Start serial communication
}

void loop() {
if (Serial.available() > 0) { // If data is available to read
int incomingByte = Serial.read(); // Read the incoming byte
Serial.print("Received: ");
Serial.println(incomingByte); // Print received byte as an integer
}
}

5.​ Expected Behavior:


○​ The program will print the ASCII value of the character sent via serial
communication (e.g., if you send the letter A, it will print 65).
6.​ Serial.read()
○​ Reads incoming serial data.
Example Program:​

void setup() {
Serial.begin(9600); // Start serial communication
}

void loop() {
if (Serial.available() > 0) {
char incomingChar = Serial.read(); // Read the incoming byte as a char
Serial.print("I received: ");
Serial.println(incomingChar); // Print the received character
}
}

7.​ Expected Output:


○​ If you send the letter A, the output will be:

I received: A

8.​ Serial.readBytes(buffer, length)


○​ Reads multiple bytes from the serial port into a buffer.

Example Program:​
char buffer[10];

void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available() >= 10) { // If 10 or more bytes are available
Serial.readBytes(buffer, 10); // Read 10 bytes into buffer
Serial.print("Received: ");
Serial.write(buffer, 10); // Print the received data
}
}

9.​ Expected Behavior:


○​ The program will wait until it receives 10 bytes and then print the data.
10.​Serial.readString()
○​ Reads all available characters from the serial buffer and returns them as a string.
Example Program:​
void setup() {
Serial.begin(9600); // Start serial communication
}

void loop() {
if (Serial.available() > 0) {
String inputString = Serial.readString(); // Read input as a string
Serial.print("You typed: ");
Serial.println(inputString); // Print the received string
}
}

11.​Expected Output:
○​ If you send the string "Hello", the output will be:


You typed: Hello

12.​Serial.write(data)
○​ Writes binary data to the serial port.

Example Program:​
void setup() {
Serial.begin(9600); // Start serial communication
}

void loop() {
Serial.write(72); // Send the ASCII code for 'H'
delay(1000); // Wait 1 second before sending again
}

13.​Expected Output:
○​ The character H will be printed every 1 second (since the ASCII code for 'H' is
72).
14.​Serial.end()
○​ Disables serial communication.

Example Program:​
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
Serial.println("Ending Serial Communication");
delay(1000);
Serial.end(); // Disable serial communication after printing once
}

15.​Expected Output:​
Ending Serial Communication

Reference:
Arduino Official Website: https://www.arduino.cc/reference/en/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy