If statements not working

I have to program a ultrasonic distance sensor to tell a robot when to stop. basically there is a wall to the side of a track and i want the robot to drive parallel to the wall, find the distance and stop after the wall ends.

the problem i am having is that no matter the distance sensed the first if statement always returns true (even the the distance displays upwards of 300 cm). Also any thoughts on optimizing the code its self is welcomed as i am self taught on the arduino platform (i have 1 semester of Matlab knowledge)

Sorry the code is quite messy and the timings random as well as everything being printed multiple times i was just checking where the code was going wrong. i also do not claim any of the actual data sampling from the sensor to be my own work.

Any help is greatly appreciated, Thank you in advance,
Joe

Working_distance_checker12323.ino (2.43 KB)

Please, if the code is small, just post it.

const int trigPin = 2;
const int echoPin = 4;
int current_distance = 99;
int p_distance = 99;
int cm = 99;
int wall = 0;
const int pin11 = 11;
const int pin12 = 12;
const int pin13 = 13;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(pin11, OUTPUT);
  pinMode(pin12, OUTPUT);
  pinMode(pin13, OUTPUT);
  digitalWrite(pin11, HIGH);
}

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, cm;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  Serial.print("cm ");
  Serial.print(cm);
  Serial.println();
  delay(1500);

  //display previous distance
  Serial.print("previous ");
  Serial.print(p_distance);
  Serial.println();
  delay(1500);

  //display current distance
  current_distance = cm;
  Serial.print("current");
  Serial.print(current_distance);
  Serial.println();
  delay(1500);

//find if the robot has reached the wall 
  if (current_distance <= 15 && p_distance <= 15);
  {   Serial.print("first if triggered");
  Serial.println();
    digitalWrite(pin11, LOW); 
    digitalWrite(pin12, HIGH); 
    wall = 1;
  }
  //show via LEDs the wall is reached
  if (wall = 1 && current_distance >= 15 && p_distance >= 15);
  { digitalWrite(pin12, LOW);
    digitalWrite(pin13, HIGH);
  Serial.print("second if triggered");
  Serial.println();
  }
//show which variables have been triggered
  Serial.print("wall= ");
  Serial.print(wall);
  Serial.println();
  delay(1500);
    p_distance = cm;
  Serial.print("previous ");
  Serial.print(p_distance);
  Serial.println();
  delay(1500);
}
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
  if (current_distance <= 15 && p_distance <= 15);

lose the semicolon.

Make it easy for people - post your code where it can be seen

@AWOL beat me to it.

I prefer to nest my IFs - to make the logic clearer - for example

	if (current_distance <= 15) {
		if(p_distance <= 15)
		{   Serial.print("p_distance <= 15");
			Serial.println();
			digitalWrite(pin11, LOW); 
			digitalWrite(pin12, HIGH); 
			wall = 1;
		}
	}
	else { // if current_distance > 15
		if (p_distance >= 15) 
		{
			if(wall == 1)
			{
				digitalWrite(pin12, LOW);
				digitalWrite(pin13, HIGH);
				Serial.print("second if triggered");
				Serial.println();
			}
		}
	}

I think you will now see that you have not covered every possible option - but maybe you don't need to.

Note also that in your code you have if (wall = 1) when it should be ==

...R

const int pin11 = 11;
const int pin12 = 12;
const int pin13 = 13;

I don't think these constants add much to the readability. How about:

const int theNumber42 = 42;

What does that tell us?