-1

I have written the following code for a project comprising an ultrasonic distance sensor, a buzzer and a few buttons. When the distance is less than 60 cm, the buzzer rings and stops only when the appropriate passcode is pressed.

When I try to compile it, I get:

exit status 1
Error compiling for board Arduino/Genuino Uno.

This does not give the reason for the compilation failure.

My code is below, any help would be appreciated.

#include<NewPing.h>  


int r;  
boolean ctr;

#define TRIGGER_PIN  10  // Arduino pin tied to trigger pin on the ultrasonic sensor.  
#define ECHO_PIN     9  // Arduino pin tied to echo pin on the ultrasonic sensor.  
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.  

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.  


//the buttons:  

const int SPEAKER=8;  

const int b1=4;  
const int b2=5;  
const int b3=6;  
const int b4=7;  
//variables to store previous button press and currend button press , used to eliminate switch bouncing  
boolean pb1=LOW;  
boolean cb1=LOW;  

boolean pb2=LOW;  
boolean cb2=LOW;  

boolean pb3=LOW;  
boolean cb3=LOW;  

boolean pb4=LOW;  
boolean cb4=LOW;  


//variables to store numbers:  

int w=0;  
int x=0;  
int y=0;  
int z=0;  



void setup() {  
  // put your setup code here, to run once:  
  pinMode(b1,INPUT);  
  pinMode(b2,INPUT);  
  pinMode(b3,INPUT);  
  pinMode(b4,INPUT);  

 Serial.begin(9600);   



}

boolean debounce1(boolean last1)  
{  
   boolean current1=digitalRead(b1);  
  if(last1 != current1)  
  {  
       delay(5);  
    }  
      current1=digitalRead(b1);  
      return current1;    
  }  

boolean debounce2(boolean last2)  
{  
   boolean current2=digitalRead(b2);  
  if(last2 != current2)  
  {  
      delay(5);  
    }  
      current2=digitalRead(b2);  
      return current2;    
  }  

  boolean debounce3(boolean last3)  
{  
    boolean current3=digitalRead(b3);  
  if(last3 != current3)  
  {  
      delay(5);  
    }  
      current3=digitalRead(b3);  
      return current3;    
  }  

  boolean debounce4(boolean last4)  
{  
   boolean current4=digitalRead(b4);  
  if(last4 != current4)  
  {  
      delay(5);  
    }  
      current4=digitalRead(b4);  
      return current4;    
  }  

boolean distance(int o)  
{  
  boolean control;  
  if (o<60)  
    control=true;  
    return control;  
  }  



void loop() {  
  // put your main code here, to run repeatedly:  
  cb1=debounce1(pb1);  
  cb2=debounce2(pb2);  
  cb3=debounce3(pb3);  
  cb4=debounce4(pb4);  

     if (pb1==LOW&&cb1==HIGH)     
   {  
      w++;  
    }  
    pb1=cb1;  


    if (pb2==LOW&&cb2==HIGH)     
   {  
      x++;  
    }  
    pb2=cb2;  


    if (pb3==LOW&&cb3==HIGH)     
   {
      y++;  
    }  
    pb3=cb3;  

    if (pb4==LOW&&cb4==HIGH)     
   {  
      z++;  
    }  
    pb4=cb4;  

    delay(50);  
    unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds   (uS).  

    Serial.print(uS / US_ROUNDTRIP_CM);  
    Serial.print(" ");  
     int r=uS / US_ROUNDTRIP_CM;  

     ctr=distance(r);  

  if(ctr==true)  
    tone(SPEAKER,1000);  

Serial.print(w);  
Serial.print(x);  
Serial.print(y);  
Serial.print(z);  


Serial.println();  

delay(100);  
if(w==1&&x==3&&y==3&&z==1)  
  noTone(SPEAKER);  

}  
0

There is a reason. There is always a reason. You may just have to scroll back in the output panel to see it.

Tone.cpp:536: multiple definition of `__vector_7'
NewPing/NewPing.cpp:284: first defined here
collect2: error: ld returned 1 exit status

In this case it is that you are using NewPing.h and tone(). Both of them use the same timer.

You can't use both tone() and the NewPing.h library together without first making one of them use a different timer or no timer at all. The easiest is the NewPing.h library. As it says in the header file:

#define TIMER_ENABLED true      // Set to "false" to disable the timer ISR (if getting "__vector_7" compile errors set this to false). Default=true

So edit that header file and set true to false for that line and it should start working.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.