Pages: [1]   Go Down
Author Topic: Problem in if-else statement  (Read 954 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this an image for my project


* 2012-12-24 11.41.11.jpg (1484.18 KB, 2560x1920 - viewed 28 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the compiler doesnot go in if -statement
That's a new one.

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank u ver much  smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why the motor does not work?!
thanx in advance smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 362
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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.
« Last Edit: December 24, 2012, 12:15:27 pm by Jimmy60 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4079
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need simpler code:

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

Pages: [1]   Go Up
Jump to: