0% found this document useful (0 votes)
19 views

Assignment 2 - SE1804

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

Assignment 2 - SE1804

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

#include <LiquidCrystal.

h>

// Initialize the LCD and set up the pins


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

const unsigned int TRIG_PIN = 4; // Changed TRIG_PIN to 4


const unsigned int ECHO_PIN = 5; // Changed ECHO_PIN to 5
const unsigned int tempPin = A0;
const int buttonPin = 2; // Changed buttonPin to 2
volatile int mode = 0;

void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
Serial.println(mode);

// Attach interrupt to the button pin


attachInterrupt(digitalPinToInterrupt(buttonPin), changeMode, FALLING);
}

void loop() {
if (mode == true) {
// Temperature mode
int tempReading = analogRead(tempPin);
float voltage = tempReading * (5.0 / 1023.0);
float temperatureC = (voltage - 0.5) * 100; // Convert to temperature in Celsius
lcd.clear();
lcd.print("Temperature: ");
lcd.setCursor(0, 1);
lcd.print(temperatureC);
lcd.print(" C");
} else {
// Distance mode
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

unsigned long duration = pulseIn(ECHO_PIN, HIGH);


int distance = duration / 29 / 2;

lcd.clear();
if (duration == 0) {
lcd.print("Warning: no pulse");
} else {
lcd.print("Distance: ");
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" cm");
}
}

delay(500); // Add a delay to allow the LCD to update


}

// ISR to change mode


void changeMode() {
mode = (mode + 1) % 2;
}

You might also like