Code issues for a lap counter on a line-follower robot on the Arduino

Asked

Viewed 25 times

-1

Hello, I am beginner in the programming study with Arduino, and my teacher asked for the following exercise:

"Using the line follower code implemented in previous lessons, the student must implement a sensor to the right of the robot that will count to 6 when this value is reached the same must stop."

The code he refers to, was made by me and this is it:

int sensorladoesq;
int sensorladodir;

void setup()
{
  Serial.begin(9600);
  pinMode(8, OUTPUT); //Este é o motor direito
  pinMode(12, OUTPUT); //Este é o motor esquerdo
}

void loop()
{
  sensorladoesq = analogRead(A1); //Lê a porta analógica do motor esquerdo
  sensorladodir = analogRead(A2); //Lê a porta analógica do motor direito
  
  if ((sensorladoesq < 225) and (sensorladodir < 225)){
  Serial.println("Andando pra frente");
  digitalWrite(12, HIGH);
  digitalWrite(8, HIGH);
  
  }
  
  if (sensorladodir > 225){
  Serial.println("Robo virou pra esquerda");
  digitalWrite(8, HIGH);
  digitalWrite(12, LOW);
  
  }
  
  if (sensorladoesq > 225){
  Serial.println("Robo virou pra direita");
  digitalWrite(12, HIGH);
  digitalWrite(8, LOW);
    
  }
}

I would like help in how to implement this counter, since my teacher has given us no other explanation about.

Thanks in advance.

1 answer

0

See the example below, made based on yours. I added a variable deveAndar, in which she is true while the robot must move. It becomes false from the moment the exercise condition takes place.

For ease, use functions, so you can reuse the same code snippet, making it more organized and easy to maintain.

To wait for 6 seconds, the function was used delay.

boolean deveAndar = true;
int sensorladoesq;
int sensorladodir;

void setup(){
    Serial.begin(9600);
    pinMode(8, OUTPUT); //Este é o motor direito
    pinMode(12, OUTPUT); //Este é o motor esquerdo
}

void andaParaFrente(){
    Serial.println("Andando pra frente");
    digitalWrite(12, HIGH);
    digitalWrite(12, HIGH);
}

void viraParaEsquerda(){
    Serial.println("Robo virou pra esquerda");
    digitalWrite(8, HIGH);
    digitalWrite(12, LOW);
}

void viraParaDireita(){
    Serial.println("Robo virou pra direita");
    digitalWrite(12, HIGH);
    digitalWrite(8, LOW);
}

void parar(){
    Serial.println("Robo parado");
    digitalWrite(0, HIGH);
    digitalWrite(0, LOW);
}

void loop(){        
    while (deveAndar){
        
        sensorladoesq = analogRead(A1); //Lê a porta analógica do motor esquerdo
        sensorladodir = analogRead(A2); //Lê a porta analógica do motor direito

        if ((sensorladoesq < 225) and (sensorladodir < 225)){
            andaParaFrente();
        }
        if (sensorladodir > 225){
            viraParaEsquerda();
        }
        if (sensorladoesq > 225){
            viraParaDireita();
            // Aguarda por 6 segundos = 6000 milisegundos
            delay(6000);
            // Para o robo
            parar();
            deveAndar = false;
        }

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.