Problem making Line Follower - Codding


I want to make a line follower. This is my first project and I’ve never contacted with C++ or Robotics.

I’ve made this code, and I guess it will work (make the robot follow a line), but Im having a problem somewhere, that I can’t solve.

If you know that the code won’t work, even if the error is corrected, please help me to re-start.

Here’s the code:

#include <Servo.h> 
Servo myservo1; 
Servo myservo2;
void setup() 
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);


void loop () {
  int sensorValue1 = digitalRead(5);
  int sensorValue2 = digitalRead(6);
  int sensorValue3 = digitalRead(7);
  Serial.println(sensorValue1, DEC);
  Serial.println(sensorValue2, DEC);
  Serial.println(sensorValue3, DEC);

  if (digitalRead(5)=1) { //Sensor Direito Preenchido, Objectivo - Esquerda
    myservo1.write(0); //Motor Direito Avança
    myservo2.write(0); //Motor Esquerdo Recua

  if (digitalRead(6)=1) { //Sensor do Meio Preenchido
    myservo1.write(0); //Motor Direito Avança
    myservo2.write(128); // Motor Esquero Avança


  if (digitalRead(7)=1) { //Sensor Esquerdo Preenchido, Objectivo - Direita
    myservo1.write(128); //Motor Direito Recua
    myservo2.write(128); //Motor Esquerdo Avança

The errors are:

Sweep:33: error: expected unqualified-id before ‘if’
Sweep:42: error: expected unqualified-id before ‘if’
Sweep:51: error: expected unqualified-id before ‘if’

Thank you very much,


The main problem is that all of your “if(digitalRead…” lines are outside of the loop function. Bring them inside the loop function by putting them before the closing “}” on line 28.

Secondly, “digitalRead(7)=1” will not do what you want. To test for equality, you use “==” (two equals). Though in this case, you can just skip the equals entirely and say “if (digitalRead(7))”