[go: up one dir, main page]

0% found this document useful (0 votes)
8 views47 pages

Class 04 - REC-Basic Robotics

The document provides an overview of variable scope in Arduino programming, detailing local, global, and static variables. It includes multiple-choice questions (MCQs) to test understanding of these concepts and introduces conditional statements such as if, if-else, and switch-case. Additionally, it presents exercises for practicing basic and intermediate programming problems related to conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views47 pages

Class 04 - REC-Basic Robotics

The document provides an overview of variable scope in Arduino programming, detailing local, global, and static variables. It includes multiple-choice questions (MCQs) to test understanding of these concepts and introduces conditional statements such as if, if-else, and switch-case. Additionally, it presents exercises for practicing basic and intermediate programming problems related to conditional logic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Basic 𝐑𝐨𝐛𝐨𝐭𝐢𝐜𝐬

– Robotics Engineering Certification

Let’s Start the Robotics Journey


Recap
Variable scope refers to the accessibility and lifetime of a variable within a program.
In Arduino, variables can have different scopes depending on where they are
declared.

1. Local Variables:
○ Declared inside a function or a block {}.
○ Accessible only within the function/block where they are defined.
○ Destroyed once the function execution is completed.
2. Global Variables:
○ Declared outside all functions, typically at the beginning of the sketch.
○ Accessible throughout the entire program, including all functions.
○ Retain their values for the entire duration of the program execution.

3. Static Variables: (self study)

4. Extern Variables: (self study)


MCQ!
1. Where should a global variable be declared in an Arduino sketch?
a) Inside the loop() function
b) Inside the setup() function
c) Outside all functions
d) Inside a user-defined function
MCQ!
1. Where should a global variable be declared in an Arduino sketch?
a) Inside the loop() function
b) Inside the setup() function
c) Outside all functions
d) Inside a user-defined function

Answer: c) Outside all functions


MCQ!
2. What will be the output of the following Arduino code?
int x = 10;
void setup() {
Serial.begin(9600);
int x = 20;
Serial.println(x);
}
void loop() {}

a) 10
b) 20
c) 0
d) Compilation error
MCQ!
2. What will be the output of the following Arduino code?
int x = 10;
void setup() {
Serial.begin(9600);
int x = 20;
Serial.println(x);
}
void loop() {}

a) 10
b) 20
c) 0
d) Compilation error

Answer: b) 20
MCQ!
3. Which of the following statements is true about global variables in
Arduino?
a) They are accessible only within the function in which they are declared.
b) They retain their values even after exiting a function.
c) They are automatically deleted after the function ends.
d) They must be declared inside the loop() function.
MCQ!
3. Which of the following statements is true about global variables in
Arduino?
a) They are accessible only within the function in which they are declared.
b) They retain their values even after exiting a function.
c) They are automatically deleted after the function ends.
d) They must be declared inside the loop() function.

Answer: b) They retain their values even after exiting a function.


MCQ!
4. What will be the output of the following Arduino code?
int x;
void setup() {
Serial.begin(9600);
Serial.println(x);
}
void loop(){}

a) 10
b) 20
c) 0
d) Compilation error
MCQ!
4. What will be the output of the following Arduino code?
int x;
void setup() {
Serial.begin(9600);
Serial.println(x);
}
void loop(){}

a) 10
b) 20
c) 0
d) Compilation error

Answer: c) 0
MCQ!
5. What will be the output of the following Arduino code?
void setup() {
Serial.begin(9600);
int x = 10;
{
int x = 20;
Serial.println(x);
}
Serial.println(x);
}
void loop() { }

a) 20, 10
b) 10, 20
c) 20, 20
d) Compilation error
MCQ!
5. What will be the output of the following Arduino code?
void setup() {
Serial.begin(9600);
int x = 10;
{
int x = 20;
Serial.println(x);
}
Serial.println(x);
}
void loop() { }

a) 20, 10
b) 10, 20
c) 20, 20
d) Compilation error

Answer: a) 20, 10
MCQ!
6. What will be the output of the following Arduino code?
int x = 5;
void setup() {
Serial.begin(9600);
int x = 10;
Serial.println(x);
}
void loop() {
Serial.println(x);
}

a) 10 (once), then 5 continuously


b) 5 (once), then 10 continuously
c) 10 continuously
d) Compilation error
MCQ!
6. What will be the output of the following Arduino code?
int x = 5;
void setup() {
Serial.begin(9600);
int x = 10;
Serial.println(x);
}
void loop() {
Serial.println(x);
}

a) 10 (once), then 5 continuously


b) 5 (once), then 10 continuously
c) 10 continuously
d) Compilation error

Answer: a) 10 (once), then 5 continuously


Recap

Summary of Input Methods


MCQ!
7. What will be the output of the following Arduino code?
int buttonPin = 7;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
}

