Engineering Technology, XMUT 2024
Lab 1
Due 17 March , 19 pm (Xiamen Time)
Note: You must present your projects to co-teachers or tutors in the lab.
You can either use a real Arduino or Tinkercad simulator to test your codes.
Resources and links:
To Submit:
-
task1.ino
-
task2.ino
-
task3.ino
-
task4.ino
-
task5.ino
Make sure your files have these names.
Core (Easy)
Task 1: Create a new sketch (
task1.ino
) and write an Arduino program that
- counts down from 30,
- prints each integer along the way,
Once the program reaches 0, it should print 0 and then stop counting.
The program should not print the multiples of 3 (3, 9, 12, 15, etc.), and it should print the word "Hop" instead.
Hint: you could add some kind of delay so that you can actually read the output.
Task 2: Create a new sketch (
task2.ino
) and copy the code below into the sketch. You need to find and fix the 2 compile-time errors (syntax errors) and 3 runtime errors (semantic errors) in the code below.
void setup() {
// Count to 10, but replace the number 5 with the word "Jump"
for ( i = 1; i >= 10; i++ ) {
if ( i = 5 ) {
Serial.println(Jump);
} else {
Serial.println(i);
}
}
}
void loop() {
}
When the program is fixed, it should count from 1 to 10 in the Serial Monitor, replacing the number 5 with the string “Jump”.
Completion (Hard)
Task 3: Create a new sketch (
task3.ino
) and copy the code below into the sketch. You need to find and fix the 4 compile-time errors (syntax errors) and 1 runtime error (semantic errors) in the code below.
int led = 13;
int ms = 200;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
// Make the LED flash faster and faster until it appears as
// a static light. Reset when the delay reaches 0 ms.
if ( ms > 0 ); {
digitalwrite(led, HIGH);
delay(ms);
digitalWrite(1ed, HIGH);
delay(ms);
ms = ms - 5
}
else{
ms = 200;
}
}
When the program is fixed, the onboard LED, connected to pin 13, blinks faster and faster until it appears to be constantly on before restarting the process again.
Task 4: Create a sketch (
task4.ino
) that will ask the user to input two numbers. You need to define a function
printNumbersBetween(int a, int b)
. Once the user inputs the numbers, the Arduino will call
printNumbersBetween(int a, int b)
and print all the numbers between those two numbers, including the two numbers themselves. (This project will show how to handle serial input, perform basic loops, and use user-defined functions.)
Challenge (the hardest)
Task 5: Create a sketch (
task5.ino
) that asks if a user wants to roll a dice. It then displays a random number between 1 and 6.
The program should repeat the process, asking if the user wants to roll the dice again.
The program should allow the user to enter "yes" or "no". The program should not be case-sensitive.
For example, if the user enters "Yes", the program would roll the dice. If the user enters "YES", the program will also roll the dice.
This means the program could give me a number between 1 and 6.