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

Arduino Practice Problems PDF

The document contains practice problems for an Arduino course. It includes questions about variables, analog and digital I/O, user-defined functions, and the millis() function. The answers provided draw any required circuits and include code to demonstrate the concepts. For example, one question involves writing a function to read an analog sensor and return the value, which it demonstrates doing over 5 iterations.

Uploaded by

akshath
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)
124 views

Arduino Practice Problems PDF

The document contains practice problems for an Arduino course. It includes questions about variables, analog and digital I/O, user-defined functions, and the millis() function. The answers provided draw any required circuits and include code to demonstrate the concepts. For example, one question involves writing a function to read an analog sensor and return the value, which it demonstrates doing over 5 iterations.

Uploaded by

akshath
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/ 5

ME121 Portland State University - Spring 2015 Arduino Practice Problems

ME 121 – Arduino Practice problems

Objective: You should be able to answer by hand (with no computer or cheat sheet) these problems.

Note: To check your result, copy/paste your code and check if you get the answer expected!
ME121 Portland State University - Spring 2015 Arduino Practice Problems

1. Variables: declaration and use

What would be the final values of x, y, z in the codes below:


void setup(){ void setup(){
int x,y,z; int x,y,z;
x = 15; x = 15.0;
y = 10; y = 10.0;
z = x + y ; z = x + y ;
} }

void loop(){ void loop(){


} }

void setup(){ void setup(){


int x,y,z; int x,y,z;
x = 15.0; x = 15;
y = 10.0; y = 10;
} z = x / y ;
}
void loop(){
z = x + y ; void loop(){
} }

void setup(){ void setup(){


float x,y,z; int x,y ;
x = 15; float z;
y = 10; x = 15;
z = x / y ; y = 10;
} z = x / y ;
}
void loop(){ void loop(){
} }

2. Analog I/O
- Analog input: Draw a circuit to use a photoresistor. Then write a code that will read the values of
a photoresistor and print them on the Serial Monitor continuously.
- Analog output: Draw a circuit to use an LED. Then write a code that will turn the LED on with
high intensity for 1 second, then half intensity for 0.5 second, then off for 0.25 second, and
repeat indefinitely.

3. Digital I/O
- Digital output: Draw a circuit to use an LED. Then write a code that will turn the LED on, wait for
x milliseconds, then turn the LED off, wait for y milliseconds, and repeat indefinitely.
- Digital input: Draw a circuit to use a button to control an LED. Then write a code that will read
and print the value of the button. Then add an if/else statement to print “LED on” or “LED off”
on the serial monitor, depending on the value of the button.
ME121 Portland State University - Spring 2015 Arduino Practice Problems

4. User-defined functions
a. Write a user defined function that computes a simple computation, like a+bc (where a=2, b=4
and c=5). Return the result in the main code and print it once.
b. Same as above but a=2.5, b=4.2 and c=5.5.
c. Write a user defined function that reads the value of a sensor reading (plugged into pin A0) and
returns it to the main code. The value is updated on the serial monitor indefinitely.
d. Same as above, but instead of printing the result indefinitely, print it 5 times.

5. Millis()

Given two integers a and b, write a code that will start a timer and print a value of (a+b) for the first
second, then the value of (a-b) for the second second, then (a+b) for the third second, etc … to be
repeated indefinitely.
ME121 Portland State University - Spring 2015 Arduino Practice Problems

Answers:

4. a.

void setup(){
Serial.begin(9600);
int a=2, b=4, c=5;
int final_result;
final_result = simple_computation (a,b,c);
Serial.println(final_result);
}

void loop(){
}

int simple_computation (int d, int e, int f){


int result;
result = d+e*f;
return result;
}

b.

void setup(){
Serial.begin(9600);
float a=2.5, b=4.2, c=5.5;
float final_result;
final_result = simple_computation (a,b,c);
Serial.println(final_result);
}

void loop(){
}

float simple_computation (float d, float e, float f){


float result;
result = d+e*f;
return result;
}

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

void loop(){
int sensor_pin = A0;
int final_reading;
final_reading = reading_function (sensor_pin);
Serial.println(final_reading);
}

int reading_function (int pin){


int result;
result = analogRead(pin);
return result;
}
ME121 Portland State University - Spring 2015 Arduino Practice Problems

d.
void setup(){
Serial.begin(9600);
int sensor_pin = A0;
int final_reading;

for (int i=1;i<=5;i++){


final_reading = reading_function (sensor_pin);
Serial.println(final_reading);
}
}

void loop(){
}

int reading_function (int pin){


int result;
result = analogRead(pin);
return result;
}

5.

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

void loop(){
int a=5, b=12;
static long start_time=0;
long current_time = millis();

if (current_time-start_time<=1000){
Serial.print("a+b: "); Serial.println(a+b);
}
else if (current_time-start_time<=2000){
Serial.print(“a-b: "); Serial.println(a-b);
}
else{
start_time = current_time;
}
}

You might also like