未在此范围内声明“ microsecondsToInches”

时间:2019-06-24 13:25:37

标签: c++ arduino

我已经为4个超声波传感器编写了一个代码,它本身可以很好地工作,但是当我在主程序中编写它时,会出现此错误。

我试图在独立的草图中查看问题出在哪里,但我已将所有问题都写到了主代码中,所以应该不成问题

独立代码:

const int pingPin = 7;
const int pingPin2= 6;
const int pingPin3 = 5;
const int pingPin4 = 4;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin2, INPUT);
  duration = pulseIn(pingPin2, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);


  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin3, INPUT);
  duration = pulseIn(pingPin3, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin4, OUTPUT);
  digitalWrite(pingPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin4, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin4, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin4, INPUT);
  duration = pulseIn(pingPin4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

主要代码:

void Reverse (void);
void Left (void);
void Right (void);
void Stop (void);

volatile int irval1;                 // variable to store the values genetated by IR_SENSOR_MODULES attached to the AGV
volatile int irval2;
volatile int irval3;
volatile int irval4;
volatile int irval5;

// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 10;
const int pingPin2= 9;
const int pingPin3 = 8;
const int pingPin4 = 7;

int pos = 0;

#include <Servo.h>;
Servo myservo;
//lijn volg systeem


#define MotorSpeed 2                 //geef de pin(2, ,3 een naam(MotorSpeed, MotorDirection)
#define MotorDirection 3
#define MotorSpeed2 5                //geef de pin(2, ,3 een naam(MotorSpeed2, MotorDirection2)
#define MotorDirection2 6

#define ir1 A1                       //geef de pin(A1, A2, A3) een naam(ir1, ir2, ir3)
#define ir2 A2
#define ir3 A3
#define ir4 A4
#define ir5 A5

#define threshold 500               //threshold value to differenciate between black line and other part of the surface
                                    // Value threshold should be changed accordingly depending on the track.

#define buttons A6


void setup() { 
                                   // put your setup code here, to run once:
  myservo.attach(4);
  myservo.write(90);

  pinMode(Drive , OUTPUT);         // sets the value  on register so that GPIO acts as output pin
  pinMode(MotorSpeed, OUTPUT);
  pinMode(MotorDirection, OUTPUT);

  pinMode(ir1 , INPUT);            // set the value of the register so that the GPIO acts as input pin
  pinMode(ir2 , INPUT);
  pinMode(ir3 , INPUT);

  irval1 = analogRead(ir1 );       // reads the value and stores in volatile int Variable
  irval2 = analogRead(ir2 );
  irval3 = analogRead(ir3 );

  pinMode(MotorSpeed, OUTPUT);     //sets both to outputs
  pinMode(MotorDirection, OUTPUT);

  pinMode(buttons, INPUT);

  Serial.begin(9600);              //Sets the data rate in bits per second (baud) for serial data transmission
} 


void loop() {
                                   // put your main code here, to run repeatedly:
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin2, INPUT);
  duration = pulseIn(pingPin2, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);


  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin3, INPUT);
  duration = pulseIn(pingPin3, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin4, OUTPUT);
  digitalWrite(pingPin4, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin4, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin4, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin4, INPUT);
  duration = pulseIn(pingPin4, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, "); 
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(400);

  if (buttons, LOW) and if (pingPin && pingPin2 && pingPin3 && pingPin4, >150cm) {


    if ( ir1 > threshold && ir2 > threshold && ir3 < threshold && ir4 > threshold && ir5 > threshold)
    {
      void Drive (void);
    }
    else if ( ir1 > threshold && ir2 > threshold && ir3 > threshold && ir4 < threshold && ir5 > threshold)
    {
      void Right(void);
    }
    else if ( ir1 > threshold && ir2 < threshold && ir3 > threshold && ir4 > threshold && ir5 > threshold)
    {
      void Left (void);
    }
    else if ( ir1 < threshold && ir2 < threshold && ir3 < threshold && ir4 < threshold && ir5 < threshold)
    {
      void Reverse (void);
    }

    else if ( ir1 > threshold && ir2 > threshold && ir3 > threshold && ir4 > threshold && ir5 > threshold)
    {
      void Stop (void);
    }
  }

  else (MotorSpeed, 0);
}


void Drive() {
  digitalWrite(MotorDirection, HIGH);
  for (int motorValue = 0 ; motorValue <= 255; motorValue += 10) {
    digitalWrite(MotorSpeed, motorValue);
    delay(100);
  }
  digitalWrite(MotorSpeed, 255);
}

void Right() {
  digitalWrite(MotorSpeed, 255);

  for (pos = 90; pos <= 0; pos += 10) {
    myservo.write(pos);
    delay(100);
  }
}

void Left() {
  digitalWrite(MotorSpeed, 255);

  for (pos = 90; pos <= 180; pos += 10) {
    myservo.write(pos);
    delay(100);
  }
}

void Reverse () {
  if (MotorSpeed == 255) {
    for (int motorValue = 255 ; motorValue >= 0; motorValue -= 10) {
      digitalWrite(MotorSpeed, motorValue);
      delay(100);
    }
    delay(3000);
    for (int motorValue = 0; motorValue <= -255; motorValue += 10) {
      digitalWrite(MotorSpeed, motorValue);
      delay(100);
    }
  }
  else {
    for (int motorValue = 0; motorValue <= -255; motorValue += 10) {
      digitalWrite(MotorSpeed, motorValue);
      delay(100);
    }
  }
}

void Stop() {
  digitalWrite(MotorSpeed, 0);
}

0 个答案:

没有答案