line tracking code error compile

This is my pid program for robot

got 5 sensors 2 pins each to drive motor from driver and 1 each for pwm

getting
error: a function-definition is not allowed here before '{' token
at compile

// ————————————————————————— Motors
int motor_left[] = {8, 9 };
int motor_right[] = {10, 11};
int motor_right0 = 10 ;
int motor_right1 = 11 ;
int left2 = 12;
int left1 = 13;
int center = 0;
int right1 = 1;
int right2 = 2;
int pwml = 3;
int pwmr = 4;
int Myint;
int Mpos;
long Diff;
long Difflast;
int rate;
int prop;
int Deriv;
int Intergral;
int Control;
int temp1;
int Lfast = 255;
int Rfast = 255;
int Tpos = 0;
int Highlimit = 255;
int Lowlimit = -255;
float Kp = 0;
float Kd = 0;
float Ki = 0;
float Interval = 0.01;
// ————————————————————————— Setup
void setup() {

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left*, OUTPUT);*
pinMode(motor_right*, OUTPUT);
pinMode(left2, INPUT);
pinMode(left1, INPUT);
pinMode(center, INPUT);
pinMode(right1, INPUT);
pinMode(right2, INPUT);
_
}_
void loop()
_
{_
_
void drive();_
_
void decide();_
_
void cal();_
_
void apply();_
_
}_
void drive()
{digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
*analogWrite (pwml ,Lfast); *
analogWrite (pwmr ,Rfast);
_
}_
void decide()
_
{_
_
if (left2 == LOW && left1 == HIGH && center == HIGH && right1 == HIGH && right2 == HIGH)_
_
{_
_
Mpos = -40;_
_
}_
if (left2 == LOW && left1 == LOW && center == HIGH && right1 == HIGH && right2 == HIGH)
_
{_
_
Mpos = -30;_
_
}if (left2 == HIGH && left1 == LOW && center == HIGH && right1 == HIGH && right2 == HIGH)_
_
{_
_
Mpos = -20;_
_
}if (left2 == HIGH && left1 == LOW && center == LOW && right1 == HIGH && right2 == HIGH)_
_
{_
_
Mpos = -10;_
_
}if (left2 == HIGH && left1 == HIGH && center == LOW && right1 == HIGH && right2 == HIGH)_
_
{_
_
Mpos = 0;_
_
Integral = 0;_
_
}if (left2 == HIGH && left1 == HIGH && center == LOW && right1 == LOW && right2 == HIGH)_
_
{_
_
Mpos = 10;_
_
}if (left2 == HIGH && left1 == HIGH && center == HIGH && right1 == LOW && right2 == HIGH)_
_
{_
_
Mpos = 20;_
_
}if (left2 == HIGH && left1 == HIGH && center == HIGH && right1 == LOW && right2 == LOW)_
_
{_
_
Mpos = 30;_
_
}if (left2 == HIGH && left1 == HIGH && center == HIGH && right1 == HIGH && right2 == LOW)_
_
{_
_
Mpos = 40;_
_
}if (left2 == HIGH && left1 == HIGH && center == HIGH && right1 == HIGH && right2 == HIGH)_
_
{_
if (Mpos < 0)
_
{Mpos = -30};_
else
_
{Mpos = 30};_
_
}_
_
}_
void cal(){
_
Diff = Tpos - Mpos;_
Prop = Diff * Kp;
Integral = Integral + Diff;
Integral = Integral * Ki;
Integral = Integral * Interval;
If (Integral > Highlimit)
_
{Integral = Highlimit};_
If (Integral < Lowlimit)
_
{Integral = Lowlimit};_
Rate = Diff - Difflast;
Deriv = Rate * Kd;
Difflast = Diff;
Control = Prop + Deriv;
Control = Control + Integral;
If (Control > Highlimit)
_
{Control = Highlimit};_
If (Control < Lowlimit)
_
{Control = Lowlimit};_
_
}_
void apply()
_
{_
If (Control <= 0)
_
{Temp1 = Lfast + Control;_
_
Myint = Temp1;_
_
analogWrite (pwml,Myint);_
_
Rightmotor = Rfast;_
_
}_
_
If*_
* If (Control > 0)*
* {Temp1 = Rfast - Control;*
* Myint = Temp1;*
* analogWrite (pwmr,Myint);*
* Leftmotor = Lfast;*
* }*

* delay (10)*
}

