Problem in if-else statement

int enableA1 = 2;
int enableA2 = 3;
int pinA1 = 1;
int pinA2 = 0;
int enableB1 = 7;
int enableB2 = 8;
int pinB1 = 6;
int pinB2 = 5;
int sensorpin=A0;
int value=0;
void setup()
{
Serial.begin(9600);
pinMode(enableA1, OUTPUT);
pinMode(enableA2, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);
pinMode(enableB1, OUTPUT);
pinMode(enableB2, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
}
void loop()
{

int analogValue = analogRead(sensorpin);
motorAOn();
//motorAOn();
//motorAForward();
if(analogValue > 550)
{

motorABackward();
//motorAOff();
//motorAForward();
}
else
{
motorAForward();

}
}

void motorAOn()
{
digitalWrite(enableA1, HIGH);
digitalWrite(enableA2, HIGH);
}
void motorBOn()
{
digitalWrite(enableB1, HIGH);
digitalWrite(enableB2, HIGH);
}
void motorAOff()
{
digitalWrite(enableA1, LOW);
digitalWrite(enableA2, LOW);
delay(1000);
}
void motorBOff()
{
digitalWrite(enableB1, LOW);
digitalWrite(enableB2, LOW);
}
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}
void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
delay(3000);
}
void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}

this is not syntax error,it is logic error the compiler doesnot go in if -statement

What is connected to the analog pin?
How did you connect it? please post schematic (you can attach an image to the post)

if you connect +5V to the analog pin what does the sketch do?
if you connect GND to the analog pin what does the sketch do?

Please use the # button to get code taggs around your code, makes it more readable. (you can modify earlier post to do it still)
Thank you

this an image for my project

the compiler doesnot go in if -statement

That's a new one.

Please read the sticky post at the top of this section.

It's a pity you used the serial pins for other purposes - it's much easier to debug a sketch with prints than it is by observation.

thank u ver much :slight_smile:

why the motor does not work?!
thanx in advance :slight_smile:

why the motor does not work?!

It was suggested that you change the code to use other pins? Did you?

It was suggested that you add debug statements. Did you?

If not, come back when you have.

If you have, you need to show us the revised code and the debug output.

If I were you I'd actually knock out a little sketch to test the readings with nothing else.

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

void loop(){
     Serial.println(analogRead(0));
     delay(2000);      // read every two seconds
}

If you look at that and check out the reference for the analogRead() function you may see your problem.

in this code motor A is turn on and make backward when sensor read a near distance but motor B not make any action
please any idea to help me?!
thanx in advance
int enableA1 = 8;
int enableA2 = 9;
int pinA1 = 10;
int pinA2 = 11;
int enableB1 = 12;
int enableB2 = 13;
int pinB1 = 2;
int pinB2 = 7;
int sensorpin=A0;
int value=0;
void setup()
{
Serial.begin(9600);
pinMode(enableA1, OUTPUT);
pinMode(enableA2, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);
pinMode(enableB1, OUTPUT);
pinMode(enableB2, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
}
void loop()
{

int analogValue = analogRead(sensorpin);
motorAOn();
motorBOn();
//motorAForward();
if(analogValue > 100)
{
//motorABackward();
Serial.println(analogValue);

//motorAOff();

motorABackward();
motorBForward();
//delay(3000);
}
else if(analogValue < 100)
{
Serial.println(analogValue);
motorAForward();
motorBForward();
//Serial.println(analogValue);
//delay(3000);

}
delay(1000);
}

// Yu Hin Hau
// Robotic Car via H-Bridge (L298)
// June 5, 2012

//See Low Level for Command Definitions

//Define Pins

//Define Low Level H-Bridge Commands

//enable motors
void motorAOn()
{
digitalWrite(enableA1, HIGH);
digitalWrite(enableA2, HIGH);
}

void motorBOn()
{
digitalWrite(enableB1, HIGH);
digitalWrite(enableB2, HIGH);
}

//disable motors
void motorAOff()
{
digitalWrite(enableA1, LOW);
digitalWrite(enableA2, LOW);
}

void motorBOff()
{
digitalWrite(enableB1, LOW);
digitalWrite(enableB2, LOW);
}

//motor A controls
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
Serial.println(“In forword”);
//delay(1000);
}

void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
Serial.println(“In backword”);
//delay(1000);
}

//motor B contorls
void motorBForward()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}

void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, HIGH);
}

//Define High Level Commands

void enableMotors()
{
motorAOn();
motorBOn();
}

void disableMotors()
{
motorAOff();
motorBOff();
}

void forward(int time)
{
motorAForward();
motorBForward();
delay(time);
}

void backward(int time)
{
motorABackward();
motorBBackward();
delay(time);
}

void turnLeft(int time)
{
motorABackward();
motorBForward();
delay(time);
}

void turnRight(int time)
{
motorAForward();
motorBBackward();
delay(time);
}

void coast(int time)
{
motorACoast();
motorBCoast();
delay(time);
}

void brake(int time)
{
motorABrake();
motorBBrake();
delay(time);
}

I can’t see anything in your program that would make motor B do anything but run forward no matter what value is returned by the sensor assuming, that is, that motorBForward(); does what its name implies.

void loop() 
{
  
  int analogValue = analogRead(sensorpin);
  motorAOn();
  motorBOn();
  //motorAForward();
  if(analogValue > 100)
  {
     //motorABackward();
    Serial.println(analogValue);
   
    //motorAOff();
    
    motorABackward();
    motorBForward();
    //delay(3000);
  }
  else if(analogValue < 100)
  {
    Serial.println(analogValue);
      motorAForward();
      motorBForward();
    //Serial.println(analogValue);
    //delay(3000);
  
  }
  delay(1000);
}

You need simpler code:

int enableB1 = 12;
int enableB2 = 13;
int pinB1 = 2;
int pinB2 = 7;

void setup()
{
  Serial.begin(9600);
  pinMode(enableB1, OUTPUT);
  pinMode(enableB2, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  motorBOn();
  motorBForward();
}

void loop() 
{
}

If motor B does not move, it is not a programming issue.