a) 0
b) 1
c) Either 0 or 1 depending on button press
d) Compilation error
MCQ!
7. What will be the output of the following Arduino code?
int buttonPin = 7;
void setup() {
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
}

a) 0
b) 1
c) Either 0 or 1 depending on button press
d) Compilation error

Answer: c) Either 0 or 1 depending on button press


MCQ!
8. What type of values does analogRead(pin) return in Arduino?

a) 0 or 1
b) 8-bit values (0-255)
c) 10-bit values (0-1023)
d) 16-bit values (0-65535)
MCQ!
8. What type of values does analogRead(pin) return in Arduino?

a) 0 or 1
b) 8-bit values (0-255)
c) 10-bit values (0-1023)
d) 16-bit values (0-65535)

Answer: c) 10-bit values (0-1023)


MCQ!
9. What will be the output of the following Arduino code if a potentiometer is connected to A0 and
turned halfway?
void setup() {
Serial.begin(9600);
}

void loop() {
int value = analogRead(A0);
Serial.println(value);
delay(1000);
}

a) 0
b) 512
c) 1023
d) Random values
MCQ!
9. What will be the output of the following Arduino code if a potentiometer is connected to A0 and
turned halfway?
void setup() {
Serial.begin(9600);
}

void loop() {
int value = analogRead(A0);
Serial.println(value);
delay(1000);
}

a) 0
b) 512
c) 1023
d) Random values

Answer: b) 512
MCQ!
10. What will happen if a floating (unconnected) input pin is read using
digitalRead()?

a) It always returns HIGH


b) It always returns LOW
c) It can return either HIGH or LOW randomly
d) It causes a runtime error
MCQ!
10. What will happen if a floating (unconnected) input pin is read using
digitalRead()?

a) It always returns HIGH


b) It always returns LOW
c) It can return either HIGH or LOW randomly
d) It causes a runtime error

Answer: c) It can return either HIGH or LOW randomly


Problem 1:
Our former mother of the nation is spending her time without doing anything productive. After a few
months, she has started to learn Arduino programming since she realized she must do some
productive work.

At one point, she has designed a circuit to take input from a variable resistor and wanted to convert
the value of that resistor into equivalent voltage using an Arduino Uno.

Now, your responsibility is to assist her by doing the following tasks:

1. Help her to design the circuit using Tinkercad Simulation. - 5 points.


2. Take input from the resistor and print the value on the serial monitor. - 5 points
3. Convert the value of the resistor into its equivalent voltage value. - 10 points

Expected Output:
Problem 1: Solution
Class 04-

Arduino Programming- 3
Conditional Statement
Conditional Statement
Conditional statements in Arduino allow the program to make
decisions based on certain conditions. These conditions
evaluate to either true (1) or false (0) and determine which
block of code will be executed.
Conditional Statement
int resistor=A0;
void setup() {
Serial.begin(9600);
}

void loop() {
int value=analogRead(resistor);
Serial.print(value);Serial.print(" Ohm ");
Serial.print("is greater than 500 ohm");

Serial.println("");
}
Conditional Statement
int resistor=A0;
void setup() {
I want that this line should
Serial.begin(9600);
} be executed when the value
is larger than 500.
void loop() {
int value=analogRead(resistor);
Serial.print(value);Serial.print(" Ohm ");
Serial.print("is greater than 500 ohm");

Serial.println("");
}
Conditional Statement
int resistor=A0;
void setup() {
I want that this line should
Serial.begin(9600);
} be executed when the value
is larger than 500.
void loop() {
int value=analogRead(resistor);
Serial.print(value);Serial.print(" Ohm ");
CONDITION
Serial.print("is greater than 500 ohm");
I need to add a condition
Serial.println(""); BEFORE THIS
} STATEMENT
Conditional Statement
1. IF
2. IF-ELSE
3. Nested IF-ELSE
4. Switch Case
1. IF Statement:
The if statement checks a condition and executes the code inside the block only if the conditio
is true.

Syntax:
if (condition) {
// Code to execute if the condition is true
}

Example:
int sensorValue = analogRead(A0);

if (sensorValue > 500) {


Serial.print(sensorValue);
}
2. if-else Statement
The if-else statement provides an alternative path if the condition is false.

Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}

Example:
int buttonState = digitalRead(7);
if (buttonState == HIGH) {
Serial.println(“HIGH”); // Turn LED ON if button is pressed
} else {
Serial.println(“LOW”); // Turn LED OFF otherwise
}
3. Nested if-else Statement
This structure allows multiple conditions to be checked in sequence. The first true condition is executed.

Syntax:
if (condition1) {
// Code executed if condition1 is true
} else if (condition2) {
// Code executed if condition1 is false, but condition2 is true
} else {
// Code executed if all conditions are false
}
Example:
int temperature = 30;

