Void loop() isn't looping, Please Help

Hi, I'm new to arduino. I am making a custom Rover project for a University course. And my loop function in my code isn't looping. The LCD shows the data once and then goes blank. I even tried a serial.print() function in the loop function and everything appears only once.
Can anyone tell me what's wrong with my code?
~TIA

#include <AFMotor.h>
#include <SharpIR.h>
#include <MPU6050.h>
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <LiquidCrystal_I2C.h>

#define IR1 A0
#define IR2 A1
#define model 1080

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

SharpIR sensor1(IR1, model);
SharpIR sensor2(IR2, model);
int minx = 10; //minimum sensor1 distance
int maxx = 21; //maximum sensor1 distance
int miny = 12; //minimum sensor2 distance
int maxy = 20; //maximum sensor2 distance
int p = (minx + maxx) / 2;
int q = (miny + maxy) / 2;

char bt = 'S';

int trig = 2;
int echo = 9;
long timeInMicro;
long obs;

MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;

struct MyData
{
  byte X;
  byte Y;
  byte Z;
};

MyData data;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  //BAUD-RATE Setup
  Serial.begin(9600);
  
  //MOTOR-SPEED Setup
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor3.setSpeed(255);
  motor4.setSpeed(255);
  Stop();
  
  //SONAR Setup
  pinMode(2, OUTPUT);
  pinMode(9, INPUT);
  //GYRO Setup
  Wire.begin();
  mpu.initialize();
  
  //DISPLAY Setup
  lcd.init();
  lcd.backlight();
}

void loop()
{
  //MOVEMENT
  bt = Serial.read(); //Bluetooth data
  if (bt == 'F')
  {
    forward();
  }
  if (bt == 'B')
  {
    backward();
  }
  if (bt == 'L')
  {
    left();
  }
  if (bt == 'R')
  {
    right();
  }
  if (bt == 'S')
  {
    Stop();
  }
  
  //SONAR
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  timeInMicro = pulseIn(echo, HIGH);
  obs = ((timeInMicro / 29) / 2); //Sonar Reading in cm
  /*
    delay(500);
    Serial.print("Obstruction = ");  //For Serial Monitor Only
    Serial.println(obs);  //For Serial Monitor Only
  */
  
  //INFRARED
  unsigned long startTime = millis();
  int d_x = sensor1.distance(); //IR1 Reading in cm
  int d_y = sensor2.distance(); //IR2 Reading in cm
  /*
    delay(500);
    Serial.print("Distance1: ");  //For Serial Monitor Only
    Serial.print(d_x);  //For Serial Monitor Only
    Serial.print("Distance2: ");  //For Serial Monitor Only
    Serial.println(d_y);  //For Serial Monitor Only
  */
  
  //GYRO
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  data.X = map(ax, -17000, 17000, 0, 255);
  data.Y = map(ay, -17000, 17000, 0, 255);
  data.Z = map(az, -17000, 17000, 0, 255);
  int x = (data.X - 140) * (90 / 122.5); //Gyro X-axis angle reading
  int y = (data.Y - 125) * (90 / 122.5); //Gyro Y-axix angle reading
  /*
    delay(500);
    Serial.print("Axis X = ");  //For Serial Monitor Only
    Serial.print(data.X);  //For Serial Monitor Only
    Serial.print("  ");  //For Serial Monitor Only
    Serial.print("Axis Y = ");  //For Serial Monitor Only
    Serial.print(data.Y);  //For Serial Monitor Only
    Serial.print("  ");  //For Serial Monitor Only
    Serial.print("Axis Z = ");  //For Serial Monitor Only
    Serial.println(data.Z);  //For Serial Monitor Only
  */
  
  //DISPLAY
  lcd.setCursor(0, 0);
  lcd.print("Tilt = ");
  lcd.print(x);
  lcd.print("\337");
  lcd.setCursor(0, 1);
  lcd.print("Incline = ");
  lcd.print(y);
  lcd.print("\337");
  delay(1000);
  lcd.clear();
  
  //Serial Monitor
  delay(500);
  Serial.print("Obstruction = ");
  Serial.println(obs);
  Serial.print("Distance1: ");
  Serial.print(d_x);
  Serial.print("Distance2: ");
  Serial.println(d_y);
  Serial.print("Axis X = ");
  Serial.print(data.X);
  Serial.print("  ");
  Serial.print("Axis Y = ");
  Serial.print(data.Y);
  Serial.print("  ");
  Serial.print("Axis Z = ");
  Serial.println(data.Z);
  
  //WEIGHT-SHIFT
  if (obs <= 15)
  {
    while (d_y > miny)
    {
      motor2.run(BACKWARD);
    }
  }
  else
  {
    if (x > 70)
    {
      while (d_x > minx)
      {
        motor1.run(FORWARD);
      }
    }
    else if (x < -70)
    {
      while (d_x < maxx)
      {
        motor1.run(BACKWARD);
      }
    }
    else
    {
      if (d_x > p)
      {
        while (d_x != p)
        {
          motor1.run(FORWARD);
        }
      }
      else if (d_x < p)
      {
        while (d_x != p)
        {
          motor1.run(BACKWARD);
        }
      }
    }
    if (y > 50)
    {
      while (d_y < maxy)
      {
        motor2.run(FORWARD);
      }
    }
    else if (y < -50)
    {
      while (d_y > miny)
      {
        motor2.run(BACKWARD);
      }
    }
    else
    {
      if (d_y < q)
      {
        while (d_y != q)
        {
          motor2.run(FORWARD);
        }
      }
      else if (d_y > q)
      {
        while (d_y != q)
        {
          motor2.run(BACKWARD);
        }
      }
    }
  }
}

