0
#include <Servo.h>        // Include Servo Library
#include <NewPing.h>      // Include Newping Library
#include<SoftwareSerial.h>
#include <dht.h>    //Include the DHT library for the temperature - humidity sensor


#define dht_apin A0         //Connect Signal pin from DHT11 sensor to analog pin A0
#define TRIGGER_PIN A1     // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A2     // Arduino pin tied to echo pin on the ultrasonic sensor.
#define SMOKE_PIN A3        //Connect Signal pin from DHT11 sensor to analog pin A0
#define FLAME_PIN A4        //Connect Signal pin from DHT11 sensor to analog pin A0
#define MAX_DISTANCE 250    // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.



dht DHT;            //Create a DHT object
Servo servo_motor;  // Servo's name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


// L298N Control Pins
// pin decalaration for 4 motors
const int m1a = 22;         //RIGHT F
const int m1b = 23;         //RIGHT F
const int m2a = 24;         //LEFT F
const int m2b = 25;         //LEFT F
const int m3a = 26;         //RIGHT B
const int m3b = 27;         //RIGHT B
const int m4a = 28;         //LEFT B
const int m4b = 29;         //LEFT B

const int buzzer = 6;


//boolean goesForward = false;
int distance = 100;
int smoke_thres = 400;

void setup()
{
  
  // Set L298N Control Pins as Output
  pinMode(m1a, OUTPUT);
  pinMode(m1b, OUTPUT);
  pinMode(m2a, OUTPUT);
  pinMode(m2b, OUTPUT);
  pinMode(m3a, OUTPUT);
  pinMode(m3b, OUTPUT);
  pinMode(m4a, OUTPUT);
  pinMode(m4b, OUTPUT);
  pinMode(TRIGGER_PIN,OUTPUT);
  pinMode(ECHO_PIN,INPUT);
  pinMode(SMOKE_PIN,INPUT);
  pinMode(FLAME_PIN,INPUT);
  




  Serial.begin(9600);                               //Begin serial communication
  delay(500);
  
  
  servo_motor.attach(10);   // Attachs the servo on pin 9 to servo object.
  servo_motor.write(115);   // Set at 115 degrees. 
  delay(2000);              // Wait for 2s.
  distance = readPing();    // Get Ping Distance.
  delay(100);               // Wait for 100ms.
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop()
{  
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
  Serial.println("Sensed Values:");
  int tdht = DHT.temperature;   //Get temperature from sensor
  Serial.println("Temperature = ");
  Serial.print(tdht);
  Serial.print("degree celcius.");

  int mqSensor = analogRead(SMOKE_PIN);
  Serial.println("Smoke = ");
  Serial.print(mqSensor);


**// when I add this if-else part of code I get error
//and please tell me how can I improve my code I'm newbie**

  
'''
    if(mqSensor >= smoke_thres){
        tone(buzzer,100,500);
      }
     else{
       noTone(buzzer);
     }
'''      

  int sensorvalue = analogRead(FLAME_PIN);
  Serial.println("Flame = ");
  Serial.print(sensorvalue);
  

  if (distance <= 20)
  {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distanceRight >= distanceLeft)
    {
      turnRight();
      delay(300);
      moveStop();
    }
    else
    {
      turnLeft();
      delay(300);
      moveStop();
    }
  
  }
  else
  {
    
    moveForward(); 
    
  }

    distance = readPing();
}

