Motor control code

I am trying to use an l293d chip and have been trying to make a self-guiding robot. I am trying to get this function to work but I get the following errors in the debug log. I have spent ages trying to find the problem but without any luck

robot:33: error: expected unqualified-id before 'if' robot:37: error: expected unqualified-id before 'else' robot:42: error: expected declaration before '}' token

I wasn't sure how much of the code to post but this is the function in question. I will post entire program below.

void motoron(boolean right, boolean reverse) {
  
  if (right == true){
    digitalWrite(rmotorpin1, !reverse);
    digitalWrite(rmotorpin2, reverse);
  }
  else if (right == false){
    digitalWrite(lmotorpin1, !reverse);
    digitalWrite(lmotorpin2, reverse);
  }

}

Thanks, Dan

int rmotorpin1 = 13;
int rmotorpin2 = 12;
int lmotorpin1 = 11;
int lmotorpin2 = 10;

int usrsendpin = 9;
int usrrecpin = 8;
int uslsendpin = 7;
int uslrecpin = 6;

int mintimetoturn = 59; // t = d * 10^3 / 340.29 //// t = time in microseconds /// d = distance in millimetres


//functions///////////////////////////////////////////////////////////////////////////////////////////////////////
int getdistance(int usrecpin, int ussendpin){
  //returns distance in mm
  int duration;
  float distance;
  
  digitalWrite(ussendpin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(ussendpin, LOW);
  duration = (pulseIn(usrecpin, HIGH, mintimetoturn))/2;
  distance = 0.34029 * duration;
  
  return round(distance);
    
}

//Motor commands\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void motoron(boolean right, boolean reverse) {
  
  if (right == true){
    digitalWrite(rmotorpin1, !reverse);
    digitalWrite(rmotorpin2, reverse);
  }
  else if (right == false){
    digitalWrite(lmotorpin1, !reverse);
    digitalWrite(lmotorpin2, reverse);
  }

}
void movestraight(boolean back;){
  motoron(1, back);
  motoron(2, back);
}

void motorstop(boolean right){

  if (right == true){
    digitalWrite(rmotorpin1, LOW);
    digitalWrite(rmotorpin2, LOW);
  }else{
     digitalWrite(lmotorpin1, LOW);
     digitalWrite(lmotorpin2, LOW);
  }
  
}

void stopmoving(){
  motorstop(true);
  motorstop(false);
}

void spinright(){
 motoron(true, true);
 motoron(false, false); 
}

void spinleft(){
  motoron(false, true);
  motoron(true, false); 
}


//Running functions////////////////////////////////////////////////////////////

void setup(){
  //set pins to in/out
  pinMode(rmotorpin1, OUTPUT);
  pinMode(rmotorpin2, OUTPUT);
  pinMode(lmotorpin1, OUTPUT);
  pinMode(lmotorpin2, OUTPUT);
  
  pinMode(usrsendpin, OUTPUT);
  pinMode(usrrecpin, INPUT);
  pinMode(uslsendpin, OUTPUT);
  pinMode(uslrecpin, INPUT);
  
  //set the robot moving
  movestraight(false);
  
}

void loop(){
  
  //check if anything ahead
  if (getdistance(usrrecpin, usrsendpin) != 0){
    spinright();
    while (getdistance(usrrecpin, usrsendpin) != 0){
      delay(1);
    }
    movestraight(false);
    
  }else if (getdistance(uslrecpin, uslsendpin) != 0){
    while (getdistance(uslrecpin, uslsendpin) != 0){
      delay(1);
    }
    movestraight(false);
  }
  //else go forward
}
  if (right == true){

This is a bit redundant. If right contains true, this is equal to if(true == true), which is equal to if(true). If right contains false, this is equal to if(false == true), which is equal to if(false). Since the value in the parens at the end is the value in right, if(right) is sufficient. The if statement in the else is unnecessary.

void movestraight(boolean back;){

That semicolon doesn't belong there.

But this is your current problem

//Motor commands\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Ditch it!

Mark

Thanks for the heads up but it doesn't solve the problem :L

Removing that line sorts out the compiler error you asked about and PaulS has already pointed out the next.

If you have more then post the errors and the new code.

Mark

Sorry about my second post. The post with the fix wasn’t there when I posted my reply. I will try when I get home this evening. Thanks for your help!

I'm not sure, but I don't think round() is a valid function call.