void forward()
{
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void backward()
{
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}
void   left()
{
  motor3.run(BACKWARD);
  motor4.run(FORWARD);
}
void right()
{
  motor3.run(FORWARD);
  motor4.run(BACKWARD);
}
void   Stop()
{
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}



Welcome to the forum

Why did you start a topic in the Uncategorised category of the forum when its description explicitly tells you not to ?

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

        while (d_y > miny)
        {
            motor2.run(BACKWARD);
        }

Will this while loop ever exit ?

Or any other the other while loops for that matter, when nothing in them changes the variables being tested ?

1 Like

I'm sorry this is first post here that's why I got confused.

thanks a lot I think I understand what I did wrong now, I didn't connect the motors to the motor shield yet, I was just testing the sensors. That's why the motors aren't working yet and that's why the while loop isn't ending. Thanks a lot for helping.

Thanks, I got it, I was testing the sensors so, I didn't connect the motors yet. And so, the loop wasn't ending.

How will connecting the motors cause either d_y or p to change so that the while loop that I used as an example ends ?

The motor is supposed to shift the position of the centre piece according to the reading of the gyro sensor and then, the IR distance readings (d_x & d_y) will change.

Whether or not you have motors hooked up, none of these while loops will ever exit once they're entered. You never update the values in the while condition within the loop. If the condition is true the first time, it will be true forever.

    while (d_y > miny)
    {
      motor2.run(BACKWARD);
    }
      while (d_x > minx)
      {
        motor1.run(FORWARD);
      }
      while (d_x < maxx)
      {
        motor1.run(BACKWARD);
      }
        while (d_x != p)
        {
          motor1.run(FORWARD);
        }
        while (d_x != p)
        {
          motor1.run(BACKWARD);
        }
      while (d_y < maxy)
      {
        motor2.run(FORWARD);
      }
      while (d_y > miny)
      {
        motor2.run(BACKWARD);
      }
        while (d_y != q)
        {
          motor2.run(FORWARD);
        }
        while (d_y != q)
        {
          motor2.run(BACKWARD);
        }

Look again at your code. Once in the while loop the distance reading will not happen

1 Like

Not unless you're updating them inside the while loop they won't.

2 Likes

Then do I have to update all the while loops with the readings?

Yes. Think of the problem as "how long should the motor run before checking the distance again?"

And the best solution is a state machine, as suggested in:

1 Like

Thanks a lot, but I don't think I can pull this off right now, at least not with my current skills.
Can't I write a separate function for taking all the readings and then call that function inside all the while loops?

Do you need the while loops ?

Suppose you changed the while loops into if statements and let loop() do what its name suggests ?

1 Like

I'm sorry, I meant no disrespect, I'm very grateful for your advices but, my coding knowledge is very limited and that too only in C not even C++. And this is my first time making something from scratch.

Thanks, I'll try to implement this idea too.

It is just one way to do it. Other methods have been suggested here so don't discount them

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.