Primary Expressions

Hello,

for my Project, I’m doing, I have to programm a car model to drive without help.

It looks like this at the moment (German comments, safe to ignore :wink: )
Also, Pin names are in german, but they should be clear

#include <Servo.h>
#define linksPin1 3
//Backbord-Motoren - Pin9 und Pin10 können keine PWM?
#define linksPin2 11
#define rechtsPin1 5
//Steuerbord-Motoren
#define rechtsPin2 6
#define echoPin 8
#define trigPin 4

int ultraschall()
{
  digitalWrite(trigPin, LOW); //sicher LOW
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);
  float entfernung = pulseIn(echoPin, HIGH) / 58.0;
  // Serial.print("Entfernung: "); Serial.print(entfernung); Serial.println(" cm");
}

Servo myservo;
int entfernungen[3];

void setup() {
  // Serial.begin(9600);
  myservo.attach(2);
  pinMode(linksPin1, OUTPUT);
  pinMode(linksPin2, OUTPUT);
  pinMode(rechtsPin1, OUTPUT);
  pinMode(rechtsPin2, OUTPUT);
  pinMode(trigPin, OUTPUT); //Signal mit Ultraschallsensor aussenden
  pinMode(echoPin, INPUT); //Signal mit Ultraschallsensor empfangen
}

void loop() {
  digitalWrite(linksPin1, HIGH);
  digitalWrite(linksPin2, HIGH);
  digitalWrite(rechtsPin1, HIGH);
  digitalWrite(rechtsPin2, HIGH);
  for (int i = 60 ; i < 101 ; i = i + 20) {
    myservo.write(i);
    ultraschall();
    delay(2000);
  }
  if (ultraschall() <= 38) {
    analogWrite(rechtsPin1, LOW);
    analogWrite(rechtsPin2, LOW);
    analogWrite(linksPin1, LOW);
    analogWrite(linksPin2, LOW);
    umgebung_scannen;
  }
}

void umgebung_scannen() {
  for (int g = 0 ; g < 3 ; g++) {
    myservo.write(80 + g * 10);
    delay(150);
    entfernungen[g] = ultraschall();
    // Richtung; Entfernung
  }
  myservo.write(90);
  if (abs(entfernungen[0] - entfernungen[2]) <= 1) { //Wand detektiert
  }

  else if (entfernungen[0] - entfernungen[2] > 1) { //Hindernis links
    fahrrechts(int Speed = 255, int Time = 1000);
    geradeaus(int Time = 2000);
    fahrlinks(int Speed = 255, Time = 1000);
  }

  else () { //Hindernis rechts
    fahrlinks(int Speed = 255, int Time = 1000);
    geradeaus(int Time = 2000);
    fahrrechts(int Speed = 255, Time = 1000);
  }
}


void fahrrechts(int Speed, int Time){
  if (Time != 0) {
    if (Speed < 0) {
      Speed = -1 * Speed;
      analogWrite(rechtsPin1, Speed);
      analogWrite(rechtsPin2, LOW);
      delay(Time);
      analogWrite(rechtsPin1, LOW);
      analogWrite(rechtsPin2, LOW);
    }
    else {
      analogWrite(rechtsPin1, LOW);
      analogWrite(rechtsPin2, Speed);
      delay(Time);
      analogWrite(rechtsPin1, LOW);
      analogWrite(rechtsPin2, LOW);
    }
  }
}

void fahrlinks(int Speed, int Time){
  if (Time != 0) {
    if (Speed < 0) {
      Speed = -1 * Speed;
      analogWrite(linksPin1, Speed);
      analogWrite(linksPin2, LOW);
      delay(Time);
      analogWrite(linksPin1, LOW);
      analogWrite(linksPin2, LOW);
    }
    else {
      analogWrite(linksPin1, LOW);
      analogWrite(linksPin2, Speed);
      delay(Time);
      analogWrite(linksPin1, LOW);
      analogWrite(linksPin2, LOW);
    }
  }
}

void geradeaus(int Time){
  if (Time != 0) {
      digitalWrite(linksPin1, HIGH);
      digitalWrite(linksPin2, HIGH);
      digitalWrite(rechtsPin1, HIGH);
      digitalWrite(rechtsPin2, HIGH);
      delay(Time);
      digitalWrite(linksPin1, LOW);
      digitalWrite(linksPin2, LOW);
      digitalWrite(rechtsPin1, LOW);
      digitalWrite(rechtsPin2, LOW);
    }
  }

void stopp(){
  digitalWrite(linksPin1, LOW);
  digitalWrite(linksPin2, LOW);
  digitalWrite(rechtsPin1, LOW);
  digitalWrite(rechtsPin2, LOW);
}

Now i got the error message:
‚Äúexpected primary-expression before ‚Äėint‚Äô‚ÄĚ
almost everywhere.

Error Code is huge:

Arduino: 1.8.12 (Windows 10), Board: "Arduino Uno"

LG_Mobil:113:16: error: expected primary-expression before 'int'

     fahrrechts(int Speed = 255, int Time = 1000);

                ^~~

LG_Mobil:113:33: error: expected primary-expression before 'int'

     fahrrechts(int Speed = 255, int Time = 1000);

                                 ^~~

