0% found this document useful (0 votes)
20 views10 pages

Q1 Code: Setup

The document contains various Arduino code examples demonstrating basic programming concepts, including array manipulation, temperature control, and LED blinking. It also includes functions for logging humidity data and monitoring conditions in different systems. Each section provides code snippets along with expected outputs for clarity.

Uploaded by

Abdul Rasheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Q1 Code: Setup

The document contains various Arduino code examples demonstrating basic programming concepts, including array manipulation, temperature control, and LED blinking. It also includes functions for logging humidity data and monitoring conditions in different systems. Each section provides code snippets along with expected outputs for clarity.

Uploaded by

Abdul Rasheed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab 01

Q1

CODE

void setup() {
Serial.begin(9600); // Start serial communication for output

// Create an array with 12 integer values


int myArray[12] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120};

// Arrays are 0-indexed, so the 8th position is index 7


int valueAt8th = myArray[7];

// Print the value


Serial.print("Value at 8th position: ");
Serial.println(valueAt8th);
}

void loop() {
// Nothing to do here
}

OUTPUT

Value at 8th position: 80

Q2

CODE

int SensorValue = 30; // Example value of sensor temperature

// Pin Definitions
const int fanPin = 8; // Digital pin connected to fan control
const int heaterPin = 9; // Digital pin connected to heater control

void setup() {
pinMode(fanPin, OUTPUT);
pinMode(heaterPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
float temperatureC = SensorValue;

Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");

// Control logic
if (temperatureC > 25.0) {
digitalWrite(fanPin, HIGH); // Turn ON fan
digitalWrite(heaterPin, LOW); // Turn OFF heater
Serial.print("Fan turned ON");
}
else if (temperatureC < 10.0) {
digitalWrite(fanPin, LOW); // Turn OFF fan
digitalWrite(heaterPin, HIGH); // Turn ON heater
Serial.print("Heater turned ON");
}
else {
digitalWrite(fanPin, LOW); // Turn OFF both
digitalWrite(heaterPin, LOW);
}

delay(1000); // Wait for 1 second before next loop


}

OUTPUT

Temperature: 30 °C
Fan turned ON
Lab 02

CODE

Q1

void setup() {
Serial.begin(9600); // Start the Serial Monitor
}

void loop() {
for (int i = 1; i <= 99; i++) {
if (i % 3 == 0 && i % 5 == 0) {
Serial.println("Mechatronics");
}
else if (i % 3 == 0) {
Serial.println("Mechanical");
}
else if (i % 5 == 0) {
Serial.println("Electronics");
}
else {
Serial.println("-");
}
}

// Run the loop only once


while (true); // Infinite loop to stop after one run
}

OUTPUT

-
-
Mechanical
-
Electronics
Mechanical
-
-
Mechanical
Electronics
-
Mechanical
-
-
Mechatronics
- Mechanical
- -
Mechanical Electronics
- Mechanical
Electronics -
Mechanical -
- Mechanical
- Electronics
Mechanical -
Electronics Mechanical
- -
Mechanical -
- Mechatronics
- -
Mechatronics -
- Mechanical
- -
Mechanical Electronics
- Mechanical
Electronics -
Mechanical -
- Electronics
- -
Mechanical Mechanical
Electronics -
- -
Mechanical Mechatronics
- -
- -
Mechatronics Mechanical
- -
- Electronics
Mechanical Mechanical
- -
Electronics -
Mechanical Mechanical
- -
- -
Mechanical
Electronics
-
Mechanical
-
-
Mechatronics
Q2

CODE

void setup() {
Serial.begin(9600); // Start the Serial Monitor

// Example usage
calculateAndPrint(4, 5, 10); // (4 * 5) + 10 = 30
}

void loop() {
// Nothing here for now
}

// Custom function
void calculateAndPrint(int a, int b, int c) {
int result = (a * b) + c;
Serial.print("Result: ");
Serial.println(result);
}

OUTPUT

Result: 30

Q3

CODE

void setup() {
Serial.begin(9600);

int score = 75; // Example score (you can change this)

// Divide score by 10 to simplify switch cases


int category = score / 10;

switch (category) {
case 6:
Serial.println("Grade: B");
break;
case 7:
Serial.println("Grade: A");
break;
case 8:
Serial.println("Grade: A*");
break;
default:
Serial.println("Grade: Not in range (60–90)");
break;
}
}

void loop() {
// Nothing here
}

OUTPUT

Grade: A
Lab 03

Q1

CODE

void setup() {
// Example: Using pins 8 and 9 with a 500ms delay
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

// Call the function in setup (or you can call it in loop for continuous
blinking)
blinkTwoLEDs(8, 9, 500);
}

void loop() {
// Nothing here for now
}

// Function to blink two LEDs alternately with specified delay


void blinkTwoLEDs(int ledPin1, int ledPin2, int delayTime) {
digitalWrite(ledPin1, HIGH); // Turn on LED 1
digitalWrite(ledPin2, LOW); // Turn off LED 2
delay(delayTime);

digitalWrite(ledPin1, LOW); // Turn off LED 1


digitalWrite(ledPin2, HIGH); // Turn on LED 2
delay(delayTime);

// Optional: you can loop this function if continuous blinking is needed


}

Q2

CODE

void setup() {
int ledPin = 9; // Must be a PWM-capable pin (e.g., 3, 5, 6, 9, 10, or 11 on
most Arduino boards)
pinMode(ledPin, OUTPUT);
}

void loop() {
int ledPin = 9;
fadeLED(ledPin); // Call the fade function
}

// Function to fade LED in and out


void fadeLED(int pin) {
// Fade in
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(pin, brightness);
delay(10); // Controls fade speed
}

// Fade out
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(pin, brightness);
delay(10); // Controls fade speed
}
}
Lab 08

Q1

CODE

void loggingHumidity() {
DateTime now = rtc.now();
float humidity = dht.readHumidity();

if (isnan(humidity)) {
Serial.println("Failed to read humidity from DHT sensor!");
return;
}

// Format timestamp
char timestamp[32];
sprintf(timestamp, "%02d:%02d:%02d %02d/%02d/%04d",
now.hour(), now.minute(), now.second(),
now.day(), now.month(), now.year());

// Open file and write data


myFile = SD.open("HUMIDITY.txt", FILE_WRITE);
if (myFile) {
myFile.print(timestamp);
myFile.print(",");
myFile.println(humidity);
myFile.close();
} else {
Serial.println("Error opening HUMIDITY.txt");
}

// Also print to serial for debugging


Serial.print(timestamp);
Serial.print(", ");
Serial.print(humidity);
Serial.println(" %");
}
Q2

ANSWER

1. Rotating Equipment – Condition monitoring e.g. Vibrations data


2. Thermal Systems – Temperature monitoring e.g. Temperature data
3. Structures – Health monitoring e.g. Strain or displacement data
4. Automotive Testing – Brakes, Fuel, RPM monitoring
5. Robotics – Video storage
6. Renewable Energy – Long term environmental data

You might also like