MAES Midterm Assignment 2
MAES Midterm Assignment 2
Faculty of Engineering
Course Name: Microprocessor and Embedded Systems Course Code: EEE 4103
Semester: Fall 24-25 Term: Mid
Total Marks: 20 Submission Date: Exam Day
Instructor Name: Md Sajid Hossain Assignment: 02
Student Information:
Student Name: Mir Shoaib Ahmed Student ID: 19-41772-3
2. To meet the conditions specified in Q1, at least 1 of the 8-bit timers needs to be used. Prepare the necessary
registers associated with an 8-bit timer to achieve the time count computed in Q1. Consider the timer to be
running in normal mode. [5]
Page 3 of 7
[Hint: TCCRxA and TCCRxB have to be initialized. The useful bits of TCCRxA are bit 0 and bit 1 which
represent WGMx0 and WGMx1 respectively. Bits 0 to 3 are the useful bits from TCCRxB. Bits 3 represent
WGMx2. Bits 0 to 2 represent the clock select functions CSx0, CSx1, CSx2 respectively. Consider WGM = 0
for normal mode of operation. The bits not mentioned here can be ignored/considered as 0]
CSx2 CSx1 CSx0 Prescaler
0 1 0 8
0 1 1 64
1 0 0 256
1 1 1 1024
Solution:
8bit timer so Timer0 TCCR0A TCCR0B
3. A vending machine at a university campus will offer three different options for cold drinks. This machine
will be able to dispense a maximum of 3 drinks at once. The machine will have a total of three buttons
(button 1 for 1 drink, button 2 for 2 drinks, and button 3 for 3 drinks) to select the number of drinks to be
dispensed. After this initial selection, the vending machine will wait for 3s and then display the options for
available drinks. The consumer can use the 3 buttons (button 1 for A drink, button 2 for B drink, and button 3
for C drink) to select their desired drink(s). If only 1 drink is to be dispensed, the consumer will press the
relevant button and the vending machine will dispense the chosen drink after a 3 s wait. If more than 1 drink
is to be dispensed, the relevant buttons need to be pressed one after another and the machine will dispense the
drinks one at a time with a 3 s gap between the consecutive drinks. This vending machine is to be built
around an Arduino Uno platform. Now, prepare a program to execute the mentioned tasks if the Arduino
Uno system is operating at 16 MHz. [For this program, digital I/O pins D0 to D9 can be used whose pin
numbers are 0 to 9 correspondingly.] [5]
Page 4 of 7
Solution:
#define BTN_DRINKS_1 0 // Button for 1 drink
#define BTN_DRINKS_2 1 // Button for 2 drinks
#define BTN_DRINKS_3 2 // Button for 3 drinks
void setup() {
// Configure buttons as inputs
pinMode(BTN_DRINKS_1, INPUT_PULLUP);
pinMode(BTN_DRINKS_2, INPUT_PULLUP);
pinMode(BTN_DRINKS_3, INPUT_PULLUP);
pinMode(BTN_A, INPUT_PULLUP);
pinMode(BTN_B, INPUT_PULLUP);
pinMode(BTN_C, INPUT_PULLUP);
void loop() {
// Step 1: Wait for selection of the number of drinks
int drinksToDispense = selectDrinks();
if (drinksToDispense > 0) {
Serial.print("Number of drinks selected: ");
Serial.println(drinksToDispense);
int selectDrinks() {
// Check buttons for number of drinks selection
if (digitalRead(BTN_DRINKS_1) == LOW) {
return 1;
} else if (digitalRead(BTN_DRINKS_2) == LOW) {
return 2;
} else if (digitalRead(BTN_DRINKS_3) == LOW) {
return 3;
}
return 0; // No selection made
}
Page 5 of 7
void dispenseDrinks(int count) {
for (int i = 0; i < count; i++) {
int drinkSelected = selectDrink();
int selectDrink() {
// Wait for a valid drink selection
while (true) {
if (digitalRead(BTN_A) == LOW) {
return 1; // Drink A selected
} else if (digitalRead(BTN_B) == LOW) {
return 2; // Drink B selected
} else if (digitalRead(BTN_C) == LOW) {
return 3; // Drink C selected
}
}
}
4. Prepare a flowchart for the program in Q3 to show the flow of logic. [5]
Solution:
Page 6 of 7
Page 7 of 7