if (temperature > 35) {


Serial.println("Too hot! Turn on fan.");
} else if (temperature > 20) {
Serial.println("Comfortable temperature.");
} else {
Serial.println("Too cold! Turn on heater.");
}
4. switch-case Statement
The switch-case statement is used when checking a variable against multiple
fixed values.

Syntax:
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
default:
// Code to execute if no case matches
}
4. switch-case Statement
Example:
int mode = 2;

switch (mode) {
case 1:
Serial.println("Mode 1: LED ON");
case 2:
Serial.println("Mode 2: LED OFF");
default:
Serial.println("Invalid mode");
}
Using Multiple Conditions in if
Statement
In Arduino, you can check multiple conditions in a if statement using logical
operators:

● && (AND) → Both conditions must be true


● || (OR) → At least one condition must be true
● ! (NOT) → Reverses the condition (true becomes false, and vice versa)
Using Multiple Conditions in if Statement
Example 1: Using AND (&&)
int temp = 25;
int humidity = 60;

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

if (temp > 20 && humidity > 50) { // Both conditions must be true
Serial.println("Comfortable environment.");
}
}

void loop() { }

✅ Output: "Comfortable environment." (because both conditions are met)


Using Multiple Conditions in if Statement
Example 2: Using OR (||)
int lightLevel = 300;
int motionDetected = 1; // 1 means motion detected

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

if (lightLevel < 500 || motionDetected == 1) { // At least one condition mus


be true
Serial.println("Turn ON the light.");
}
}

void loop() { }

✅ Output: "Turn ON the light." (because motion is detected)


Using Multiple Conditions in if Statement
Example 3: Using NOT (!)
int buttonState = LOW;

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

if (!buttonState) { // Equivalent to if (buttonState == LOW)


Serial.println("Button is NOT pressed.");
}
}

void loop() { }

✅ Output: "Button is NOT pressed."


Using Multiple Conditions in if Statement
Example 4: Combining AND & OR
int temp = 35;
int humidity = 30;
int fanOn = 0;

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

if ((temp > 30 && humidity < 40) || fanOn == 1) {


Serial.println("Turn on cooling system.");
}
}

void loop() { }

✅ Output: "Turn on cooling system." (because temp > 30 && humidity < 40 is true)
QnA
Exercise
Basic If-Else Problems
1. Odd or Even Number
○ Write a program that takes an integer input and checks whether it is odd or even.
2. Positive, Negative, or Zero
○ Take an integer input and determine whether it is positive, negative, or zero.
3. Largest of Two Numbers
○ Take two numbers as input and print the largest one.
4. Largest of Three Numbers
○ Take three numbers as input and find the largest among them.
5. Leap Year Checker
○ Write a program that takes a year as input and determines whether it is a leap year or not.
6. Vowel or Consonant
○ Take a character as input and check whether it is a vowel (a, e, i, o, u) or consonant.
7. Divisibility Check
○ Write a program to check if a number is divisible by both 3 and 5.
8. Grading System
○ Take marks as input and print the corresponding grade:
■ 90+ → A
■ 80-89 → B
■ 70-79 → C
■ 60-69 → D
■ Below 60 → Fail
9. Evenly Divisible
○ Take two numbers as input and check if the first number is completely divisible by the second number.
10. Absolute Value
● Take an integer input and print its absolute value.
Intermediate If-Else Problems
11. Minimum and Maximum Among Four Numbers
● Take four numbers and find the minimum and maximum values.
12. Triangle Validity
● Take three side lengths as input and determine if they can form a valid triangle.
13. Day of the Week (Number to Name)
● Take a number (1-7) and print the corresponding day of the week (1 → Sunday, 2 → Monday, etc.).
14. Number Classification
● Take a number as input and check if it is positive, negative, or zero using a nested if-else.
15. BMI Calculator
● Take weight (kg) and height (m) as input, calculate BMI, and classify it as:
○ Below 18.5 → Underweight
○ 18.5 - 24.9 → Normal
○ 25 - 29.9 → Overweight
○ 30+ → Obese
Switch-Case Based Problems

16. Basic Calculator


● Take two numbers and an operator (+, -, *, /) as input and perform the calculation using switch-case.
17. Month Name from Number
● Take a number (1-12) as input and print the corresponding month name using switch-case.
18. Number of Days in a Month
● Take a month number (1-12) as input and print the number of days in that month.
19. Grade Evaluation (Using Switch)
● Take a grade (A, B, C, D, F) as input and print its description using switch-case:
○ A → Excellent
○ B → Good
○ C → Average
○ D → Below Average
○ F → Fail
20. Convert Number to Word (0-9)
● Take a single-digit number (0-9) and print its word representation using switch-case (e.g., 1 → "One", 2 → "Two").

Bonus Challenges

● Implement a Rock, Paper, Scissors game using if-else.


● Create a simple login system with a predefined username and password.
● Check if a character is uppercase, lowercase, digit, or special character.

You might also like