Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Table of Contents

1.Objective: .................................................................................................................................................. 1
2. Introduction:............................................................................................................................................. 1
1. LED blinking: ...................................................................................................................................... 2
2. Delay Subroutine............................................................................................................................... 2
3. Serial communication: ...................................................................................................................... 2
3.Procedures:................................................................................................................................................ 2
a) Procedure of the LED Blinking........................................................................................................... 2
b) Procedure for the Delay subroutine. ................................................................................................ 2
c) Procedure for the serial Monitor. ..................................................................................................... 2
4.Code: .......................................................................................................................................................... 3
I. Code for the LED blinking with Boolean function and Random Delay.............................................. 3
II. Code for the serial communication using random delay .................................................................. 3
III. Code for the Random delay from the serial monitor.................................................................... 4
5.Results: ...................................................................................................................................................... 5
1. Random Delay ................................................................................................................................... 5
2. Random Delay with showing the Timestamps.................................................................................. 6
3. Difference Between the time shown and generating random delay ............................................... 6
4. Serial Monitor delay using random numbers from the input ........................................................... 7
6.Discussions: ............................................................................................................................................... 7

1.Objective:
The objective of this report is to get familiarity with the basics of the Arduino programming and the
structure of the programming in the Arduino.

2. Introduction:
This report is based on the understanding of the basic codes of the Arduino Mega. These concepts are
including but not limited to the blinking of LED, delay subroutine and serial communication.
1. LED blinking:
The LED blinking is done with the various methods, these are the simple delay creations, where the
code gets a lot lengthy, afterwards the code efficiency is increased via Boolean functions of
inequality.

2. Delay Subroutine:

The delay subroutine is made using different algorithms for the creation of the required delays in
seconds in milliseconds.

3. Serial communication:
There is mentioned a bit in detailed about the numerous method for having the serial
communication, where a light has been shed on the delay computation using the graph of the serial
monitor.

3.Procedures:
As we have seen there are various things, that have been discussed in the report, let’s have a look at
these things a bit in detail, given below.

a) Procedure of the LED Blinking.


The procedure for having the blinking of the LED is s under.

1. Opening IDE Arduino


2. Selecting the Arduino Mega from the Board in Tool.
3. Write the Code in the Arduino.
4. Verify the Code or press Ctrl + R
5. Load the code to the Arduino or press Ctrl + U

b) Procedure for the Delay subroutine.

The overall procedure for the delay subroutine is as under.


1. Opening IDE Arduino
2. Selecting the Arduino Mega from the Board in Tool.
3. Write the Code in the Arduino.
4. Verify the Code or press Ctrl + R
5. Load the code to the Arduino or press Ctrl + U
6. For monitoring the delay at the serial plotter, either open it from tool then serial
plotter or press Ctrl+ Shift + L.
7. For Checking the delay at the serial monitor, then either open the serial monitor or
press the Ctrl + Shift + M.

c) Procedure for the serial Monitor.


1. Opening IDE Arduino
2. Selecting the Arduino Mega from the Board in Tool.
3. Write the Code in the Arduino.
4. Verify the Code or press Ctrl + R
5. Load the code to the Arduino or press Ctrl + U
6. Open the serial Monitor from the section of the Tool or press Ctrl + shift + M.

4.Code:
I. Code for the LED blinking with Boolean function and Random Delay

// Select the pin number of the LED to control


const int ledPin = LED_BUILTIN;
// This will hold the current state of the LED
bool ledState = false;

void setup() {
// Configure the LED Pin for OUTPUT
pinMode(ledPin, OUTPUT);
randomSeed(analogRead(0));
}

void loop() {
// Toggle the LED State
ToggleLED(ledPin);
// Wait the delay time until the next loop
delay(random(10000, 20000));
}

void ToggleLED(int pinNumber) {


// Toggle the state of the LED in memory
ledState = !ledState;
// Write the new value for the LED to the pin
digitalWrite(ledPin, ledState);
}

II. Code for the serial communication using random delay

// Select the pin number of the LED to control


const int ledPin = LED_BUILTIN;
// This will hold the current state of the LED
bool ledState = false;

void setup() {
// Initialize a Serial Interface at 9600 baud
Serial.begin(9600);
// Wait for the interface to fully initialize
while(!Serial) {
;
}
}

void loop() {
// Toggle the LED State
ToggleLED(ledPin);
// Determine the delay time
int delayTime = random(200, 2000);
// Print the delay time to the Serial Monitor
Serial.print("Delay Time: ");
Serial.println(delayTime);
// Wait the delay time until the next loop
delay(delayTime);
}

void ToggleLED(int pinNumber) {


// Toggle the state of the LED in memory
ledState = !ledState;
// Write the new value for the LED to the pin
digitalWrite(ledPin, ledState);
}

III. Code for the Random delay from the serial monitor

// Select the pin number of the LED to control


const int ledPin = LED_BUILTIN;
// This will hold the current state of the LED
bool ledState = false;

void setup() {
// Initialize a Serial Interface at 9600 baud
Serial.begin(9600);
// Wait for the interface to fully initialize
while(!Serial) {
;
}
}

void loop() {
// Toggle the LED State
ToggleLED(ledPin);
// Wait the delay time until the next loop
int delayTime=1000;
delay(delayTime);
// Check if serial data is available
if (Serial.available() > 0) {
// If it is, read until a new line char is received, then convert to Int
delayTime = Serial.readString().toInt();6
// Confirm new data received by printing it back to the Serial Monitor
Serial.print("Received new delay time: ");
Serial.println(delayTime);
}
}

void ToggleLED(int pinNumber) {


// Toggle the state of the LED in memory
ledState = !ledState;
// Write the new value for the LED to the pin
digitalWrite(ledPin, ledState);

5.Results:
1. Random Delay
2. Random Delay with showing the Timestamps

3. Difference Between the time shown and generating random delay


4. Serial Monitor delay using random numbers from the input

6.Discussions:

From the first graph we can see that the delay is of the random numbers, and these number are
generated using the Random (Min, Max) command. The delay coming from this command is then shown
on the serial monitor.
In the second Graph we can see that the delay is likewise, but shown it with the timestamp, which is
clearly shown in the serial monitor.

In the third graph we can see that there exist a difference between the delay come from the random
numbers and the delay shown from the timestamps, this delay is drawn with the help of the serial
plotter.

In the final graph we can see that the delay is generated but based on the input. Written on the serial
monitor, this delay is varying with respect to the input number one enters.

You might also like