void setup() {

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(left2, INPUT);
pinMode(left1, INPUT);
pinMode(center, INPUT);
pinMode(right1, INPUT);
pinMode(right2, INPUT);

}

You are missing a } to close the for loop.

[rant]
If you used proper indenting, that would have been easy to see.

The compiler doesn't care about white space, but humans do.

There is a big controversy over whether the opening curly brace for a block of code goes on the line that starts the block, like this:

for(int i=0; i<3; i++) {

or on the following line:

for(int i=0; i<3; i++)
{

Personally, I like the latter method, and to line up the closing curly brace with the opening one:

void setup()
{
    for(int i=0; i<3; i++)
    {
        // Do something
    }
}

If you leave a { or } out, it's much easier to see.
[/rant]

thank you for ur help

but bot is not working

it seems that pwm is not working properly

i cant see gradual changes in there speed

i am using motors simmilar to solarbotics gm9 with 300 rpm

can u give me some tips

The variables left2, left1, etc. used in decide are pin numbers, not sensor values. The pin number will never be HIGH. The value read from the sensor attached to that pin might, but you never actually read the value of the sensors.

yes i know that mistake of mine

but forget about above program

i am simpply trying to impliment pwm form a motor driver

and its not giving me a good resolution

this is my program ( with pwm)
http://akashxav.com/2009/04/18/arduino-l293d-dc-motor/

and my motor driver is

http://robokits.co.in/shop/index.php?main_page=product_info&cPath=1&products_id=176

one more thing

i got my arduino from here

its a usb ver with atmega 8

so does it include 3 4 5 pins for pwm ?

the less no of pwm pins are because of lesser chip or older design "?

Atmega 8?

i got my arduino from here

Here? Where would that be?

:-[ :-[

http://www.bhasha.co.cc/product.php?id_product=22

The number of PWM pins is a function of the chip design, not the amount of memory the chip has. You should have 6 PWM pins - 3, 5, 6, 9, 10, and 11.

You should have 6 PWM pins

I thought the ATmega8 only has three.

The picture in the link showed an Arduino clone with either a 168 or 328 chip.

// ————————————————————————— Motors
int motor_left[] = {8, 9 };
int motor_right[] = {10, 11};
int left = 12;
int right = 13;
int center = 0;

// ————————————————————————— Setup
void setup() {

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left*, OUTPUT);*
pinMode(motor_right*, OUTPUT);
_
}_
pinMode(left,INPUT);
pinMode(right,INPUT);
pinMode(center,INPUT);
_
}_
_
// ————————————————————————— Loop*_
void loop() {
int left1 = digitalRead (left);
int right1 = digitalRead (right);
int center1 = digitalRead (center);

if (left1 == HIGH && center1 == LOW && right1 == HIGH)
{
* drive_forward();
_
}_
if (left1 == LOW && center1 == LOW && right1 == LOW)
_
{_
drive_forward();
_
}_
if (left1 == LOW && center1 == HIGH && right1 == HIGH)
_
{_
turn_left_hard();
_
}_
if (left1 == LOW && center1 == LOW && right1 == HIGH)
_
{_
turn_left();
_
}_
if (left1 == HIGH && center1 == HIGH && right1 == LOW)
_
{_
turn_right_hard();
_
}_
if (left1 == HIGH && center1 == LOW && right1 == LOW)
_
{_
turn_right();
_
}_
_
}_
_
// ————————————————————————— Drive*_
void drive_forward()
{
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_left_hard()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right_hard()
{
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left()
{
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right()
{
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
}
i got a smiilar code with only left and right and 2 sensor. works good
here the left and right word comes in same yellowish colour as int and pinmode etc. but when i add center it dosent work
i checked pins and changed left to left1 to change its colour
then it works fine but with 3 sensor it dosent
i have connected all sensors to 5v arduino power. i have kept common ground between moterdriver and arduino but when i connect vcc of motor driver to arduino arduino get power from moter driver
u can see motor driver in above links. so is it good way to power arduino ?

Rather then continue continuing the new topic you posted we'll work from this one. But first...

To modify your post you need to look in the upper right corner of the post you want to modify. Then click on the word Modify. Next highlight your code and then click the button in the text box marked with the "#" symbol.

Now in the other post you posted this code:

// —————————————————————————  Motors
int motor_left[] = {8, 9 };
int motor_right[] = {10, 11};
int motor_right0 = 10 ;
int motor_right1 = 11 ;
int left = 12;
int center = 13;
int right = 0;
int pwml = 3;
int pwmr = 4;
int Myint;
int Mpos;
long Diff;
long Difflast;
int Rate;
int prop;
int Deriv;
int Integral ;
int Control;
int Temp1;
int Lfast = 255;
int Rfast = 255;
int Tpos = 0;
int Highlimit = 255;
int Lowlimit = -255;
int Prop;
float Kp = 1;
float Kd = 0;
float Ki = 0;
float Interval = 0.01;
// ————————————————————————— Setup
void setup() {

// Setup motors
int i;
for(i = 0; i < 2; i++)
{
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

pinMode(left, INPUT);
pinMode(center, INPUT);
pinMode(right, INPUT);


}

void loop()
{
int left1 = digitalRead (left);
int center1 = digitalRead (center);
int right1 = digitalRead (right);


if (left1 == HIGH && center1 == LOW && right1 == HIGH)
Mpos =0 ;
Integral  = 0 ;
if (left1 == HIGH && center1 == LOW && right1 == LOW)
Mpos =10;
if (left1 == HIGH && center1 == HIGH && right1 == LOW)
Mpos =20;
if (left1 == LOW && center1 == LOW && right1 == HIGH)
Mpos = -10;
if (left1 == LOW && center1 == HIGH && right1 == HIGH)
Mpos = -20;
if (left1 == HIGH && center1 == HIGH && right1 == HIGH)
{
  if (Mpos < 0 )
  {
    Mpos = -30 ;
  }
  else
  {
    Mpos =30 ;
  }

Diff = Tpos - Mpos;
Prop = Diff * Kp;
Integral  = Integral  + Diff;
Integral  = Integral  * Ki;
Integral  = Integral  * Interval;
if (Integral   > Highlimit)
{
Integral = Highlimit;
}
if (Integral  < Lowlimit)
{
 Integral  = Lowlimit;
}
Rate = Diff - Difflast;
Deriv = Rate * Kd;
Difflast = Diff;
Control = Prop + Deriv;
Control = Control + Integral;
if (Control > Highlimit)
{
Control = Highlimit;
}
if (Control < Lowlimit)
{
 Control = Lowlimit;
}

if (Control <= 0)
    {Temp1 = Lfast + Control;
    Myint = Temp1;
    analogWrite (pwml,Myint);
    digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
analogWrite (pwmr,Rfast);
    }
 

 if (Control > 0)
    {Temp1 = Rfast - Control;
    Myint = Temp1;
    analogWrite (pwmr,Myint);
    digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
analogWrite (pwml , Lfast);
    }

 
 delay (10);
}
}

First things first. What problems are you having?

Now still being new to this the first thing i'm looking for are the possible small errors. First thing i notice is your "int i" before using it in a "for()" statement. This is not needed.

Next a little more organization in your code would help. Add more comments so its easier fro others to figure out what your trying to achieve with each step of the code. A big help would be getting the proper indents added so its less confusing.

In your first "if()" statement,

if (left1 == HIGH && center1 == LOW && right1 == HIGH)
Mpos =0 ;
Integral  = 0 ;

its not clear if the "integral = 0;" is suppose to be part of that statement or separate. Once again the proper intentions on the code would help point that out as a problem or not. It may be helpful to have all "if()" statements use the curly brackets ({}) even if there is only one command it will execute.

First thing i notice is your "int i" before using it in a "for()" statement. This is not needed.

Misleading; it is absolutely necessary to declare a variable "i", because there is no other "i" in scope at this point.
There is nothing at all wrong with "setup" as written. (though some proper indentation and a few "const" qualifiers on the array declarations wouldn't go amiss)

Also, this

if (left1 == HIGH && center1 == LOW && right1 == HIGH)
Mpos =0 ;
Integral  = 0 ;
if (left1 == HIGH && center1 == LOW && right1 == LOW)
Mpos =10;
if (left1 == HIGH && center1 == HIGH && right1 == LOW)
Mpos =20;
if (left1 == LOW && center1 == LOW && right1 == HIGH)
Mpos = -10;
if (left1 == LOW && center1 == HIGH && right1 == HIGH)
Mpos = -20;
if (left1 == HIGH && center1 == HIGH && right1 == HIGH)

seems unnecessarily complex - Factor it up a little, instead of testing "left1 == HIGH" each time, test it once:

if (left1 == HIGH {
  if (centre1 == HIGH) {
  } else {
   // centre 1 is LOW, left1 is HIGH
  }
} else {
  //left1 is low
}

It may make it easier for you to see the logic.

Ok i see where my confusion was. She was initiating the variable outside the for() statement when i'm use to using it inside the for() statement.

i am sorry about my code quoting

in above code just concetrate on pid part and please suggest mistake as i dont know about pid

it is code for robot called microbot

the author sent me code.

http://www.wrighthobbies.net/bots/microbot/index.htm

$regfile = "m48def.dat"
$crystal = 8000000

Dim Sensors As Byte , I As Byte , Myint As Integer
Dim Mpos As Integer , Diff As Long , Difflast As Long
Dim Rate As Single , Prop As Single , Deriv As Single , Integral As Single
Dim Control As Single , Temp1 As Single


'Tccr0a = &B10100011                                         'Set Registers for PWM
'Tccr0b = &B00000001                                         'Bascom doesn't have built-in support for Tiny13 PWM yet

Config Timer0 = Pwm , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 1

Config Portb.0 = Output
Config Portb.1 = Output
Config Portb.2 = Input
Config Portb.3 = Input
Config Portb.4 = Input

'Turn on Pull up Resistors
Portb.2 = 1
Portb.3 = 1
Portb.4 = 1

'Aliases

Rightmotor Alias Pwm0a                                      'PWMA Channel
Leftmotor Alias Pwm0b                                       'PWMB Channel

Const Nogo = 127
Const Lfast = 255
Const Rfast = 255
Const Tpos = 0
Const Highlimit = 255
Const Lowlimit = -255                                       '0

'Constants for PID

Const Kp = 1.75                                             'Gain factor for proportional
Const Kd = 11                                               'Gain factor for derivitive
Const Ki = .05

Const Interval = .01
Rightmotor = Nogo
Leftmotor = Nogo

Wait 5

Rightmotor = Rfast
Leftmotor = Lfast

Do
   Sensors = 0                                              'Sensor values are stored as bits in byte
   Sensors.0 = Not Pinb.2                                   'Left Sensor
   Sensors.1 = Not Pinb.4                                   'Center Sensor
   Sensors.2 = Not Pinb.3                                   'Right Sensor

   Select Case Sensors

   Case &B010                                               'Center Sensor
   Mpos = 0
   Integral = 0

   Case &B011                                               'Center and Right Sensor
   Mpos = 10
   Case &B001                                               'Right Sensor
   Mpos = 20

   Case &B110                                               'Center and Left Sensor
   Mpos = -10

   Case &B100                                               'Left Sensor
   Mpos = -20

   Case &B000
   If Mpos < 0 Then Mpos = -30 Else Mpos = 30

   End Select

   Diff = Tpos - Mpos
   Prop = Diff * Kp
   Integral = Integral + Diff
   Integral = Integral * Ki
   'Integral = Integral * Interval
   If Integral > Highlimit Then Integral = Highlimit
   If Integral < Lowlimit Then Integral = Lowlimit

   Rate = Diff - Difflast                                   'Calculate Differential

   Deriv = Rate * Kd
   Difflast = Diff
   Control = Prop + Deriv
   Control = Control + Integral                             'New control value

   If Control > Highlimit Then Control = Highlimit
   If Control < Lowlimit Then Control = Lowlimit

   If Control <= 0 Then
      Temp1 = Lfast + Control
      Myint = Temp1
      Leftmotor = Myint
      Rightmotor = Rfast
   End If

   If Control > 0 Then
      Temp1 = Rfast - Control
      Myint = Temp1
      Rightmotor = Myint
      Leftmotor = Lfast
   End If

   Waitms 10


Loop

so please observe and please suggest conversion mistake

this will be good thread for PID

as i have seen people kept there pid code a secret

Hello,
Looks like the author bought a board using ATMega 8 for driving motors using PWM, which has 3 PWM outputs to otherwise 6 on ATMega 168 and 328. Make sure you buy compatible hardware before pointing fingers @ others.

;D :sunglasses: :wink:

If you need 6 PWM pins opt for ATMega 168/328 chip and dont whine.. :sunglasses: