Efforts to Eliminate the Use of Goto Function in Arduino Design for Developing an Intelligent Waiter

The robot will follow a black line on the floor of the HOP and detect nearby individuals, pausing when it senses their presence. If it detects that the glass has been lifted, it will come to a complete stop and will only resume movement 15 seconds after it no longer senses any glass movement or when the glass is replaced. Once the glass is put down, the LED indicator will switch from green to red. The robot will continue moving, disregarding any obstacles in its path, until it reaches the filling station. At the filling station, the filler will replace the glass and press the Arduino hard reset button. To avoid the function at the end, one solution is to create a startfresh function. The program contains several lengthy segments that execute based on an “IF” statement.


Question:

Essentially, I am working on an Arduino Uno venture that entails developing an intelligent server that:

  1. The basic Arduino chassis will come equipped with a glass holder on top, alongside three sensors namely
  2. 4 Infrared Sensors, one on each side
  3. ,

  4. 2 Line Sensors, one left and one right
  5. and

  6. Light Sensor, one the cup holder
  7. .

  8. Additionally, the following features will be included:
  9. 2 Servo Motors, one left and right
  10. 1 LED showing cup status
  11. The following tasks will be performed:
  12. Move along a black line that will be taped on the floor of the HOP
  13. Will sense people on all sides, and pause when it senses close bodies
  14. Will stop indefinitely if it senses that the glass has been lifted
  15. Will start again in 15 seconds if it doesn’t sense movement of glass OR will start again when glass is put down
  16. After glass has been put down, LED will turn from green to red
  17. Robot will continue to move, ignoring all obstacles, until it reaches filling station
  18. Will stop at filling station, where filler will replace glass and press Arduino hard
    reset button

My Code is:

#include 
int redLED = 5;
int yellowLED = 6;
int greenLED = 7;
int cup1 = 0;
int picked = 1;
int lineright = 0;
int lineleft = 0;
int sensorright = 0;
int sensorleft = 0;
int sensorfront = 0;
int sensorback = 0;
int sensorpinright = 1;
int sensorpinleft = 2;
int sensorpinfront = 3;
int sensorpinback = 4;
Servo servoright;
Servo servoleft;
void setup ()
{
  servoright.attach (9);
  servoleft.attach (10);
  pinMode (redLED, OUTPUT);
  pinMode (greenLED, OUTPUT);
  pinMode (greenLED, OUTPUT);
}
void loop()
{
   sensorright = digitalRead (sensorpinright);
   sensorleft = digitalRead (sensorpinleft);
   sensorfront = digitalRead (sensorpinfront);
   sensorback = digitalRead (sensorpinback);
   lineleft = analogRead (1);
   lineright = analogRead (2);
   cup1 = analogRead (3);
 if (sensorright < 0 && sensorleft < 0 && sensorfront < 0 && sensorback < 0)
  {
    digitalWrite (led1, GREEN);
    startfresh:
    if (lineleft < 800)
    {
      servoleft.write (180);
      servoright.write (180);
    }
    else
    {
      if (lineright < 800)
      {
        servoright.write (0);
        servoleft.write (0);
      }
      else
      {
        servoright.write (0);
        servoleft.write (180);
      }
    }
  }
  else
  {
    for (int i=0;i<5;i++)
    {
      servoright.write (93);
      servoleft.write (93);
      if (picked < 1)
      {
        while (cup1<500)
        {
          digitalWrite (yellowLED, HIGH);
          picked = 1;
        }
      }
      else
      {
        digitalWrite (yellowLED, LOW);
        digitalWrite (redLED, HIGH);
        goto startfresh;
      }
      delay (2000)
    }
  }
}

My code requires me to avoid using the

goto

function at the end, but I am struggling to restructure it with only two functions. I am uncertain if the

goto

function is suitable in this scenario. Can you suggest a straightforward method to reorganize this?




Solution:

Make startfresh as function

 if (sensorright < 0 && sensorleft < 0 && sensorfront < 0 && sensorback < 0)
  {
    digitalWrite (led1, GREEN);
    startfresh();
  }
  else
  {
    for (int i=0;i<5;i++)
    {
      servoright.write (93);
      servoleft.write (93);
      if (picked < 1)
      {
        while (cup1<500)
        {
          digitalWrite (yellowLED, HIGH);
          picked = 1;
        }
        delay (2000);
      }
      else
      {
        digitalWrite (yellowLED, LOW);
        digitalWrite (redLED, HIGH);
        startfresh();
      }
    }
  }
}
void startfresh() {
  if (lineleft < 800)
  {
    servoleft.write (180);
    servoright.write (180);
  }
  else
  {
    if (lineright < 800)
    {
      servoright.write (0);
      servoleft.write (0);
    }
    else
    {
      servoright.write (0);
      servoleft.write (180);
    }
  }
}

Frequently Asked Questions