0% found this document useful (0 votes)
9 views9 pages

code

The document contains an Arduino sketch for controlling four servos based on temperature and distance measurements. It uses a thermistor to monitor temperature and an ultrasonic sensor to measure distance, activating servos and an LED based on the readings. If the temperature exceeds 100°C, a buzzer is activated and the LED remains on.

Uploaded by

Arduino Basic
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)
9 views9 pages

code

The document contains an Arduino sketch for controlling four servos based on temperature and distance measurements. It uses a thermistor to monitor temperature and an ultrasonic sensor to measure distance, activating servos and an LED based on the readings. If the temperature exceeds 100°C, a buzzer is activated and the LED remains on.

Uploaded by

Arduino Basic
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/ 9

#include <Servo.

h>

Servo LF;

Servo RF;

Servo LH;

Servo RH;

int pos = 90;

const int trigPin = 3;

const int echoPin = 4;

long duration;

int distance;

int safetyDistance;

const int LedPin = 5;

int ThermistorPin = 0;

int Vo;

float R1 = 10000;

float logR2, R2, T;

float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;

const int Buzzer = 6;

void setup()

LF.attach(9);

RF.attach(8);

LH.attach(10);

RH.attach(11);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT);// Sets the echoPin as an Input

pinMode(LedPin, OUTPUT); //Sets the LedPin as an Output

pinMode(Buzzer, OUTPUT); //Sets the Buzzer as an Output

Serial.begin(9600); // Starts the serial communication

void loop() {

int pos = 90;

LF.write(pos);

RF.write(pos);

LH.write(pos);

RF.write(pos); //Initialized position for else condition

Vo = analogRead(ThermistorPin); //read data from thermistor

R2 = R1 * (1023.0 / (float)Vo - 1.0);

logR2 = log(R2);

T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));

T = T - 273.15;

Serial.print("Temperature: "); //in celcius

Serial.print(T);

if (T <= 100) //applied the temp condition for whole loop.This will check the temperature once for a
loop

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(30);

for (pos = 90; pos <= 130; pos += 1) //1

LF.write(pos);

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 90; pos >= 70; pos -= 1) //2

LH.write(pos);

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);


// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 90; pos >= 70; pos -= 1) //3

RH.write(pos);

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)


{

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 130; pos >= 90; pos -= 1) //4

LF.write(pos);

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 90; pos >= 50; pos -= 1) //5

{
RF.write(pos);

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 70; pos <= 90; pos += 1) //6

RH.write(pos);

delay(50);

// Clears the trigPin


digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 70; pos <= 90; pos += 1) //7

LH.write(pos);

delay(50);

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds

duration = pulseIn(echoPin, HIGH);

// Calculating the distance

distance= duration*0.034/2;

safetyDistance = distance;

if (safetyDistance >= 10)

digitalWrite(LedPin, LOW);

delay(150);

for (pos = 50; pos <= 90; pos += 1) //8

RF.write(pos);

delay(50);

else

digitalWrite(LedPin, HIGH);

else

digitalWrite(Buzzer, HIGH);

digitalWrite(LedPin, HIGH);

You might also like