2

I'm building a device that uses two ultrasonic sensors to detect objects and provide audio feedback in the form of beeps that get faster the closer an object is. I have two speakers, one for each sensor, but the tone command will only do one set of beeps at a time. I tried to get around this with the Polytone Library and polytone command, but when I go to compile, I get an "exit status 1. Error compiling for board Arduino Uno" message. What am I doing wrong? Is there another way to get both sensors beeping at the same time? Please help!

I'm very new at coding. I tried to put the polytone code in a couple different ways, which I can show if that helps. There's also gyroscope code in there, but I have that working pretty well, so I guess just ignore that.

    #include <digitalWriteFast.h>
#include <Polytone.h>


#include <Wire.h> //library allows communication with I2C / TWI devices
#include <math.h> //library includes mathematical functions


const int MPU=0x68; //I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; //16-bit integers
int AcXcal,AcYcal,AcZcal,GyXcal,GyYcal,GyZcal,tcal; //calibration variables
double t,tx,tf,pitch,roll;

const int anPin1 = 0;
const int anPin2 = 1;
int triggerPin1 = 13;


long distance1, distance2;

//Code Part 2: Level LEDS
// Level LEDs
int levelLED_level = 11;
int levelLED_rollpos = 9;
int levelLED_rollneg = 8;
int levelLED_pitchpos = 12;
int levelLED_pitchneg = 10;
int LED_temp = 7;
Polytone poly;
void setup(){
    Serial.begin(9600);  // sets the serial port to 9600
    pinMode(triggerPin1, OUTPUT);

    Wire.begin(); //initiate wire library and I2C
    Wire.beginTransmission(MPU); //begin transmission to I2C slave device
    Wire.write(0x6B); // PWR_MGMT_1 register
    Wire.write(0); // set to zero (wakes up the MPU-6050)  
    Wire.endTransmission(true); //ends transmission to I2C slave device
    Serial.begin(9600); //serial communication at 9600 bauds
   

    pinMode(levelLED_rollneg, OUTPUT);
    pinMode(levelLED_rollpos, OUTPUT);
    pinMode(levelLED_pitchpos, OUTPUT);
    pinMode(levelLED_pitchneg, OUTPUT);
    pinMode(levelLED_level, OUTPUT);
}

void start_sensor(){
  digitalWrite(triggerPin1,HIGH);
  delay(1);
  digitalWrite(triggerPin1,LOW);
}

void read_sensors(){
  /*
  Scale factor is (Vcc/512) per inch. A 5V supply yields ~9.8mV/in
  Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
  */
  distance1 = analogRead(anPin1)*1.35;
  distance2 = analogRead(anPin2)*2.092;
}

void print_all(){
  Serial.print("MB1000");
  Serial.print(" ");
  Serial.print(distance1);
  Serial.print("mm");
  Serial.print(" ");
  Serial.print(" ");
  Serial.print("MB1260");
  Serial.print(" ");
  Serial.print(distance2);
  Serial.print("mm");
  Serial.println();
}

//Front sensor ranges
void midiSlowFront() {
  poly.begin(); // Set up polling timer interrupt
  poly.setPins(4,5); // Connect pins 4-5 to positive terminal of speaker/piezo

  poly.tone(262);
  delay(2000.0);
  poly.end();
}
void midiFront() {
   poly.begin(); // Set up polling timer interrupt
   poly.setPins(4,5); // Connect pins 4-5 to positive terminal of speaker/piezo
   poly.tone(262);
   delay(500.0);
   poly.end();
}
void midiFastFront() {
   poly.begin(); // Set up polling timer interrupt
   poly.setPins(4,5); // Connect pins 4-5 to positive terminal of speaker/piezo
   poly.tone(262);
   delay(150.0);
   poly.end();
}

