Day 2: Arduino Mega
Debugging Material for Day 2 (Released on 3 July 2024)
This material is designed as supplementary to the Day 2 Slides. You should not use this material without first reading the slides and trying out by yourself. Instead, refer to this material only if you encounter issues or bugs while following the slides.
LED Task
Blink once in two seconds
This code is from Prof's slides. We implemented the LED connecting to GND and Pin 13 on Arduino to replicate the expected performace.
#define LED_PIN 13
void setup()
{
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
digitalWrite(LED_PIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
// turn the LED off by making the voltage LOW
digitalWrite(LED_PIN, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
Blink twice in a second
To make the LED blink twice in a second, you need to change the delay times to 250 milliseconds. This will ensure that the LED turns on and off twice within one second. Here's the modified code:

Blink short-short-long
To make the LED blink in a short-short-long pattern, you can define the short and long delay times and sequence the blinks accordingly. Here's how you can achieve this:
In this code:
A short blink is achieved with 250 milliseconds on and 250 milliseconds off.
A long blink is achieved with 1000 milliseconds on and 1000 milliseconds off.
A 1-second pause is added after the pattern to make it more distinguishable before it repeats.

Blink randomly
To make the LED blink randomly, you can use the random() function in Arduino to generate random delay times. Here's an example code:
In this code:
randomSeed(analogRead(0))initializes the random number generator with a seed value based on an analog read from pin 0, which helps to ensure the randomness each time the Arduino is reset.random(100, 1000)generates a random delay time between 100 and 1000 milliseconds for both the on and off states of the LED. You can adjust the range of the random values to fit your needs.

Serial Communication
we will explore serial communication with Arduino and how to improve the readability of the received data for our human users. We'll start with a basic code example and then discuss enhancements.
Basic Serial Communication Code
Here's the initial code for serial communication:
This code sets up serial communication at a baud rate of 9600 and prints "Hello World!" when the program starts. In the loop() function, it checks if there is any data available to read from the serial buffer. If there is, it reads the incoming byte and prints it in both decimal and hexadecimal formats.
Explanation and Issue
When using Serial.read(), the function reads incoming serial data as bytes. This means the data is represented as integer values, which might not be human-readable, especially when dealing with characters.
For example, if you send the character 'A' through the serial monitor, the program will output:
While 65 and 0x41 (hexadecimal representation) are correct according to the ASCII standard, they are not immediately intuitive. We often want to see the actual character being sent for better understanding and debugging.
Improving Readability
To make the data more human-readable, we can modify the code to print the character representation directly. Here is the improved version:
Enhanced Explanation
In this improved version, we added Serial.print((char)inByte) to print the character representation of the incoming byte. Now, when a character like 'A' is sent, the output will be more informative:
This output shows:
The decimal value
65The corresponding character
AThe hexadecimal value
0x41
Ultrasonic Sensor
Equivalent Code without Library

Equivalent Code with using Library
You should download and implement the library in Arduino IDE in prior:
Download NewPing.h from: https://www.arduino.cc/reference/en/libraries/newping/
Include the ZIP library in Arduino IDE: Sketch > Include Library > Add.ZIP Libraries

Here is the code in your hands out, after including the headfiles, the code can be compiled correctly.
DC Motor
LED Blinky

Additional Challenge
Write a program to control the brightness of LED using Serial Communication:
This way, the brightness will only be updated when a valid input starting with 'B' is received, avoiding unintentional changes when no new data is present.

Challenge Questions
Ultrasonic & Interrupt
Can you use an interrupt to turn on an LED based on the distance measured by the ultrasonic sensor?

Joystic

Last updated