stand-alone trolley with two axis compilation error

Asked

Viewed 40 times

0

hello.. I am beginner and am developing an altonomic cart and found an error in the compilation

Arduino: 1.8.13 (Linux), Card:"Arduino Uno"

/home/Acer/snap/Arduino/Current/Arduino/cart_autonomo1.0/cart_autonomo1.0.In: In Function 'void loop()': cart_autonomo1.0:54:9: error: 'Else' without a Previous 'if' Else (distance < 500){ ~~ cart_autonomo1.0:54:15: error: 'distacia' was not declared in this Scope Else (distance < 500){ ~~~~~~~~~ /home/Acer/snap/Arduino/Current/Arduino/cart_autonomo1.0/cart_autonomo1.0.Ino:54:15: note: suggested Alternative: 'distance' Else (distance < 500){ ~~~~~~~~~ distance cart_autonomo1.0:66:9: error: 'Else' without a Previous 'if' Else (action != "left"){ ~~ cart_autonomo1.0:66:34: error: expected ';' before '{' token Else (action != "left"){ ^ cart_autonomo1.0:73:11: error: 'Else' without a Previous 'if' Else (action == "left"){ ~~ cart_autonomo1.0:73:36: error: expected ';' before '{' token Else (action == "left"){ ^ cart_autonomo1.0:79:11: error: break statement not Within loop or switch break ~~~ cart_autonomo1.0:80:5: error: expected ';' before '}' token } ^ Exit status 1 'Else' without a Previous 'if'

that’s the mistake.

follows the code /second program18/11/

#include<Ultrasonic. h>

//sets the ultrasonic pins

#defines triger 12 #define echo 13

// sets rear engine control pins #define pmt1 3 #define pmt2 5

//sets direction pins to vehicle

#define pdir 0 #defines pesq 1

//initializes ultrasonic sensor Ultrasonic Ultrasonic(triger, echo);

int speed =0; String action;

void setup() { // starts serial monitor Serial.Begin(9600); pinMode (pmt1, OUTPUT); pinMode (pmt2, OUTPUT); pinMode (pdir, OUTPUT); pinMode (pesq, OUTPUT); //char acao; }

void loop() { //make distance reading int distance;

distance = Ultrasonic.read(); Serial.print ("distance in cm:"); Serial.println (distance); delay(1000);

//determines speed //int speed; if (distance>1000){ // speed = 192; Serial.println("speed selected 75% of rpm"); } Else (distance < 1000 and distance > 500);{ speed = 128; Serial.println("speed selected 50% of rpm"); } Else (distance < 500){ speed = 64; Serial.println("speed selected 25% of rpm"); }

//action analysis (front, right, left, or re

//char acao; String action;

if (distancia<200);{
    //vira pra direita        
    else (acao != "esquerda"){
      digitalWrite(pdir,HIGH);
      digitalWrite(pesq,LOW);
      acao="esquerda";
      //FALTA SUB PARA TRACIONAR MOTOR
      }
      //vira pra esquerda
      else (acao == "esquerda"){
        digitalWrite(pdir,LOW);
        digitalWrite(pesq,HIGH);
        acao="direita";
        //falta sub para tracionar motor
      }
      break
}

}

thank you

  • That your first one should be an if.

1 answer

0

Correction of errors related to conditional structures if and Else are relatively simple, just ensure that when a Else is inserted into the Code, which shall be preceded by a if.

For example, the code below will result in a build error because, you are inserting a Else without possessing his if.

// Esse código gera um erro de compilação
if (distancia < 200)
{ 
    // Esse "else" não possui um if antes
    else (acao != "esquerda")
    {
        // Faça algo
    }
    else (acao == "esquerda")
    {
        // Faça algo
    }
}

In this case the solution is relatively simple just sub-create the first Else by a if which, would be the desired behavior, after this one should also verify that you are performing another check, therefore it is necessary to insert another if before the condition, making the code so.

// Esse código compila
if (distancia < 200)
{ 
    if (acao != "esquerda")
    {
        // Faça algo
    }
    else if (acao == "esquerda")
    {
        // Faça algo
    }
}

Therefore, just apply these principles throughout the rest of your code and in this way errors related to conditional structures will be fixed.


Note also that if you are using the library Ultrasonic. h of filipeflop, this does not have a method read(), as you put on this line distancia = ultrasonic.read();.

Therefore, following the example on the website of filipeflop:

Just copy the code snippet that is used to calculate the distance in centimeters and then assign to the variable distance, resulting in something like this.

// Realiza a leitura da distância fornecida pelo sensor
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);

// Atribui a distância lida à variável "distancia" utilizando o cast.
int distancia = (int)cmMsec;

Finally, your code should result in something similar to this:

#include<Ultrasonic.h>

// Definição dos pinos do sensor ultrassônico
#define triger 12
#define echo 13

// Definição dos pinos para controle do motor traseiro
#define pdir 0 
#define pesq 1

// Definição dos pinos para controle da direção do veículo
#define pmt1 3
#define pmt2 5

int velocidade = 0;
String acao = "";

Ultrasonic ultrasonic(triger, echo);

void setup()
{
    // Inicialização do monitor serial
    Serial.begin(9600);

    // Configuração dos pinos
    pinMode(pmt1, OUTPUT);
    pinMode(pmt2, OUTPUT);
    pinMode(pdir, OUTPUT);
    pinMode(pesq, OUTPUT);
}

void loop()
{
    // Realiza a leitura da distância fornecida pelo sensor
    float cmMsec;
    long microsec = ultrasonic.timing();
    cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);

    // Atribui a distância lida à variável "distancia" utilizando o cast.
    int distancia = (int)cmMsec;

    // Imprime a distância lida pelo sensor
    Serial.print("distancia em cm:");
    Serial.println(distancia);

    delay(1000);

    // Inicia as verificações
    if (distancia > 1000)
    {
        velocidade = 192;
        Serial.println("velocidade selecionada 75% do rpm");
    }
    else if (distancia < 1000 && distancia > 500)
    {
        velocidade = 128;
        Serial.println("velocidade selecionada 50% do rpm");
    }
    else if (distancia < 500)
    {
        velocidade = 64;
        Serial.println("velocidade selecionada 25% do rpm");
    }

    if (distancia < 200)
    {
        if (acao != "esquerda")
        {
            digitalWrite(pdir, HIGH);
            digitalWrite(pesq, LOW);

            acao = "esquerda";
        }
        else if (acao == "esquerda")
        {
            digitalWrite(pdir, LOW);
            digitalWrite(pesq, HIGH);

            acao = "direita";
        }
    }
}

Browser other questions tagged

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