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

Source code

Iot

Uploaded by

Anvitha m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Source code

Iot

Uploaded by

Anvitha m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Source Code

// Pin Definitions

const int potPin = A0; // Potentiometer connected to A0

const int switchPin = 13; // Switch connected to digital pin 2

const int irSensorPin = 3; // IR sensor connected to digital pin 3

const int trigPin = 4; // Ultrasonic sensor TRIG connected to digital pin 4

const int echoPin = 5; // Ultrasonic sensor ECHO connected to digital pin 5

// Variables

bool cruiseControl = false; // Cruise control toggle state

int cruiseSpeed = 50; // Cruise control speed in km/h

int potValue = 0; // Current potentiometer value

int speedValue = 0; // Current speed in km/h

int previousSpeed = 0; // Speed before obstacle detection

bool obstacleDetected = false; // IR sensor obstacle detection flag

long distance = 0; // Distance measured by ultrasonic sensor (in cm)

void setup() {

pinMode(switchPin, INPUT_PULLUP); // Use internal pull-up for switch

pinMode(irSensorPin, INPUT); // Set IR sensor pin as input

pinMode(trigPin, OUTPUT); // Set ultrasonic TRIG pin as output

pinMode(echoPin, INPUT); // Set ultrasonic ECHO pin as input

Serial.begin(9600); // Initialize serial communication

void loop() {

// Read switch state

int switchState = digitalRead(switchPin);

// Set cruise control based on switch state


cruiseControl = (switchState == LOW); // LOW means switch is ON

// Detect obstacle using IR sensor

obstacleDetected = digitalRead(irSensorPin) == LOW; // LOW means obstacle detected

// Measure distance using ultrasonic sensor

distance = getUltrasonicDistance();

// Determine speed based on sensors and controls

if (obstacleDetected || distance <= 20) {

// Stop the vehicle if an obstacle is detected or distance < 20 cm

previousSpeed = speedValue;

speedValue = 0;

} else {

if (cruiseControl) {

speedValue = cruiseSpeed; // Maintain cruise speed

} else {

potValue = analogRead(potPin); // Read potentiometer value (0-1023)

speedValue = map(potValue, 0, 1023, 0, 120); // Map to km/h range (0-120 km/h)

// Display information on Serial Monitor

Serial.println("=======================");

Serial.print("Cruise Control: ");

Serial.println(cruiseControl ? "ON" : "OFF");

Serial.print("Speed: ");

Serial.print(speedValue);

Serial.println(" km/h");

Serial.print("Distance to Object: ");


Serial.print(distance);

Serial.println(" cm");

if (obstacleDetected) {

Serial.println("Obstacle Detected by IR: STOP!");

if (distance <= 20) {

Serial.println("Obstacle Detected by Ultrasonic: STOP!");

Serial.println("=======================");

delay(1000); // Small delay for stability

// Function to measure distance using ultrasonic sensor

long getUltrasonicDistance() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH); // Measure time for echo

long distance = duration * 0.034 / 2; // Convert to distance in cm

return distance;

You might also like