IBT_2 code problem

I busy whit programming a arduino to control IBT_2. I want to get current sense working on it.
So when the motor pulls to mutch current it turns its direction.
Normal operation works and i can meassure the current also on it.

But in the part IF ELSE i get a error but don't understand why in example in ELSE it is not there

That is if i verify the program it says

In function 'void loop()': 60:1: error: expected ';' before '{' token exit status 1

why does it say that. and in example it is not there. thit i do something wrong

int potpin1 = A3;
int switch1 = A1;
int switch2 = A2;
int pwm1 = 5;
int pwm2 = 6;
int enable1 = 3;
int enable2 = 2;
int output1;
int turn1; 
int switcha;
int switchb;
int current;
int testcurrent;

void setup() {
  digitalWrite(enable1, LOW);
  digitalWrite(enable2, LOW);
  
  delay (15);
}

void loop() {
 output1 = analogRead(potpin1);
   turn1 = map(output1, 0, 1023, 0, 255);

//switcha = analogRead(switch1);
//if (switcha == LOW){
//  digitalWrite(enable1, LOW);
//  digitalWrite(enable2, LOW);
//}
//else {
//    digitalWrite(enable1, HIGH);
//  digitalWrite(enable2, HIGH);
//}

//current 
//resistor 1k ohm
//measured voltage/ resistor=.. * 8500 = 42.5A
//5v/1k ohm * 8500 =42.5A
testcurrent = analogRead(switch2);
current = map(testcurrent,0,1023,0,42.5);
if (current <9, pwm2 == 0)
{  
analogWrite(pwm1, turn1);
analogWrite(pwm2, 0);
}
else if(current >=9, pwm2 == 0)
{
analogWrite(pwm2, turn1);
analogWrite(pwm1, 0);
}
else if (current <9, pwm1 == 0)
{
 analogWrite(pwm2, turn1);
analogWrite(pwm1, 0);
}
else (current >=9, pwm1 == 0)
{
 analogWrite(pwm1, turn1);
analogWrite(pwm2, 0); 
} 
    delay (100);
  }

Whit the if else it looks how mutch current is drawn and whit direction it is turning.

if this is working than i only want to make a extra potentionmeter to control maximum amperage

	else (current >=9, pwm1 == 0)

Don't you think there is an "if" missing on this line?

If you don't meant an else-if you must delete the condition as after an "else" statement the otherwise block immediately follows.

Also, if (current <9, pwm2 == 0) is this the way you write a compound if() line of code ?

Don’t invent syntax, review this:

@LarryD
you mean instead of , i need to use &&
That would be a easy fix.

@pylon
if i do that it also compiles correctly, but does the last thing not need to be else only whitout if. Or does that not matter and it can be used

We don't know that because we don't know in detail what the code is intended to do. Maybe that term was meant as a comment but the programmer forgot to set it as a comment?

Because of some trouble whit if and else setting up wanted to try it whit tinkercad first


This is how i have it now.
schematic is done, i have IBT2 and the current feedback i want to use.
In schematic i have made two engine to be one pwm so i can see in tinkercad if it works

But i have problem whit printin pwm1 and pwm 2 give the value i have at int in begin of script (so printin pwm1 is always 5) instead of the real value it sends to pin 5 and pin 6.

or is it not possible to see what arduino is sending over pin 5 and 6 through seriel monitor

Solution found
@pylon
found out that after "else" you will never have "(...)" behind it. The reason it didn't work

the only thing is IBT2 sends feedback whit 960hz but when duty is low it almost never measures the amperage, how can i fix this

int potpin1 = A0;
int potc = A1;
int switch1 = A2;
int switch2 = A3;
int switch3 = A4;
int currentm = A5;
int pwm1 = 5;
int pwm2 = 6;
int output1;
int outputc;
int turn1; 
int m1current; 
int m2current;     
int mescurrent; //measured current from ibt_2
int maxcurrent; // max set current
int setpwm1;
int setpwm2;


void setup() {
  Serial.begin(9600); 
  delay (15);
}