int lookRight()     // Look Right Function for Servo Motor
{  
  servo_motor.write(50);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int lookLeft()      // Look Left Function for Servo Motor 
{
  servo_motor.write(180);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(115);
  return distance;
}

int readPing()      // Read Ping Function for Ultrasonic Sensor.
{
  delay(100);                 // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  //int cm = sonar.ping_cm();   //Send ping, get ping distance in centimeters (cm).
  float cm = accurateDist();    // get accurate distance
  if (cm==0)                    
  {
    cm=25;
  }
  return cm;
}

void moveStop()       // Move Stop Function for Motor Driver.
{
  digitalWrite(m1a, LOW);
  digitalWrite(m1b, LOW);
  digitalWrite(m2a, LOW);
  digitalWrite(m2b, LOW);
  digitalWrite(m3a, LOW);
  digitalWrite(m3b, LOW);
  digitalWrite(m4a, LOW);
  digitalWrite(m4b, LOW);
  
}

void moveForward()    // Move Forward Function for Motor Driver.
{
  digitalWrite(m1a, HIGH);
  digitalWrite(m1b, LOW);
  digitalWrite(m2a, HIGH);
  digitalWrite(m2b, LOW);
  digitalWrite(m3a, HIGH);
  digitalWrite(m3b, LOW);
  digitalWrite(m4a, HIGH);
  digitalWrite(m4b, LOW);
}

void moveBackward()   // Move Backward Function for Motor Driver.
{
  digitalWrite(m1a, LOW);
  digitalWrite(m1b, HIGH);
  digitalWrite(m2a, LOW);
  digitalWrite(m2b, HIGH);
  digitalWrite(m3a, LOW);
  digitalWrite(m3b, HIGH);
  digitalWrite(m4a, LOW);
  digitalWrite(m4b, HIGH);
}

void turnRight()      // Turn Right Function for Motor Driver.
{
  digitalWrite(m1a, LOW);
  digitalWrite(m1b, HIGH);
  digitalWrite(m2a, HIGH);
  digitalWrite(m2b, LOW);
  digitalWrite(m3a, HIGH);
  digitalWrite(m3b, LOW);
  digitalWrite(m4a, HIGH);
  digitalWrite(m4b, LOW);
}

void turnLeft()       // Turn Left Function for Motor Driver.
{
  digitalWrite(m1a, HIGH);
  digitalWrite(m1b, LOW);
  digitalWrite(m2a, LOW);
  digitalWrite(m2b, HIGH);
  digitalWrite(m3a, HIGH);
  digitalWrite(m3b, LOW);
  digitalWrite(m4a, HIGH);
  digitalWrite(m4b, LOW);
}
float accurateDist()
{
 DHT.read11(dht_apin);           //Read the data from the DHT sensor
 float p = 101000;               //Set atmospheric pressure to 101.000 kPa
 float temp = DHT.temperature;   //Get temperature from sensor  
 float hum = DHT.humidity;  //Get humidity from sensor
 
 float a0 = 331.5024;
 float a1 = 0.603055;
 float a2 = -0.000528;
 float a3 = 51.471935;
 float a4 = 0.1495874;
 float a5 = -0.000782;
 float a6 = -1.82e-7;       
 float a7 = 3.73e-8;         
 float a8 = -2.93e-10;     
 float a9 = -85.20931;
 float a10 = -0.228525;
 float a11 = 5.91e-5;  
 float a12 = -2.835149;
 float a13 = -2.15e-13; 
 float a14 = 29.179762;
 float a15 = 0.000486;
 float T = temp + 273.15;
 float h = hum /100.0;
 float f = 1.00062 + 0.0000000314 * p + 0.00000056 * temp * temp;
 float Psv = exp(0.000012811805 * T * T - 0.019509874 * T + 34.04926034 - 6353.6311 / T);
 float Xw = h * f * Psv / p;
 float c = 331.45 - a0 - p * a6 - a13 * p * p;
 c = sqrt(a9 * a9 + 4 * a14 * c);
 float Xc = ((-1) * a9 - c) / ( 2 * a14);
 float speedOfSound = a0 + a1 * temp + a2 * temp * temp + (a3 + a4 * temp + a5 * temp * temp) * Xw + (a6 + a7 * temp + a8 * temp * temp) * p + (a9 + a10 * temp + a11 * temp * temp) * Xc + a12 * Xw * Xw + a13 * p * p + a14 * Xc * Xc + a15 * Xw * p * Xc;
 //Send a short (10 microseconds) ultrasonic burst 
 digitalWrite(TRIGGER_PIN, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIGGER_PIN, LOW);
 float microseconds = pulseIn(ECHO_PIN, HIGH, 100000); //Mesure the duration of a HIGH pulse in echo pin in microseconds. Timeout in 0,1 seconds
 float seconds = microseconds / 1000000;               //Convert microseconds to seconds
 float meters = seconds * speedOfSound;                //Get the distance in meters using the speed of sound calculated earlier
 float cm = meters * 100;                              //Convert meters to cm
 cm = cm/2;                                            //We only want the distance to the obstacle and not the roundtrip
 //Write results to serial monitor
 Serial.print("Obstacle distance = ");
 Serial.print(cm);
 Serial.print("cm  ");
 Serial.print("Current humidity = ");
 Serial.print(hum);
 Serial.print("%  ");
 Serial.print("temperature = ");
 Serial.print(temp); 
 Serial.print("C  ");
 Serial.print("Speed of Sound = ");
 Serial.print(speedOfSound);
 Serial.println("m/sec   ");
 //Wait 2 seconds before accessing sensor again. 
 delay(5000);
 return cm;
}
8
  • you should add the error message to question, (not the announcement you used as ttitle). but I guess you have a timer conflict between servo and tone – Juraj Sep 16 '20 at 17:26
  • @Juraj please tell me how do I solve it. And please help me rectify any error you can see in my code. – Prince Jha Sep 16 '20 at 20:32
  • You didn't define the buzzer pin before using it. – hcheung Sep 17 '20 at 0:14
  • 1
    add the true error message to question – Juraj Sep 17 '20 at 5:01
  • 2
    Does this answer your question? Running a servo and tone() won't compile? "multiple definition of `__vector_32'"? – Juraj Sep 17 '20 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.