0

基本上,我正在制作一个涉及创建智能服务员的 Arduino Uno 项目:

  1. 将在基本的 Arduino 机箱顶部有一个玻璃支架。传感器将是:
    • 4 个红外传感器,每侧一个
    • 2个线传感器,一左一右
    • 光传感器,一个杯架
  2. 它还将具有:
    • 2个伺服电机,左右各一个
    • 1 个 LED 显示杯子状态
  3. 它会:
    • 沿着一条黑线移动,这条黑线将贴在 HOP 的地板上
    • 会感应到四面八方的人,当感应到靠近的物体时会停下来
    • 如果它感觉到玻璃已经被抬起,它将无限期地停止
    • 如果没有感应到玻璃移动,将在 15 秒内重新启动,或者在玻璃放下时重新启动
    • 放下玻璃后,LED 会由绿色变为红色
    • 机器人将继续移动,忽略所有障碍物,直到到达加油站
    • 将停在加油站,灌装机将更换玻璃并按下 Arduino 硬重置按钮

我的代码是:

#include <Servo.h>
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)
    }
  }
}

我想用我的代码做的是避免最后的goto功能。但是,由于只有 2 个功能的限制,我找不到任何方法来重组它。在这种情况下,该goto功能似乎很好,但我不确定。有没有简单的方法来重组它?

4

1 回答 1

0

使 startfresh 作为函数

 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);
    }
  }
}
于 2013-02-19T19:43:48.873 回答