Go Down

Topic: Problem in if-else statement (Read 1 time) previous topic - next topic

HassanAdel

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);
}

HassanAdel

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

robtillaart


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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

HassanAdel


AWOL

Quote
the compiler doesnot go in if -statement

That's a new one.

Please read the sticky post at the top of this section.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HassanAdel


HassanAdel

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

PaulS

Quote
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.

Jimmy60

#9
Dec 24, 2012, 06:12 pm Last Edit: Dec 24, 2012, 06:15 pm by Jimmy60 Reason: 1
If I were you I'd actually knock out a little sketch to test the readings with nothing else.

Code: [Select]

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.

HassanAdel

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);
}


UKHeliBob

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.

Code: [Select]
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);
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

You need simpler code:

Code: [Select]
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.

Go Up