void loop() {
  int switchstate1 = digitalRead(switch1); //switch current set pot or set max
  int switchstate2 = digitalRead(switch2); // Left or right
  int switchstate3 = digitalRead(switch3); // autoturning direction on to off
output1 = analogRead(potpin1);
turn1 = map(output1, 0, 1023, 0, 255);
  Serial.write(13);
   Serial.print(turn1);
  Serial.print("=pwm speed,  ");
    Serial.print(m1current);   //current from ibt2
    Serial.print("=current from IBT2, ");
   Serial.print(m2current);   //max set current potmeter
    Serial.println("=max set current, ");
  
//current 
  //resistor 1k ohm
  //measured voltage/ resistor=.. * 8500 = 42.5A
  //5v/1k ohm * 8500 =42.5A
  mescurrent = analogRead(currentm);
  m1current = map(mescurrent,0,1023,0,42.5);
if (switchstate3 == HIGH)
{
  maxcurrent = analogRead(potc);
  m2current = map(maxcurrent,0,1023,0,42.5);
}
  else
  {
    m2current = 42;
  }
  

//currentswitch
if (switchstate1 == LOW)  
{
Serial.print("autorotate");
    if  (m1current<m2current && setpwm1 == 0)
    	{
		analogWrite(pwm1, 0); 
    	delay (10);
    	analogWrite(pwm2, turn1);
      	
      	Serial.println(", left current low");
      if (switchstate2 == LOW) 
      {
      setpwm1 = 0;
      setpwm2 = 1;  
      }
      else
      {
      setpwm1 = 1;
      setpwm2 = 0;
      }
		}
	else if (m1current<m2current && setpwm2 == 0)
    	{
       	analogWrite(pwm2, 0); 
    	delay (10);
    	analogWrite(pwm1, turn1);
      	setpwm1 = 1;
      	setpwm2 = 0;
      	Serial.println(", right current low");
      if (switchstate2 == HIGH) 
      {
      setpwm1 = 1;
      setpwm2 = 0;  
      }
      else
      {
      setpwm1 = 0;
      setpwm2 = 1;
      }
    	}
    else if (m1current>=m2current && setpwm1 == 0)
    	{
      if (switchstate2 == LOW)
      {
			analogWrite(pwm2, 0); 
    		delay (10);
    		analogWrite(pwm1, turn1);
      		setpwm1 = 1;
      		setpwm2 = 0;

      }
      else
      {
        analogWrite(pwm1, 0);
        delay  (10);
        analogWrite(pwm2, 0);
      	Serial.println(", left current high  !!!stopped!!!");
        delay (1000);
      }
    	}   
    else if (m1current>=m2current && setpwm2 == 0)
    	{
      if (switchstate2 == LOW)
      {
          	analogWrite(pwm1, 0); 
    		delay (10);
    		analogWrite(pwm2, turn1);
      		setpwm1 = 0;
      		setpwm2 = 1;
      		Serial.println(", right current high");
            }
      else
      	{
      	analogWrite(pwm2, 0);
        delay  (10);
        analogWrite(pwm1, 0);
      	Serial.println(", right current high  !!!stopped!!!");
        delay (1000);  
      	}
    }
     else
     {
     Serial.println(", error");
     analogWrite(pwm1, 0);
     analogWrite(pwm2, 0); 
     }
}
else
{
  Serial.print("stop autorotate");
    analogWrite(pwm1, 0);
    analogWrite(pwm2, 0); 
  
if (switchstate2 == LOW) 
{ 
  if (m1current<m2current)
  {
  analogWrite(pwm1, turn1);
    analogWrite(pwm2, 0);
  Serial.println(", right");
  }
  else
  {
    analogWrite(pwm1, 0);
    analogWrite(pwm2, 0);
    Serial.println(", stop working right");
  }
}
else 
{
  if (m1current<m2current)
  {
  analogWrite(pwm2, turn1);
  analogWrite(pwm1, 0);
  Serial.println(", left");
  }
  else
  {
    analogWrite(pwm1, 0);
    analogWrite(pwm2, 0);
    Serial.println(", stop working left");    
  }
}
}

   delay (500);  

  }


schematic is changed also a little bit for using pull up resistors for the switches

the only thing i want to add in the code is softstart and softstop in it
only i don't have any input pins left that i can use.
so i turned switch1 to digital pin 2 and set this as a input pin.
so i can use A2 to set amount of time for softstart and soft stop

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.