If statement multi-condition problem

Hey I am working on a stepper that moves depending on readings and under some conditions, I wrote an else if statement in a separate function, for If i put a function to rotate the stepper CW and for else if i put a function to rotate ACW. But else if didn’t work even if its condition was true

int way = 0;
int Readings = 0;
int blueline = 9;
int intByte ;
void setup()
{
Serial.begin(9600);
pinMode(IN1, OUTPUT); 
pinMode(IN2, OUTPUT); 
pinMode(IN3, OUTPUT); 
pinMode(IN4, OUTPUT); 
pinMode(Q,OUTPUT);
// delay(1000);

}
void loop() {
if (Serial.available() > 0) {
    int inByte = Serial.parseInt();
         Serial.print(inByte);
}

kar(intByte);
}

void kar (int x){
  
  if (x <= blueline && way == 0)
  {
        digitalWrite(Q, HIGH);
        rotatep();
        way = 1;
  }
else if(x > blueline && way == 1) 
  {
    digitalWrite(Q, LOW);
    rotaten();
    way = 0 ;
  }
 
}

Your indenting is horrid. After pasting your code in the IDE, and using Tools + Auto Format, and putting all the { on new lines, and deleting useless white space, I see that loop() looks like this:

void loop() 
{
  if (Serial.available() > 0) 
  {
    int inByte = Serial.parseInt();
    Serial.print(inByte);
  }

  kar(intByte);
}

So, if there is serial data to read, you read the data and store the integer created from the data in a local variable call inByte whose type is NOT byte. You print the value that you read, and the block ends, so inByte goes out of scope. Then, you call some function with some other value, and that function doesn't do what you expect. Is that right?

Because you have two tests in the IF statement there are 4 possible outcomes. But you are only acting on two of them. Is that what you want?

I think this line

kar(intByte);

should be

kar(inByte);
  • which would dispose of @PaulS's problem

I agree with Paul that it is silly and misleading to use the word Byte in the name of an int variable. Accurate and meaningful names are a big help in avoiding strange problems.

You seem to have other functions such as rotatep() which you have not included.

Post your complete program.

...R

okay i changed the inByte to datareading and declared it out of the loop()

and here is the full code

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11
#define Q 13
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left = 511;
long time;
int way = 0;
int datareading;
int Readings = 0;
int blueline = 9;
int intByte ;
void setup()
{
  Serial.begin(9600);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(Q, OUTPUT);
  // delay(1000);

}
void loop() 
{
  if (Serial.available() > 0) 
  {
    datareading = Serial.parseInt();
    Serial.print(datareading);
  }
  kar(datareading);
}

void kar (int x) 
{

  if (x <= blueline && way == 0)
  {
    digitalWrite(Q, HIGH);
    rotatep();
    way = 1;
  }
  else if (x > blueline && way == 1)
  {
    digitalWrite(Q, LOW);
    rotaten();
    way = 0 ;
  }

}


void rotatep()
{
  Direction = true;
  steps_left = 511;
  while (steps_left > 0) 
  {
    currentMillis = micros();
    if (currentMillis - last_time >= 1000) 
    {
      stepper(1);
      time = time + micros() - last_time;
      last_time = micros();
      steps_left--;
    }
  }

}
// move the motor ACW
void rotaten()
{
  Direction = false;
  steps_left = 511;
  while (steps_left > 0) 
  {
    currentMillis = micros();
    if (currentMillis - last_time >= 1000) 
    {
      stepper(1);
      time = time + micros() - last_time;
      last_time = micros();
      steps_left--;
    }
  }

}
// motor stepper function
void stepper(int xw) 
{
  for (int x = 0; x < xw; x++) 
  {
    switch (Steps) 
    {
      case 0:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        break;
      case 1:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, HIGH);
        break;
      case 2:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        break;
      case 3:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, HIGH);
        digitalWrite(IN4, LOW);
        break;
      case 4:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
      case 5:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, HIGH);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
      case 6:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
      case 7:
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, HIGH);
        break;
      default:
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, LOW);
        digitalWrite(IN3, LOW);
        digitalWrite(IN4, LOW);
        break;
    }
    SetDirection();
  }
}

void SetDirection() 
{
  if (Direction == 1){Steps++;}
  if (Direction == 0){Steps--;}
  if (Steps > 7){Steps = 0;}
  if (Steps < 0){Steps = 7;}
}

VitaTamry: and here is the full code

Glad you have it working.

...R