Program not working

Hi,
The program that I have attached is not working right, I cant seem to find the error. Please anyone help

sketch_feb14b.ino (4.33 KB)

ramyar:
Hi,
The program that I have attached is not working right, I cant seem to find the error. Please anyone help

What should it do ?
What does it do ?

Here's the code to save anyone downloading it.... (hint to OP, there.... )

// change in main
char Last_Direction = '\0';
const int IRSense1 = A0;
const int IRSense2 = A1;
const int IRSense3 = A2;
const boolean H_EN1 = 2; //It's a PWM pin
const boolean H_EN2 = 5; //It's a PWM pin
const int M1_F = 3;       
const int M1_R = 4;       
const int M2_F = 6;      
const int M2_R = 7;  
int Obstacle_F;
int Obstacle_L;
int Obstacle_R;    

void Read()
{
Obstacle_F = analogRead(IRSense1);
Obstacle_L = analogRead(IRSense2);
Obstacle_R = analogRead(IRSense3);
}

void setup() 
{
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(H_EN1,OUTPUT);
  pinMode(H_EN2,OUTPUT);
  
  pinMode(M1_F,OUTPUT);
  pinMode(M1_R,OUTPUT);
  pinMode(M2_F,OUTPUT);
  pinMode(M2_R,OUTPUT);
}

void loop() 
{                     
   Drive();
}


void Drive() 
{ 
  Read();
  if((Last_Direction == '\0' || Last_Direction == 'S') && 190 >= Obstacle_F >= 70 && 70 <= Obstacle_L <= 850 && 70 <= Obstacle_R <= 850) 
  {
    Drive_Straight(); 
    Last_Direction = 'S';
  }
  else if(Last_Direction == 'L' && 190 >= Obstacle_F >= 70 && 70 <= Obstacle_L <= 850 && 70 <= Obstacle_R <= 850) 
  {
    while(730 <= Obstacle_R <= 850)
    {
    Drive_Straight();
    Last_Direction = 'S';
    Read();
    }
  }
  
  else if(Last_Direction == 'R' && 190 >= Obstacle_F >= 70 && 70 <= Obstacle_L <= 850 && 70 <= Obstacle_R <= 850) 
  {
    while(730 <= Obstacle_L <= 850)
    {
    Drive_Straight();
    Last_Direction = 'S';
    Read();
    }
    //if(850 <= Obstacle_F <= 850) ////// both side no obstacle //while?
    
  }
  else if((Last_Direction == '\0' || Last_Direction == 'S' || Last_Direction == 'E' || Last_Direction == 'T') && 730 <= Obstacle_F <= 850 && 730 <= Obstacle_R <= 850 && 190 >= Obstacle_L >= 70) 
  {
    if(Last_Direction == 'E' || Last_Direction == 'T')
    {
      Drive_Left();
      Last_Direction = 'L';
      Read();
      while(850 >= Obstacle_R >= 730)
      {
        Drive_Straight();
        Last_Direction = 'S';
        Read();
      }
    }
    else
    {
    Drive_Left(); 
    Last_Direction = 'L';
    Drive();
    }
  }
  
  else if((Last_Direction == '\0' || Last_Direction == 'S' || Last_Direction == 'E' || Last_Direction == 'T') && 730 <= Obstacle_F <= 850 && 730 <= Obstacle_L <= 850 && 190 >= Obstacle_R >= 70)
  {
    if(Last_Direction == 'E' || Last_Direction == 'T')
    {
      Drive_Right();
      Last_Direction = 'R';
      Read();
      while(850 >= Obstacle_L >= 730)
      {
        Drive_Straight(); 
        Last_Direction = 'S';
        Read();
      }
    }
    else
    {
    Drive_Right();
    Last_Direction = 'R';
    Drive();
    }
  }
  else if((Last_Direction == '\0' || Last_Direction == 'S') && 730 <= Obstacle_F <= 850 && 730 <= Obstacle_L <= 850 && 730 <= Obstacle_R <= 850)
  {
    while(730 <= Obstacle_L <= 850 || 730 <= Obstacle_R <= 850)
    {
      Retreat(); 
      if(Last_Direction == 'S' || Last_Direction == 'E')
        Last_Direction = 'E';
      else if(Last_Direction == '\0' || Last_Direction == 'T')
        Last_Direction = 'T';
      Read();
    }
    Stop();
    Drive();
  }
}

void Drive_Straight()
{
  digitalWrite(H_EN1, LOW); 
  digitalWrite(H_EN2, LOW);
  digitalWrite(M1_F, HIGH); 
  digitalWrite(M1_R, LOW);
  digitalWrite(M2_F, HIGH);
  digitalWrite(M2_R, LOW);
  analogWrite(H_EN1, 255);
  analogWrite(H_EN2, 255);
}

void Stop() 
{
  digitalWrite(H_EN1, LOW); 
  digitalWrite(H_EN2, LOW);
  digitalWrite(M1_F, LOW); 
  digitalWrite(M1_R, LOW);
  digitalWrite(M2_F, LOW);
  digitalWrite(M2_R, LOW);
  digitalWrite(H_EN1, 0);
  digitalWrite(H_EN2, 0);
}
  
void Drive_Left() 
{
  for(int Time = 0; Time <= 10; Time ++)
  {
    digitalWrite(H_EN1, LOW); 
    digitalWrite(H_EN2, LOW);
    digitalWrite(M1_F, LOW); 
    digitalWrite(M1_R, LOW);
    digitalWrite(M2_F, HIGH);
    digitalWrite(M2_R, LOW);
    digitalWrite(H_EN1, 0);
    analogWrite(H_EN2, 255); 
  }
}

void Drive_Right()
{
  for(int Time = 0; Time <= 10; Time ++)
  {
    digitalWrite(H_EN1, LOW); 
    digitalWrite(H_EN2, LOW);
    digitalWrite(M1_F, HIGH); 
    digitalWrite(M1_R, LOW);
    digitalWrite(M2_F, LOW);
    digitalWrite(M2_R, LOW);
    analogWrite(H_EN1, 255);
    digitalWrite(H_EN2, 0); 
  }
}


void Retreat()
{
  digitalWrite(H_EN1, LOW); 
  digitalWrite(H_EN2, LOW);
  digitalWrite(M1_F, LOW); 
  digitalWrite(M1_R, HIGH);
  digitalWrite(M2_F, LOW);
  digitalWrite(M2_R, HIGH);
  analogWrite(H_EN1, 255);
  analogWrite(H_EN2, 255); 
}

Someone with better C skills than mine can confirm or deny, but I'm not sure this....

... 190 >= Obstacle_F >= 70 ...

.... does what you expect.

I have a suspicion it needs to be in two and-ed pieces?

edit: That said, UKHB is correct to ask what the code does vs what you expect. You should also use a meaningful subject title.

JimboZA:

... 190 >= Obstacle_F >= 70 ...

Yeah, that doesn't do what you want it to. You can't chain comparison operators like that. It would check the first one, and resolve that to true (1) or false (0). Then it would test if that is >= 70, which it never would be.

Hi,
What model Arduino is it for and does it compile?

Tom.... :slight_smile:

I am using Arduino Mega, yes it compiled without error and the program is for obstacle detection.

ramyar:
yes it compiled without error

This....

190 >= Obstacle_F >= 70

.... will compile, but it doesn't do what you want.

Thank you, I will change it and try again :slight_smile: