Help with If/while statements

Having issues with my code, not sure what I am doing wrong. Every time it runs it gets stuck in the first while loop and never leaves.

When I run the serial monitor the values for my sensor drop dramatically when look at the same object at the same range when the motors turn off.

I attached a copy of my code.

Thanks.

Lab_7.ino (2.97 KB)

Very few people here are willing to open unknown files. Please read the How to Use This Forum post at the top of the Forum on how to post your code using code tags. Also, you can use Ctrl-T to reformat your code to a common C style.

This is the OP’s code

void setup()
{
  Serial.begin (9600);
  pinMode(4, OUTPUT);
  pinMode(A1, INPUT);
  pinMode(7, INPUT_PULLUP);
  pinMode(13, OUTPUT);        // connect Arduino pin 13 to Motor Controller board AI1
  pinMode(12, OUTPUT);        // connect Arduino pin 12 to Motor Controller board AI2
  pinMode(9, OUTPUT);         // connect Arduino pin 9 to Motor Controller board BI2
  pinMode(8, OUTPUT);          // connect Arduino pin 8 to Motor Controller board BI1
  pinMode(11, OUTPUT);       // connect Arduino pin 11 to Motor Controller board PWMA
  pinMode(10, OUTPUT);       // connect Arduino pin 10 to Motor Controller board PWMB
  pinMode(6, OUTPUT);
  pinMode(5, INPUT);

}


int counter = 0;

void loop()
{

  while (digitalRead(7) == HIGH)
  {

    Serial.print(getDistance());     //print the distance that was measured
    Serial.println(" in");      //print units after the distance

    delay (500);
  }
  while (true)
  {
    if (getDistance() < 10)
    {
      if (counter < 2)
      {
        counter = counter ++;
        leftv();
        Serial.print(getDistance());     //print the distance that was measured
        Serial.println(" in");      //print units after the distance
        delay (500);
      }

      else if ((1 < counter) && (counter < 4))
      {
        counter = counter ++;
        rightv();
        Serial.print(getDistance());     //print the distance that was measured
        Serial.println(" in");      //print units after the distance
        delay (500);
      }
      else if (counter = 4)

      {
        counter = counter ++;
        leftv();
        Serial.print(getDistance());     //print the distance that was measured
        Serial.println(" in");      //print units after the distance
        delay (500);
      }

      else
      {
        tone(4, 150);
        delay (300);
        tone(4, 300);
        delay (300);
        tone(4, 450);
        delay (300);
        tone(4, 600);
        delay (300);
        tone(4, 450, 300);
        delay (300);
      }
    }
    else
    {
      straight();
      Serial.print(getDistance());     //print the distance that was measured
      Serial.println(" in");      //print units after the distance
      delay (50);
    }
  }

}

//Functions//

float getDistance()
{
  float echoTime;
  float calculatedDistance;
  digitalWrite(6, HIGH);
  delayMicroseconds(10);
  digitalWrite(6, LOW);
  echoTime = pulseIn(5, HIGH);
  calculatedDistance = (echoTime / 148.0);
  return calculatedDistance;
}

void leftv()
{
  motor();
  analogWrite(11, 120);
  analogWrite(10, 0);
}

void rightv()
{
  motor();
  analogWrite(11, 0);
  analogWrite(10, 120);
}

void straight()
{
  motor();
  analogWrite(11, 125);
  analogWrite(10, 140);
}

void stopv()
{
  motor();
  analogWrite(11, 0);
  analogWrite(10, 0);
}

void motor()
{
  digitalWrite(13, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, HIGH);
}

What’s connected to pin 7? It will never leave the while loop if there isn’t something like a switch grounding that input.

Also, if the point is to delay the startup until something happens on pin 7, you can just put that first while loop in setup instead of having to encompass your working code in the redundant while(true) section

A button is connected to pin 7, and it doesn’t have an issues leaving

while (digitalRead(7) == HIGH)
{

Serial.print(getDistance()); //print the distance that was measured
Serial.println(" in"); //print units after the distance

delay (500);
}

It gets stuck in

if (counter < 2)
{
counter = counter ++;
leftv();
Serial.print(getDistance()); //print the distance that was measured
Serial.println(" in"); //print units after the distance
delay (500);
}

It gets stuck in

 if (counter < 2)

{
        counter = counter ++;
        leftv();
        Serial.print(getDistance());    //print the distance that was measured
      Serial.println(" in");      //print units after the distance
        delay (500);
      }

counter = counter++

This is syntax produces undefined behaviour. The compiler actually gives a warning if you have them turned on.

Use one of these forms instead.

counter++;
counter +=1;
counter = counter + 1;

if (counter < 2)
{
counter++;
leftv();
Serial.print(getDistance()); //print the distance that was measured
Serial.println(" in"); //print units after the distance
delay (500);
}

Still does the same thing.

The serial monitor shows the sensor values dropping to crazy values when the motors are on. If I point it at the wall prior to pressing the button the value is 40, however when I press the button the value drops to 3 at the same location. Either way I think the code should force it out of that loop because the counter should be 2 or greater after a few seconds.

This is wrong.

// else if (counter = 4)
else if (counter == 4);

Add a serial print of the value of counter everywhere in the code. The logic of the nested ifs, else ifs, and elses is confusing to me, and I'm not sure what is being actually happening.

Post the complete cleaned up code with some Serial debugging output.

        Serial.print(getDistance());     //print the distance that was measured

But NOT the distance that was measured to trigger the if statement that led to this code being executed.'

      else if (counter = 4)

In the immortal words of AWOL, oops.