error on my sketch

I fixed up the stray, and formatted it.

long sensors_average;
int sensors_sum;
int position;
long sensors[] = {
  0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
int proportional;
int integral;
int derivative;
int last_proportional;
int Kp;
int Ki;
int Kd;
int max_speed;
int left_speed;
int right_speed;
int error_value;
int threshold;
int readings;

void setup()
{ 

}

void loop()
{
  sensors_read(); //Reads sensor values and computes sensor sum and weighted average
  pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
  calc_turn(); //Computes the error to be corrected
  //motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
}

void pid_calc()
{ 
  position = int(sensors_average / sensors_sum);
  proportional = position - set_point; // Replace set_point by your set point
  integral = integral + proportional;
  derivative = proportional - last_proportional;
  last_proportional = proportional;
  error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void sensors_read()
{ 
  sensors_average = 0;
  sensors_sum = 0;
  for (int i = 0; i < 5; i++)
  {
    sensors[i] = analogRead(i);
    // Readings less than threshold are filtered out for noise
    if (sensors[i] < threshold)
      sensors[i] = 0;
    sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
    //readings
    sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
  }
}

void calc_turn()
{ //Restricting the error value between +256.
  if (error_value < -256)
  {
    error_value = -256;
  }
  if (error_value > 256)
  {
    error_value = 256;
  }
  // If error_value is less than zero calculate right turn speed values
  if (error_value < 0)
  {
    right_speed = max_speed + error_value;
    left_speed = max_speed;
  }
  // If error_value is greater than zero calculate left turn values
  else
  {
    right_speed = max_speed;
    left_speed = max_speed - error_value;
  }
}

I still get the error message about "set_point" but as the comment says, you are supposed to put your set point there. I don't know what that is.

int set_point;
is missing from the declarations

Nick, I tried a few things and couldn't find the stray \

Where was it?

thans Nick and crossroads
@liudr thats whay im ask to this topic :frowning:

oke now i want to try on my bot :smiley: thanks

Nick, I tried a few things and couldn't find the stray \

Where was it?

Here, although it's hard to see:

proportional = position – set_point; // Replace set_point by your set point

That ain't no minus sign. It's hex 0x96. Minus is 0x2D.

Fixed version:

proportional = position - set_point; // Replace set_point by your set point

See the difference?

cant see sir
i dont know looks the code is same

now i enable driver motor and got the prob

// map motor poles to Arduino pins
#define motor1pole1 2
#define motor1pole2 3
#define motor2pole1 7
#define motor2pole2 8

// map L293d motor enable pins to Arduino pins
#define enablePin1 9
#define enablePin2 10

#define M1_MAX_SPEED 100
#define M2_MAX_SPEED 100

#define motordelay 30
#define debugmotorsec 3000

long sensors_average;
int sensors_sum;
int position;
long sensors[] = {
  0, 0, 0, 0, 0}; // Array used to store 5 readings for 5
int proportional;
int integral;
int derivative;
int last_proportional;
int Kp;
int Ki;
int Kd;
int max_speed;
int left_speed;
int right_speed;
int error_value;
int threshold;
int readings;
int set_point;
int motor_drive;
void setup()
{ 
  // set mapped L293D motor1 and motor 2 enable pins on Arduino to output (to turn on/off motor1 and motor2 via L293D)
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);

  // set mapped motor poles to Arduino pins (via L293D)
  pinMode( motor1pole1 , OUTPUT);
  pinMode( motor1pole2, OUTPUT);
  pinMode( motor2pole1, OUTPUT);
  pinMode( motor2pole2 , OUTPUT);
}

void loop()
{
  sensors_read(); //Reads sensor values and computes sensor sum and weighted average
  pid_calc(); //Calculates position[set point] and computes Kp,Ki and Kd
  calc_turn(); //Computes the error to be corrected
  motor_drive(right_speed, left_speed); //Sends PWM signals to the motors
}

void pid_calc()
{ 
  position = int(sensors_average / sensors_sum);
  proportional = position - set_point; // Replace set_point by your set point
  integral = integral + proportional;
  derivative = proportional - last_proportional;
  last_proportional = proportional;
  error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
}

void sensors_read()
{ 
  sensors_average = 0;
  sensors_sum = 0;
  for (int i = 0; i < 5; i++)
  {
    sensors[i] = analogRead(i);
    // Readings less than threshold are filtered out for noise
    if (sensors[i] < threshold)
      sensors[i] = 0;
    sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean of the sensor
    //readings
    sensors_sum += int(sensors[i]); //Calculating sum of sensor readings
  }
}

void calc_turn()
{ //Restricting the error value between +256.
  if (error_value < -256)
  {
    error_value = -256;
  }
  if (error_value > 256)
  {
    error_value = 256;
  }
  // If error_value is less than zero calculate right turn speed values
  if (error_value < 0)
  {
    right_speed = max_speed + error_value;
    left_speed = max_speed;
  }
  // If error_value is greater than zero calculate left turn values
  else
  {
    right_speed = max_speed;
    left_speed = max_speed - error_value;
  }
}

sketch_feb13a.ino: In function 'void loop()':
sketch_feb13a:55: error: 'motor_drive' cannot be used as a function

int motor_drive;

Because you declared it as an "int" variable.

i dont know looks the code is same

Not to the compiler it doesn't.
If ever I see the error message "stray ''...", I can put a pretty certain bet that the code has been copied from a website.

xulingxuling23:
i dont know looks the code is same

Look closely:

See the different length of the minus sign?

@nick
gosh its diffrent thx for help me nick

@awol
so i must use double not int?

No. You declared motor_drive as an int then tried to use it as a function

int motor_drive;
 motor_drive(right_speed, left_speed); //Sends PWM signals to the motors

Declaring it as another type will not solve the problem.
Should there be a function in your code that causes both motors to run ?

[quote author=Nick Gammon link=topic=148656.msg1116888#msg1116888 date=1360739615]

See the difference?

Umm no.

The other option is to paste the code into an editor that allows a hex view of the source, like UltraEdit.

That's exactly where I got the hex codes from.

I get suspicious when a line says something about a stray "" when I don't see any of them. Then I look at the punctuation in the line. As a last resort, I would retype the whole thing.

UKHeliBob:
No. You declared motor_drive as an int then tried to use it as a function

int motor_drive;
 motor_drive(right_speed, left_speed); //Sends PWM signals to the motors

Declaring it as another type will not solve the problem.
Should there be a function in your code that causes both motors to run ?

yeah i want both motors run sir and i make a functian and ^^ no errors thx sir

Nick,

That evil symbol! OP must have copied and pasted from some webpage maybe with different character coding.

OP,

I think frankly you should go through basic arduino and c tutorials. You don't seem to understand programming and yet want to do a project that needs some that understanding. Learn the basic first. We can help you write some code, correct mistakes, but you have to understand your (or what you found) code first.

You cannot have an int variable and a function with the same name.

I thought you could. The compiler should be able to figure it out.
But I wrote a short test program, and apparently, you can't.

And even if it was allowed, it's confusing. Don't give program variables
and functions the same name.

You cannot have an int variable and a function with the same name.

I thought you could. The compiler should be able to figure it out.

Why? They are both just symbols.

I'm sure that you've seen people posting here something like:

void myFun()
{
}

void loop()
{
   myFun;
}

wondering why muFun was not called. The compiler just saw a symbol that was not syntactically invalid. That's really no different from:

void loop()
{
   7;
}

Useless, but perfectly legal.

xulingxuling23:
yeah i want both motors run sir and i make a functian and ^^ no errors thx sir

Do you know how to make one motor run, either way will do to start with ?
You are probably using a motor shield (which one ?) Are there any example programs for it ?

UKHeliBob:

xulingxuling23:
yeah i want both motors run sir and i make a functian and ^^ no errors thx sir

Do you know how to make one motor run, either way will do to start with ?
You are probably using a motor shield (which one ?) Are there any example programs for it ?

#define motor1pole1 2
#define motor1pole2 3
#define motor2pole1 7
#define motor2pole2 8

// map L293d motor enable pins to Arduino pins
#define enablePin1 9
#define enablePin2 10

#define M1_MAX_SPEED 100
#define M2_MAX_SPEED 100

#define motordelay 30
#define debugmotorsec 3000

void setup() {
  Serial.begin(9600);

  // set mapped L293D motor1 and motor 2 enable pins on Arduino to output (to turn on/off motor1 and motor2 via L293D)
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);

  // set mapped motor poles to Arduino pins (via L293D)
  pinMode( motor1pole1 , OUTPUT);
  pinMode( motor1pole2, OUTPUT);
  pinMode( motor2pole1, OUTPUT);
  pinMode( motor2pole2 , OUTPUT);
  motorspeed(0, 0);
}

int mspeed = 100;  // pick a starting speed up to 255

void loop() {

// set speed of motor 1 and 2 to same speed
  motorspeed(mspeed, mspeed);

// spin motor 1 only in one direction
  Serial.print("MOTOR 1 FORWARD @ SPEED: "); 
  Serial.println(mspeed);
  motorforward(1);
  delay(debugmotorsec);
  motorstop(1);

  // spin motor 2 only in one direction
  Serial.print("MOTOR 2 FORWARD @ SPEED: "); 
  Serial.println(mspeed);
  motorforward(2);
  delay(debugmotorsec);
  motorstop(2);

  // spin motor 1 only in opposite direction
  Serial.print("MOTOR 1 BACK @ SPEED: "); 
  Serial.println(mspeed);
  motorback(1);
  delay(3000);
  motorstop(1);

// spin motor 2 only in opposite direction
  Serial.print("MOTOR 2 BACK @ SPEED: ");  
  Serial.println(mspeed);
  motorback(2);
  delay(debugmotorsec);
  motorstop(2);

// stop both motors 1 and 2
  Serial.println("BOTH MOTORS STOP FOR 2 SEC.");
  motorstop(1);
  motorstop(2);
  delay(2000);

// spin both motors in one direction
  Serial.print("BOTH MOTORS FORWARD @ SPEED: ");
  Serial.println(mspeed);
  motorforward(1);
  motorforward(2);
  delay(debugmotorsec);

// stop both motors
  Serial.println("BOTH MOTORS STOP FOR 2 SEC.");
  motorstop(1);
  motorstop(2);

  delay(2000);
// spin both motors in opposite direction
  Serial.print("BOTH MOTORS BACK @ SPEED: ");
  Serial.println(mspeed);
  motorback(1);
  motorback(2);
  delay(debugmotorsec);

// stop both motors
  Serial.println("BOTH MOTORS STOP FOR 2 SEC.");
  motorstop(1);
  motorstop(2);
  delay(2000);


// spin both motors but in opposite directions
  Serial.print("MOTOR1 FORWARD | MOTOR2 BACK @ SPEED: ");
  Serial.println(mspeed);
  motorforward(1);
  motorback(2);
  delay(debugmotorsec);

// stop both motors
  Serial.println("BOTH MOTORS STOP FOR 2 SEC.");
  motorstop(1);
  motorstop(2);
  delay(2000);

// spin both motors in the other opposite direction
  Serial.print("MOTOR1 BACK | MOTOR2 FORWARD @ SPEED: ");
  Serial.println(mspeed);
  motorback(1);
  motorforward(2);
  delay(debugmotorsec);

// stop both motors
  Serial.println("BOTH MOTORS STOP FOR 2 SEC.");
  motorstop(1);
  motorstop(2);
  delay(2000);

  mspeed += 50;  // add 50 to speed of motor spin. Max speed 255

// set speed of motor 1 and 2 to same new speed
  motorspeed(mspeed,mspeed);

}

// MOTOR FUNCTIONS

void motorstop(int motornum){
  delay(motordelay);
  if (motornum == 1) {
    digitalWrite(motor1pole1, LOW);
    digitalWrite(motor1pole2, LOW);
  }
  else if (motornum == 2) {

    digitalWrite(motor2pole1, LOW);
    digitalWrite(motor2pole2, LOW);
  }
  delay(motordelay);
}

void motorforward(int motornum){
  if (motornum == 1) {
    digitalWrite(motor1pole1, HIGH);
    digitalWrite(motor1pole2, LOW);
  }
  else if (motornum == 2) {

    digitalWrite(motor2pole1, LOW);
    digitalWrite(motor2pole2, HIGH);
  }
  delay(motordelay);
}

void motorback(int motornum){
  if (motornum == 1) {
    digitalWrite(motor1pole1, LOW);
    digitalWrite(motor1pole2, HIGH);
  }
  else if (motornum == 2) {
    digitalWrite(motor2pole1, HIGH);
    digitalWrite(motor2pole2, LOW);
  }
  delay(motordelay);
}

void motorspeed(int motor1speed, int motor2speed) {
  if (motor1speed > M1_MAX_SPEED ) motor1speed = M1_MAX_SPEED; // limit top speed
  if (motor2speed > M2_MAX_SPEED ) motor2speed = M2_MAX_SPEED; // limit top speed
  if (motor1speed < 0) motor1speed = 0; // keep motor above 0
  if (motor2speed < 0) motor2speed = 0; // keep motor speed above 0
  analogWrite(enablePin1, motor1speed);
  analogWrite(enablePin2, motor2speed);
}

here the code is succes working but i cant use on my PID program :frowning:

here the code is succes working but i cant use on my PID program

Since you didn't volunteer, I'll drag it out of you. Why can't you? PID computes an output based on an input and a setup, with the objective of moving the input closer to the setpoint, by changing the output.

If the setpoint is the desired speed, and the input is the actual speed, the output would be the required change in speed/pwm setting/etc. to get the current speed closer to the desired speed.

It isn't clear what your inputs, outpoints, or setpoints would be, though.