Arduino Practice Problems PDF
Arduino Practice Problems PDF
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
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(){
}
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(){
}
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);
}
d.
void setup(){
Serial.begin(9600);
int sensor_pin = A0;
int final_reading;
void loop(){
}
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;
}
}