//Side sensor ranges
void midiSlowSide() {
   poly.begin(); // Set up polling timer interrupt
   poly.setPins(4,5); // Connect pins 4-5 to positive terminal of speaker/piezo
   poly.tone(440);
   delay(2000.0);
   poly.end();
}
void midiSide() {
   poly.begin(); // Set up polling timer interrupt
   poly.setPins(4,5); // Connect pins 4-5 to positive terminal of speaker/piezo
   poly.tone(440);
   delay(500.0);
   poly.end();
}
void midiFastSide() {
   poly.begin(); // Set up polling timer interrupt
   poly.setPins(4,5); // Connect pins 4-5 to positive terminal of speaker/piezo
   poly.tone(440);
   delay(150.0);
   poly.end();
}

void loop(){
    Wire.beginTransmission(MPU); //begin transmission to I2C slave device
    Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false); //restarts transmission to I2C slave device
    Wire.requestFrom(MPU,14,true); //request 14 registers in total  

    start_sensor();
    read_sensors();
    print_all();

    //Acceleration data correction
    AcXcal = -950;
    AcYcal = -300;
    AcZcal = 0;

    //Temperature correction
    tcal = -1600;

    //Gyro correction
    GyXcal = 480;
    GyYcal = 170;
    GyZcal = 210;


    //read accelerometer data
    AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) 0x3C (ACCEL_XOUT_L)  
    AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) 0x3E (ACCEL_YOUT_L)
    AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) 0x40 (ACCEL_ZOUT_L)
 
    //read temperature data
    Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) 0x42 (TEMP_OUT_L)
 
    //read gyroscope data
    GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) 0x44 (GYRO_XOUT_L)
    GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) 0x46 (GYRO_YOUT_L)
    GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) 0x48 (GYRO_ZOUT_L)

    //temperature calculation
    tx = Tmp + tcal;
    t = tx/340 + 36.53; //equation for temperature in degrees C from datasheet
    tf = (t * 9/5) + 32; //fahrenheit

    //get pitch/roll
    getAngle(AcX,AcY,AcZ);
 
    //printing values to serial port
    Serial.print("Angle: ");
    Serial.print("Pitch = "); Serial.print(pitch);
    Serial.print(" Roll = "); Serial.println(roll);

    Serial.print("Temperature in celsius = "); Serial.print(t);  
    Serial.print(" fahrenheit = "); Serial.println(tf);  

    if ((roll < 7.00) && (roll > -7.00) && (pitch > -7.00) && (pitch < 7.00)){
    digitalWrite(levelLED_rollpos, LOW);
    digitalWrite(levelLED_rollneg, LOW);
    digitalWrite(levelLED_level, HIGH);
    digitalWrite(levelLED_pitchpos, LOW);
    digitalWrite(levelLED_pitchneg, LOW);
    }
   
    if (roll > 7.01) {
    // Turn on Pos Roll LED
    digitalWrite(levelLED_rollpos, HIGH);
    digitalWrite(levelLED_rollneg, LOW);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, LOW);
    digitalWrite(levelLED_pitchneg, LOW);
    }
   
    if (roll < -7.01) {
    //Turn on Neg Roll LED
    digitalWrite(levelLED_rollpos, LOW);
    digitalWrite(levelLED_rollneg, HIGH);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, LOW);
    digitalWrite(levelLED_pitchneg, LOW);
    }
   
    if (pitch < -7.01) {
    //Turn on Neg Pitch
    digitalWrite(levelLED_rollpos, LOW);
    digitalWrite(levelLED_rollneg, LOW);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, LOW);
    digitalWrite(levelLED_pitchneg, HIGH);
    }
   
    if (pitch > 7.01) {
    //Turn on Pos Pitch
    digitalWrite(levelLED_rollpos, LOW);
    digitalWrite(levelLED_rollneg, LOW);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, HIGH);
    digitalWrite(levelLED_pitchneg, LOW);
    }

    if ((pitch > 7.01) && (roll > 7.01)){
    digitalWrite(levelLED_rollpos, HIGH);
    digitalWrite(levelLED_rollneg, LOW);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, HIGH);
    digitalWrite(levelLED_pitchneg, LOW);
    }

    if ((roll > 7.01) && (pitch < -7.01)){
     digitalWrite(levelLED_rollpos, HIGH);
    digitalWrite(levelLED_rollneg, LOW);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, LOW);
    digitalWrite(levelLED_pitchneg, HIGH);
    }

    if ((roll < -7.01) && (pitch < -7.01)){
     digitalWrite(levelLED_rollpos, LOW);
    digitalWrite(levelLED_rollneg, HIGH);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, LOW);
    digitalWrite(levelLED_pitchneg, HIGH);
    }

    if ((roll < -7.01) && (pitch > 7.01)){
     digitalWrite(levelLED_rollpos, LOW);
    digitalWrite(levelLED_rollneg, HIGH);
    digitalWrite(levelLED_level, LOW);
    digitalWrite(levelLED_pitchpos, HIGH);
    digitalWrite(levelLED_pitchneg, LOW);
    }

    if (tf > 72.00){
      digitalWrite(LED_temp, HIGH);
    }

    if (tf < 72.00){
      digitalWrite(LED_temp, LOW);
    }

    if((distance1 > 24)){
    midiSlowFront();
    }
    if((distance1 < 24) && (distance1 > 12)){
    midiFront();
    }
    if(distance1 < 12){
    midiFastFront();
    }
    if(distance2 > 24){
    midiSlowSide();
    }
    if((distance2 < 24) && (distance2 > 12)){
    midiSide();
    }
    if(distance2 < 12){
    midiFastSide();
    }
  delay(150);
}