LG_Mobil:114:15: error: expected primary-expression before 'int'

     geradeaus(int Time = 2000);

               ^~~

LG_Mobil:115:15: error: expected primary-expression before 'int'

     fahrlinks(int Speed = 255, Time = 1000);

               ^~~

LG_Mobil:115:32: error: 'Time' was not declared in this scope

     fahrlinks(int Speed = 255, Time = 1000);

                                ^~~~

LG_Mobil:118:9: error: expected primary-expression before ')' token

   else () { //Hindernis rechts

         ^

exit status 1
expected primary-expression before 'int'

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

I never had such a long Error Message.

Can you explain me, how to fix this (and maybe other mistakes, if you find some.)

~ColdRogue26

Declare the speed and time variables separately. The compiler doesn’t like you trying to declare them in a function call.

#include <Servo.h>
#define linksPin1 3
//Backbord-Motoren - Pin9 und Pin10 können keine PWM?
#define linksPin2 11
#define rechtsPin1 5
//Steuerbord-Motoren
#define rechtsPin2 6
#define echoPin 8
#define trigPin 4

int ultraschall()
{
  digitalWrite(trigPin, LOW); //sicher LOW
  delayMicroseconds(10);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(100);
  digitalWrite(trigPin, LOW);
  float entfernung = pulseIn(echoPin, HIGH) / 58.0;
  // Serial.print("Entfernung: "); Serial.print(entfernung); Serial.println(" cm");
}

Servo myservo;
int entfernungen[3];

void setup() {
  // Serial.begin(9600);
  myservo.attach(2);
  pinMode(linksPin1, OUTPUT);
  pinMode(linksPin2, OUTPUT);
  pinMode(rechtsPin1, OUTPUT);
  pinMode(rechtsPin2, OUTPUT);
  pinMode(trigPin, OUTPUT); //Signal mit Ultraschallsensor aussenden
  pinMode(echoPin, INPUT); //Signal mit Ultraschallsensor empfangen
}

void loop() {
  digitalWrite(linksPin1, HIGH);
  digitalWrite(linksPin2, HIGH);
  digitalWrite(rechtsPin1, HIGH);
  digitalWrite(rechtsPin2, HIGH);
  for (int i = 60 ; i < 101 ; i = i + 20) {
    myservo.write(i);
    ultraschall();
    delay(2000);
  }
  if (ultraschall() <= 38) {
    analogWrite(rechtsPin1, LOW);
    analogWrite(rechtsPin2, LOW);
    analogWrite(linksPin1, LOW);
    analogWrite(linksPin2, LOW);
    umgebung_scannen;
  }
}

void umgebung_scannen() {
  for (int g = 0 ; g < 3 ; g++) {
    myservo.write(80 + g * 10);
    delay(150);
    entfernungen[g] = ultraschall();
    // Richtung; Entfernung
  }
  myservo.write(90);
  if (abs(entfernungen[0] - entfernungen[2]) <= 1) { //Wand detektiert
  }

  else if (entfernungen[0] - entfernungen[2] > 1) { //Hindernis links
    fahrrechts(255, 1000);
    geradeaus(2000);
    fahrlinks(255, 1000);
  }

  else { //Hindernis rechts
    fahrlinks(255, 1000);
    geradeaus(2000);
    fahrrechts(255, 1000);
  }
}


void fahrrechts(int Speed, int Time) {
  if (Time != 0) {
    if (Speed < 0) {
      Speed = -1 * Speed;
      analogWrite(rechtsPin1, Speed);
      analogWrite(rechtsPin2, LOW);
      delay(Time);
      analogWrite(rechtsPin1, LOW);
      analogWrite(rechtsPin2, LOW);
    }
    else {
      analogWrite(rechtsPin1, LOW);
      analogWrite(rechtsPin2, Speed);
      delay(Time);
      analogWrite(rechtsPin1, LOW);
      analogWrite(rechtsPin2, LOW);
    }
  }
}

void fahrlinks(int Speed, int Time) {
  if (Time != 0) {
    if (Speed < 0) {
      Speed = -1 * Speed;
      analogWrite(linksPin1, Speed);
      analogWrite(linksPin2, LOW);
      delay(Time);
      analogWrite(linksPin1, LOW);
      analogWrite(linksPin2, LOW);
    }
    else {
      analogWrite(linksPin1, LOW);
      analogWrite(linksPin2, Speed);
      delay(Time);
      analogWrite(linksPin1, LOW);
      analogWrite(linksPin2, LOW);
    }
  }
}

void geradeaus(int Time) {
  if (Time != 0) {
    digitalWrite(linksPin1, HIGH);
    digitalWrite(linksPin2, HIGH);
    digitalWrite(rechtsPin1, HIGH);
    digitalWrite(rechtsPin2, HIGH);
    delay(Time);
    digitalWrite(linksPin1, LOW);
    digitalWrite(linksPin2, LOW);
    digitalWrite(rechtsPin1, LOW);
    digitalWrite(rechtsPin2, LOW);
  }
}

void stopp() {
  digitalWrite(linksPin1, LOW);
  digitalWrite(linksPin2, LOW);
  digitalWrite(rechtsPin1, LOW);
  digitalWrite(rechtsPin2, LOW);
}

Thanks for the quick help, it worked. :slight_smile: