Go Down

Topic: Expression cannot be used as a function (Read 2946 times) previous topic - next topic

Eduardo_Veiga

Jun 09, 2016, 05:34 pm Last Edit: Jun 09, 2016, 07:02 pm by Eduardo_Veiga
Can you help me? I am getting this error: expression cannot be used as a function



Code: [Select]
//#include <AFMotor.h> //import your motor shield library
#define trigPin 4 // define the pins of your sensor
#define echoPin 5
//AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
//AF_DCMotor motor2(2, MOTOR12_8KHZ);
#define E1 10  // Ativa o Pino para o motor 1 - direita
#define E2 11  // Ativa o Pino para o motor 2 - esquerda

#define I1 8  // Controla o pino 1 para o motor 1
#define I2 9  // Control pin 2 for motor 1
#define I3 12  // Control pin 1 for motor 2
#define I4 13  // Control pin 2 for motor 2

#include <Servo.h> //include Servo library
Servo panMotor;
int leftDistance, rightDistance; //distances on either side
void setup() {
 panMotor.attach(6); //Servo, mudar pino
 panMotor.write(90); //set PING))) pan to center

 
 //Serial.begin(9600); // begin serial communitication  
 //Serial.println("Motor test!");
  pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
 //motor1.setSpeed(105); //set the speed of the motors, between 0-255
 //motor2.setSpeed (105);  
   pinMode(E1, OUTPUT);
   pinMode(E2, OUTPUT);

   pinMode(I1, OUTPUT);
   pinMode(I2, OUTPUT);
   pinMode(I3, OUTPUT);
   pinMode(I4, OUTPUT);
}

void loop() {

  long duration, distance; // start the scan
 digitalWrite(trigPin, LOW);  
 delayMicroseconds(2); // delays are required for a succesful sensor operation.
 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10); //this delay is required as well!
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;// convert the distance to centimeters.
 if (distance > 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {  
 // Serial.println ("Close Obstacle detected!" );
//Serial.println ("Obstacle Details:");
//Serial.print ("Distance From Robot is " );
//Serial.print ( distance);
//Serial.print ( " CM!");// print out the distance in centimeters.

//Serial.println (" The obstacle is declared a threat due to close distance. ");
//Serial.println (" Turning !");
   //motor1.run(FORWARD);  // Turn as long as there's an obstacle ahead.
   //motor2.run (BACKWARD);
   digitalWrite(E1, HIGH);
   digitalWrite(E2, HIGH);

   digitalWrite(I1, LOW);
   digitalWrite(I2, HIGH);
   digitalWrite(I3, LOW);
   digitalWrite(I4, HIGH);
}
 else {
  //Serial.println ("No obstacle detected. going forward");
  delay (15);
  //motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!
  // motor2.run(FORWARD);  
  digitalWrite(E1, LOW);
   digitalWrite(E2, LOW);

   digitalWrite(I1, LOW);
   digitalWrite(I2, HIGH);
   digitalWrite(I3, LOW);
   digitalWrite(I4, HIGH);
   panMotor.write(0);
   delay(500);
   rightDistance = trigPin(); //scan to the right ----------------------The error is here
   delay(500);
   panMotor.write(180);
   delay(700);
   leftDistance = trigPin(); //scan to the left-----------------And here
   delay(500);
   panMotor.write(90); //return to center
   delay(100);
   compareDistance();
 }  
}
void compareDistance()
{
 if (leftDistance>rightDistance)
 {
   digitalWrite(E1, HIGH);
   digitalWrite(E2, LOW);

   digitalWrite(I1, LOW);
   digitalWrite(I2, HIGH);
   digitalWrite(I3, LOW);
   digitalWrite(I4, HIGH);
 }
 else if (rightDistance>leftDistance)
 {
   digitalWrite(E1, LOW);
   digitalWrite(E2, HIGH);

   digitalWrite(I1, LOW);
   digitalWrite(I2, HIGH);
   digitalWrite(I3, LOW);
   digitalWrite(I4, HIGH);
 }
}



ieee488

1. First. Post using code tags </>
2. The IDE will tell you where the error is as in which line #.  We're not going to guess at this!


KeithRB

I don't know what trigPin is supposed to be, but it is defined as a naked 4, so after the preprocessor is done with it it becomes leftDistance = 4();

So the error message is spot on.

pert

Code: [Select]

#define trigPin 4 // define the pins of your sensor
#define echoPin 5

I'm guessing this means you're using an ultrasonic sensor to detect distance. Your code shows you have no understanding of how to use this sensor. You need to first use a simple sketch to become familiar with how to get a reading from the sensor before writing code for that sensor in your actual project. There are a number of libraries you can use with these sensors and they should all come with example sketches. I think this is the most popular one: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home, assuming it supports your sensor and board. Use the example sketch to make sure your sensor is working correctly and then make sure you understand what every line of that sketch does.

Go Up