//function to convert accelerometer values into pitch and roll
void getAngle(int Ax,int Ay,int Az) {
    double x = Ax;
    double y = Ay;
    double z = Az;

    pitch = atan(x/sqrt((y*y) + (z*z))); //pitch calculation
    roll = atan(y/sqrt((x*x) + (z*z))); //roll calculation

    //converting radians into degrees
    pitch = pitch * (180.0/3.14);
    roll = roll * (180.0/3.14) ;
}

EDIT: Here's the stuff before the error message, my bad!



In file included from C:\Users\Documents\Arduino\polytonetest\polytonetest.ino:1:0:

C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src/digitalWriteFast.h:25:0: warning: "digitalPinToTimer" redefined

 #define digitalPinToTimer(P) \

 

In file included from sketch\polytonetest.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:180:0: note: this is the location of the previous definition

 #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )

 

In file included from C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src\Polytone.h:11:0,

                 from C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src\Polytone.cpp:7:

C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src\digitalWriteFast.h:25:0: warning: "digitalPinToTimer" redefined

 #define digitalPinToTimer(P) \

 

In file included from C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src\Polytone.h:10:0,

                 from C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src\Polytone.cpp:7:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:180:0: note: this is the location of the previous definition

 #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )

 

libraries\arduino-polytone-master\Polytone.cpp.o (symbol from plugin): In function `Polytone::begin()':

(.text+0x0): multiple definition of `t'

sketch\polytonetest.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here

C:\Users\Documents\Arduino\libraries\arduino-polytone-master\src\Polytone.cpp:18:15: warning: type of 't' does not match original declaration [-Wlto-type-mismatch]

 unsigned long t = 0;

               ^

C:\Users\Documents\Arduino\polytonetest\polytonetest.ino:12:8: note: type 'double' should match type 'long unsigned int'

 double t,tx,tf,pitch,roll;

        ^

C:\Users\Documents\Arduino\polytonetest\polytonetest.ino:12:8: note: 't' was previously declared here

C:\Users\Documents\Arduino\polytonetest\polytonetest.ino:12:8: note: code may be misoptimized unless -fno-strict-aliasing is used

collect2.exe: error: ld returned 1 exit status
2
  • 1
    All that text that comes before the "exit status 1" tells you what is wrong. Without that we know less than you do. – Majenko Apr 7 at 9:47
  • Ohhh, I see! Sorry about that! I will edit the post! – Kaytee Apr 7 at 16:49

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.