Error in PID based Line Follower code: "stray '\342' in program"

I am making a a PID based line follower.

I have attached the .ino file below so that you can compile it yourself and view the error messages.

I am getting the following error message -


Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

PID:52: error: stray '\342' in program

proportional = (pos – 100); //randomly chosen set point

^

PID:52: error: stray '\200' in program

PID:52: error: stray '\223' in program

C:\Users\user\Desktop\PID\PID.ino: In function 'void setMotors()':

PID:38: error: 'en1' was not declared in this scope

analogWrite(en1,mod(l));

^

PID:38: error: 'l' was not declared in this scope

analogWrite(en1,mod(l));

^

PID:38: error: 'mod' was not declared in this scope

analogWrite(en1,mod(l));

^

PID:39: error: 'en2' was not declared in this scope

analogWrite(en2, mod(r));

^

PID:39: error: 'r' was not declared in this scope

analogWrite(en2, mod(r));

^

PID:40: error: 'L1' was not declared in this scope

digitalWrite(L1, HIGH);

^

PID:41: error: 'L2' was not declared in this scope

digitalWrite(L2, LOW);

^

PID:42: error: 'r1' was not declared in this scope

digitalWrite(r1, HIGH);

^

PID:43: error: 'r2' was not declared in this scope

digitalWrite(r2, LOW);

^

C:\Users\user\Desktop\PID\PID.ino: In function 'void loop()':

PID:49: error: 'position' was not declared in this scope

position = readline();

^

PID:49: error: 'readline' was not declared in this scope

position = readline();

^

PID:52: error: expected ')' before numeric constant

proportional = (pos – 100); //randomly chosen set point

^

PID:53: error: 'derivative' was not declared in this scope

derivative = proportional - last_proportional;

^

PID:53: error: 'last_proportional' was not declared in this scope

derivative = proportional - last_proportional;

^

PID:60: error: 'powerDif' was not declared in this scope

powerDif = proportional * Kp + integral * Ki + derivative *Kd;

^

PID:73: error: 'power_difference' was not declared in this scope

if(power_difference < 0)

^

exit status 1
stray '\342' in program

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


For this code -

int rmf = 3;
int rmb = 5;
int lmf = 6;
int lmb = 9;

int a[4];

int pos = 0;
int sensorSum = 0;

int derivitative = 0;
int integral = 0;
int proportional = 0;

int Kp;
int Kd;
int Ki;

void setup() {
  // put your setup code here, to run once:
Serial.begin (9600);
}

void sense(){
  for (int i = 0; i<5; i++)
  {
    a[i] = analogRead(i);
    sensorSum += a[i]; 
  }

  Serial.print(sensorSum);

  pos = (-2000*a[0] + -1000*a[1] + 0*a[2] + 1000*a[3] + 2000*a[4])/(a[1] + a[2] + a[3] + a[4]);
  Serial.print (pos);
}

void setMotors (){
  analogWrite(en1,mod(l));
  analogWrite(en2, mod(r));
  digitalWrite(L1, HIGH);
  digitalWrite(L2, LOW);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, LOW);
}
void loop() {
  // put your main code here, to run repeatedly:
 sense();

 position = readline();
 Serial.println("position = ");
 Serial.println(pos);
 proportional = ((int)pos – 100); //randomly chosen set point 
 derivative = proportional - last_proportional;
 integral = integral+proportional;
 last_proportional = proportional;
 Kp = 0.08;
 Ki = 0.0002;
 Kd = 1.0;
 
 powerDif = proportional * Kp + integral * Ki + derivative *Kd;

 const int max = 180;
 
 if(powerDif>max)
 {
  powerDif = max;
 }
      
 if(powerDif < -max)
 {
  powerDif = (-1*max);
 }
 if(power_difference < 0)
 {
  setMotors(max+powerDif, max);
 }
 else 
 {
  setMotors(max, max-powerDif);  
 }

 
 }

What changes should I make in my code ?

PID.ino (1.34 KB)

This happens sometimes when copy-and-pasting stuff from the web. The content shows up as white space, but i's not actually spaces and tabs and the compiler doesn't know what to make of them.

To fix, copy and paste all your code into Notepad or another editor which will display those stray characters, and replace them with ordinary spaces.

Paste your code in the reply box on this forum.
Next select it all and copy.
Next paste back in your sketch, completely overwriting what is current there.

That should do the trick. You do not have to post the reply.