Engineering Technology, XMUT 2021

Lab 5

days Due 17 May (extended to 20 May), 19:00 (Xiamen Time)

ALERT! Note: This Lab is individual. You must not work in groups.

ALERT! You can either use a real Arduino or Tinkercad simulator to test your codes.

For this Lab, we need to use two functions: setup() and loop(). You must use the following template for all of your sketch programs .
    void setup() {

    }
    void loop() {
        
    } 

Resources and links:

To Submit:

  • task1.ino
  • task2.ino
  • task3.ino
  • task4.ino
  • task5.ino
  • task6.ino
  • task7.ino
ALERT! Make sure your files have these names.

Core (Easy)

PICK Task 1: Create a new sketch (task1.ino) and Write an Arduino program to
  1. start with an integer at 0,
  2. print the integer number to the Serial Monitor,
  3. increment the integer.

It should continue printing and incrementing the integer forever.

task1.png

PICK Task 2: Create a new sketch (task2.ino) and copy the code from Task 1. Then modify the Arduino program that prints integers that count up from 1.

The program should not print the multiples of 5 (5, 10, 15, 20, etc.), and it should print the word "Jump" instead.

task2Solution.jpg

HELP Hint: you could add some kind of delay so that you can actually read the output.

PICK Task 3: Create a new sketch (task3.ino) and write an Arduino program that
  1. counts down from 30,
  2. printing each integer along the way.

Once the program reaches 0, it should print 0 and then stop counting.

task3.png

Completion (Hard)

PICK Task 4: Create a new sketch (task4.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”.

task4Solution.jpg

PICK Task 5: Create a new sketch (task5.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, blink faster and faster until it appears to be constantly on before restarting the process again.

PICK Task 6: Create a new sketch (task6.ino) copy the following lines to the top (creating 2 global variables):

const int len = 10;
int a[len] = {0, 2, -4, 12, -52, 42, -96, 7, -23, 99};

In setup(), compute the average of the a[] array and print it to the Serial Monitor.

In this case, the average of a[] is -1.3. Without modifying your code, you should be able to change the global variables to

const int len = 8;
int a[len] = {125, 1920, 503, 299, 67, 13578, 7632, 22043};

and get an average of 5770.8750.

HELP Hint: You can use Serial.println(float_variable, 4); to print four decimal places.

HELP Hint: This video helps you to solve this task.

Challenge (the hardest)

PICK Task 7: Create a sketch (task7.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 would also roll the dice.

task7Solution.jpg

This means the program could give me a number between 1 and 6.

task7Solution1.jpg

HELP Hint: This video helps you to solve this task.