//3.5寸屏需要用到的脚 Reset、D13、D12、D11、D10、D9、D4、D3、D2
//3.5寸之后，还剩下D0 D1 D5 D7 D8 A0 A1 A2 A3 A4 A5
//
//液位传感器 D5
//跌落传感器 D7
//步进电机控制电路 A0 A1 A2
//水阀门12v（继电器） A3
//左碰撞传感器 A4
//右碰撞传感器 A5
//自锁开关（带LED，直接串接在电源线上）
                      
#include <SPI.h>
#include <DmTftSsd2119.h>
#include <DmTouch.h>
#include <utility/DmTouchCalibration.h>
#include <AccelStepper.h>  //本示例程序使用AccelStepper库
DmTftSsd2119 tft = DmTftSsd2119(); 
DmTouch dmTouch = DmTouch(DmTouch::DM_TFT35_107);
DmTouchCalibration calibration = DmTouchCalibration(&tft, &dmTouch);
bool calibrated = false;

const int M2dirpin = A0;     // 方向控制引脚
const int M2steppin = A1;    // 步进控制引脚
const int M2en = A2;  // 使能控制引脚
const int liqLvlpin = 5; //液位检测引脚4
const int trakepin = 7;  //纸杯检测引脚
const int relaypin = A3;  //继电器控制引脚
const int leftCrashpin = A4;  //左碰撞检测引脚
const int rightCrashpin = A5; //右碰撞检测引脚

uint16_t targetSure = 0;
uint8_t cups[] = {0,0,0,0,0};
uint8_t drinks[] = {0,0,0,0,0};
uint8_t cupsNum = 0;
uint8_t drinksNum = 0;
boolean startFlag = 0;
boolean lastDir = 0;//上次碰到的传感器的方向，0是后边向左边移动，1是后边向右边移动
boolean doneFlag = 0;
boolean actionFlag = 0;
boolean cupsAddFlag = 0;
boolean returnFlag = 0;
boolean retryFlag = 0;
boolean breakFlag1 = 0;
boolean breakFlag2 = 0;
boolean breakFlag3 = 0;

uint8_t needDri = 0;
int8_t currentPos = 0;
unsigned long timer1 = 0;
unsigned long timer2 = 0;
unsigned long timer3 = 0;

void menu()
{
  char str1[]="WORLD";
  char str2[]="PEACE!";
  tft.fillRectangle(19, 29, 98, 108, GREEN);//显示充满的方形
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.fillRectangle(218, 29, 297, 108, GREEN);//显示充满的方形
  tft.fillRectangle(19, 137, 98, 216, GREEN);//显示充满的方形
  tft.fillRectangle(118, 137, 197, 216, GREEN);//显示充满的方形
  tft.fillRectangle(218, 137, 297, 216, GREEN);//显示充满的方形
//  tft.drawLine(0,0,0,239,GREEN);
//  tft.drawLine(0,0,319,0,GREEN);
//  tft.drawLine(0,239,319,239,GREEN);
//  tft.drawLine(319,0,319,239,GREEN);
  tft.drawNumber(19+36,29+34,1,1,0);
  tft.drawNumber(118+36,29+34,2,1,0);
  tft.drawNumber(218+36,29+34,3,1,0);
  tft.drawNumber(19+36,137+34,4,1,0);
  tft.drawNumber(118+36,137+34,5,1,0);
  tft.drawString(218+20,137+24,str1);
  tft.drawString(218+16,137+44,str2); 
  }


void yesOrNo()
{
  char str3[]="YES";
  char str4[]="NO";
  tft.fillRectangle(49, 137, 128, 216, BLUE);//显示充满的方形
  tft.fillRectangle(188, 137, 267, 216, RED);//显示充满的方形
  tft.drawString(78,170,str3);
  tft.drawString(220,170,str4); 
  }

void select1()
{
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawNumber(118+36,29+34,1,1,0); 
  }

void select2()
{
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawNumber(118+36,29+34,2,1,0); 
  }

void select3()
{
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawNumber(118+36,29+34,3,1,0); 
  }

void select4()
{
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawNumber(118+36,29+34,4,1,0); 
  }

void select5()
{
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawNumber(118+36,29+34,5,1,0); 
  }

void selectWorldPeace()
{
  char str1[]="WORLD";
  char str2[]="PEACE!";
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawString(118+20,29+24,str1);
  tft.drawString(118+16,29+44,str2); 
  }

void makingDisplay()
{
  char str5[]="Making...";
  tft.fillRectangle(120, 80, 199, 159, GREEN);//显示充满的方形
  tft.drawString(124,110,str5);
  }

void needCups()
{
  char str6[]="Need cups!";
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawString(118,59,str6);
  }

void needDrinks()
{
  char str9[]="Need drinks!";
  tft.fillRectangle(118, 29, 197, 108, GREEN);//显示充满的方形
  tft.drawString(113,59,str9);
  }

void returnOrRetry()
{
  char str7[]="Return";
  char str8[]="Retry";
  tft.fillRectangle(49, 137, 128, 216, BLUE);//显示充满的方形
  tft.fillRectangle(188, 137, 267, 216, RED);//显示充满的方形
  tft.drawString(66,170,str7);
  tft.drawString(208,170,str8); 
  }

void done()
{
  char str10[]="Done!";
  tft.fillRectangle(120, 80, 199, 159, GREEN);//显示充满的方形
  tft.drawString(140,110,str10);
  }

void resetDisplay()
{
  char str11[]="Reset...";
  tft.fillRectangle(120, 80, 199, 159, GREEN);//显示充满的方形
  tft.drawString(128,110,str11);
  }
  
void worldPeaceAdd1()
{
  char str1[]="WORLD";
  char str2[]="PEACE!+1";
  tft.fillRectangle(120, 80, 199, 159, GREEN);//显示充满的方形
  tft.drawString(120+20,80+24,str1);
  tft.drawString(120+8,80+44,str2); 
  }

void tft_touchInit()
{
  dmTouch.setCalibrationMatrix(calibration.getDefaultCalibrationData((int)DmTouch::DM_TFT35_107));
  tft.init();
  dmTouch.init();
  }

void stepperInit()
{
  pinMode(M2dirpin,OUTPUT);
  pinMode(M2steppin,OUTPUT);
  pinMode(M2en,OUTPUT);
  }

void liquidLevelInit()
{
  pinMode(liqLvlpin,INPUT);
  }

void trackerInit()
{
  pinMode(trakepin,INPUT);
  }

void relayInit()
{
  pinMode(relaypin,OUTPUT);
  }

void crashInit()
{
  pinMode(leftCrashpin,INPUT);
  pinMode(rightCrashpin,INPUT);
  }

void clearScreen()
{
  tft.clearScreen();
  }

void reset()
{
  resetDisplay();
  resetStepper();
  }
  
void resetStepper()
{
  cheakLeftCrash();
  delay(1000);
  reback();
  } 

void cheakLeftCrash()//开机复位，向左移动，并碰到左侧的碰撞传感器
{
  digitalWrite(M2en,LOW); //低电平使能
  delayMicroseconds(2);
  digitalWrite(M2dirpin,LOW); //步进电机方向控制 LOW向左 HIGH向右
  while(1)
  {
    if(digitalRead(leftCrashpin) == 0)
    {
      lastDir = 1;//表示此刻在最左端，之后要向右移动
      break;
      }
    digitalWrite(M2steppin,LOW);
    delayMicroseconds(2);
    digitalWrite(M2steppin,HIGH);  //上升沿步进 
    delay(1); 
    }
  digitalWrite(M2en,HIGH);
  }

void cheakRightCrash()//向右移动，并碰到右侧的碰撞传感器
{
  digitalWrite(M2en,LOW); //低电平使能
  delayMicroseconds(2);
  digitalWrite(M2dirpin,HIGH); //步进电机方向控制
  while(1)
  {
    if(digitalRead(rightCrashpin) == 0)
    {
      lastDir = 0;//表示此刻在最右端，之后要向左移动
      break;
      }
    digitalWrite(M2steppin,LOW);
    delayMicroseconds(2);
    digitalWrite(M2steppin,HIGH);  //上升沿步进 
    delay(1); 
    }
  digitalWrite(M2en,HIGH);
  }

void reback()//回退距离为退至正对杯子
{
  uint16_t j;
  digitalWrite(M2en,LOW); //低电平使能
  delayMicroseconds(2);
  if(lastDir == 1){   //
    digitalWrite(M2dirpin,HIGH);
  }
  else {
    digitalWrite(M2dirpin,LOW);
  }
  for(j=0;j<=25;j++){
  digitalWrite(M2steppin,LOW);
  delayMicroseconds(2);
  digitalWrite(M2steppin,HIGH);  //上升沿步进
  delay(1);
    }
  currentPos = 0;
  digitalWrite(M2en,HIGH);
  }

void judgeTouch1(uint16_t x,uint16_t y){
  if(y >= 137 && y <= 216){
    if(x >= 49 && x <= 128){
      returnFlag = 1;
    }
    else if(x >= 188 && x <= 267){
      retryFlag = 1;
    }
  }
  
}
void scanTouch1(){
  uint16_t x,y;
  bool touched = true;
  if (dmTouch.isTouched()) {
  dmTouch.readTouchData(x,y,touched);//读取触点的x、y坐标
  judgeTouch1(x,y);
  }
}

void scanTouch(){
  uint16_t x,y;
  bool touched = true;
  if (dmTouch.isTouched()) {
  dmTouch.readTouchData(x,y,touched);//读取触点的x、y坐标
  judgeTouch(x,y);  
 }
}

void judgeTouch(uint16_t x,uint16_t y)
{
  if(y>=29&&y<=108){
    if(x>=19&&x<=98){
      targetSure = 1; 
    }
    else if(x>=118&&x<=197){
      targetSure = 2;
    }
    else if(x>=218&&x<=297){
      targetSure = 3;
    }
  }
  else if(y>=137&&y<=216){
    if(x>=19&&x<=98){
      targetSure = 4;
    }
    else if(x>=118&&x<=197){
      targetSure = 5;
    }
    else if(x>=218&&x<=297){
      targetSure = 6;
    }
  }  
}

void sureDisplay()
{
  uint16_t i, n ;
  bool touched = true;
  
  if(targetSure != 0)
  {
    clearScreen();
    switch(targetSure){
      case 1:
              select1();
              break;
      case 2:
              select2();
              break;
      case 3:
              select3();
              break;
      case 4:
              select4();
              break;
      case 5:
              select5();
              break;
      case 6:
              selectWorldPeace();
              break;
      default:
              break;
  } 
  yesOrNo(); 
  while(1){
    if (dmTouch.isTouched()) {
    dmTouch.readTouchData(i,n,touched);//读取触点的x、y坐标
    if(n>=137&&n<=216){
      if(i>=49&&i<=128){
        startFlag = 1;
        break;
      }
      else if(i>=188&&i<=267){
        targetSure = 0;
        clearScreen();
        menu();
        break;
      }
    }
  }
  }
}
}

void runOneDistance(){
  uint16_t j = 0;
  for(j=0;j<=560;j++){
  digitalWrite(M2steppin,LOW);
  delayMicroseconds(2);
  digitalWrite(M2steppin,HIGH);  //上升沿步进
  delay(1);
}
}

void runToOtherEnd(){ //从上一次正对着杯子的位置开始，依次移动4次相同的杯子间距，然后最后再移动一小段距离，去碰碰撞传感器，碰完后，再回退一段距离，正对着杯子正中
  uint16_t j;
  digitalWrite(M2en,LOW); //低电平使能
  delayMicroseconds(2);
  if(lastDir == 0){
  digitalWrite(M2dirpin,LOW); //步进电机方向控制
  }
  else {
  digitalWrite(M2dirpin,HIGH); //步进电机方向控制  
  }
  if(lastDir == 0){
    drinks[4] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[4] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
  else {
    drinks[0] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[0] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
  
  runOneDistance();
  currentPos++;
  delay(1000);
  if(lastDir == 0){
    drinks[3] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[3] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
  else {
    drinks[1] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[1] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
  
  runOneDistance();
  currentPos++;
  delay(1000);
    drinks[2] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[2] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
    
  runOneDistance();
  currentPos++;
  delay(1000);
    if(lastDir == 0){
    drinks[1] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[1] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
    else {
    drinks[3] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[3] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }

  runOneDistance();
  currentPos++;
  delay(1000);
  if(lastDir == 0){
    drinks[0] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[0] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
  else {
    drinks[4] = digitalRead(liqLvlpin);//将该位置的液位信息存入数组
    cups[4] = digitalRead(trakepin);//将该位置的杯子有无情况存入数组
  }
  
  if(lastDir == 0){
    cheakLeftCrash();
  }
  else {
    cheakRightCrash();
  }
  delay(1000);
  reback();
  digitalWrite(M2en,HIGH);
  Serial.print(cups[0]);
  Serial.print(cups[1]);
  Serial.print(cups[2]);
  Serial.print(cups[3]);
  Serial.println(cups[4]);
  Serial.print(drinks[0]);
  Serial.print(drinks[1]);
  Serial.print(drinks[2]);
  Serial.print(drinks[3]);
  Serial.println(drinks[4]);
}


void cheakFlag(){
  if(startFlag == 1){
    actionJudge(targetSure);
  }
}

void actionJudge(uint16_t num){
  switch(num){
    case 6:
            clearScreen();
            worldPeaceAdd1();
            delay(3000);
            allReset();
            clearScreen();
            menu();
            break;
    default:
            clearScreen();
            makingDisplay();
            runToOtherEnd();
            cheakCups_Drinks();
            break;
  }  
}


void judgeResult(){
  while(1){
  if(doneFlag == 1){
    Serial.println("done");
    clearScreen();
    done();
    delay(3000);
    clearScreen();
    menu();
    allReset();
    break;
  }
  else if(cupsAddFlag == 1){
    Serial.println("addcups");
    clearScreen();
    needCups();
    returnOrRetry();
    while(1){
      scanTouch1();
      if(returnFlag == 1){
        clearScreen();
        menu();
        allReset();
        breakFlag1 = 1;
        break;
      }
      if(retryFlag == 1){
        retryFlag = 0;
        cupsAddFlag = 0;
        clearScreen();
        makingDisplay();
        runToOtherEnd();
        cheakCups_Drinks();
        break;
      }
    }
    if(breakFlag1 == 1){
      breakFlag1 = 0;
      break;
    }
  }
  else if(actionFlag == 1){
    Serial.println("action");
    action();
  }
  else {
    break;
  }
 }
}

void moveToEnd(){
  if(lastDir == 0){
    cheakLeftCrash();
  }
  else{
    cheakRightCrash();
  }
  reback();
}

void action(){
  for(;needDri > 0;needDri--){
    uint16_t j;
    boolean liqLvl = 0;
    boolean trake = 0;
    boolean result = 0;
    if(lastDir == 0){
      digitalWrite(M2dirpin,LOW); //步进电机方向控制
      }
    else{
      digitalWrite(M2dirpin,HIGH);
    }
    liqLvl = digitalRead(liqLvlpin);
    trake = digitalRead(trakepin);
    if(liqLvl == trake){
      result = 1;
    }
    else {
      result = 0;
    }
      while(result)//如果这个位置上有水或没有杯子，就移动到下一个位置;移动4次，不用管是否要去碰撞那个碰撞传感器，下次cheak时再进行校准
        {

          if(currentPos < 4){
            digitalWrite(M2en,LOW); //低电平使能
            delayMicroseconds(2);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            runOneDistance(); 
            delay(1000);
            liqLvl = digitalRead(liqLvlpin);
            trake = digitalRead(trakepin);
            if(liqLvl == trake){
              result = 1;
            }
            else {
              result = 0;
            }
            Serial.println(digitalRead(liqLvlpin));
            Serial.println(digitalRead(trakepin));
            Serial.println("runonedistance");
            Serial.println(currentPos);
            digitalWrite(M2en,HIGH);
            currentPos++;
            if(currentPos == 4){
              if(digitalRead(liqLvlpin) == 1){//如果在最末端发现还是有水，说明出错了，回到菜单界面
             allReset();
             breakFlag3 = 1;
             break;
             Serial.println("break1");
              }
              else{
                break; //如果最末端没有水，则退出掺水
                Serial.println("break2");
              }
            }
          }
          else{
            break;//如果当前位置为4了，就不移动步进电机，
            Serial.println("break3");
          }
        }
        if(breakFlag3 == 1){
          breakFlag3 = 0;
          break;
          Serial.println("break4");
        }
  
          timer1 = millis();
          while(1){
            digitalWrite(relaypin,HIGH);//打开水阀
            if(digitalRead(liqLvlpin)){ //如果检测到水已经倒满，那么就关闭水阀，跳出循环，然后移动到下一个位置去
              digitalWrite(relaypin,LOW);
              delay(500);
              break;
            }
            timer2 = millis();
            timer3 = timer2 - timer1;
            if(timer3 >= 30000){
              digitalWrite(relaypin,LOW);
              clearScreen();
              needDrinks();
              returnOrRetry();
              while(1){
                scanTouch1();
                if(returnFlag == 1){
                  allReset();
                  breakFlag1 = 1;
                  breakFlag2 = 1;
                  break;
                }
                if(retryFlag == 1){//跳出当前等待return和retry的选择的循环，回到上一级循环，打开水阀对当前位置进行倒水的
                  retryFlag = 0;
                  timer1 = millis();
                  clearScreen();
                  makingDisplay();
                  break;
                }
              }
            }
            if(breakFlag1 == 1){
            breakFlag1 = 0;
            break;
            }
          }
    if(breakFlag2 == 1){
      break;
    }
  }
  moveToEnd();
  Serial.println("moveToEnd");
  if(breakFlag2 == 0){
    clearScreen();
    done();
    delay(3000);
  }
    allReset();
    clearScreen();
    menu();
}

void allReset(){
    uint8_t i;
    targetSure = 0;
    cupsNum = 0;
    drinksNum = 0;
    startFlag = 0;
    doneFlag = 0;
    actionFlag = 0;
    cupsAddFlag = 0;
    for(i=0;i++;i<5){
      cups[i] = 0;
      drinks[i] = 0;
    }
    returnFlag = 0;
    retryFlag = 0;
    needDri = 0;
    breakFlag2 = 0;
}

void cheakCups_Drinks(){
  uint8_t i;
  for(i=0;i<5;i++){
    if(cups[i] > 0){
      cupsNum++;
    }
    if(drinks[i] > 0){
      drinksNum++;
    }
  }
  Serial.println(cupsNum);
  Serial.println(drinksNum);
  Serial.println(targetSure);
  if(drinksNum >= targetSure)
  {
    doneFlag = 1;
    }
  if(drinksNum < targetSure && cupsNum >= targetSure)
  {
    actionFlag = 1;
    needDri = targetSure - drinksNum;
    Serial.println(needDri);
    }
  if(cupsNum < targetSure)
  {
    cupsAddFlag = 1;
    }
}




void setup()
{
  Serial.begin(9600);
  tft_touchInit();//初始化tft和触摸
  stepperInit();//设置步进电机的引脚输出模式，并将使能引脚启动使能
  liquidLevelInit();//液位传感器引脚模式设置
  trackerInit();//追踪传感器引脚模式设置
  relayInit();//继电器引脚模式设置
  crashInit();//碰撞传感器引脚模式设置
  clearScreen();//tft清屏
  reset();//屏幕显示“reset”，步进电机移至最左端并碰到碰撞传感器，并回退至正对杯子
  clearScreen();//完成重置后，进行清屏操作
  menu();//显示菜单页面
}

void loop()
{
  scanTouch();//在菜单显示页面等待触摸
  //Serial.println("here0");
  sureDisplay();//如果检测到targetSure值不等于0，则进入该函数内部进行判断。如果targetSure是1-5或者worldpeace，则显示相应的画面，画面下方提供两个选项“YES”“NO”。
                //这里会死等着触摸命令,如果是“YES”，那么就会将startFlag置1，cheakFlag()会进行判断这个值然后动作。如果是“NO”，那么就会清除掉targetSure的值，并且清屏显示menu
  //Serial.println("here1");
  cheakFlag();//如果startFlag置1，那么进去判断targetSure的值，如果是6，那么就画面显示“worldpeace+1”，清空用到的变量值，然后清屏显示menu；如果是1-5，则需要执行倒饮料前判断。
  //Serial.println("here2");
  judgeResult();//倒饮料操作，先会屏幕显示“making...”，然后将步进电机移动到另一头的碰撞传感器，并回退至正对杯子。移动过程中，会检测相应的5个位置的杯子和饮料的情况，并将两组数值记录在两个数组中。
  //Serial.println("here3");//检查杯子和饮料的数量，如果饮料数量大于等于需要制作的杯数，则显示“done”，并清空所有改变了的变量，并清屏显示menu
              //如果杯子数量少于需要制作的杯数，则显示“needcups”，下方给出两个选项“return”，“retry”，并死循环等着触摸操作，如果选择了return，那么清空所有变量值，并清屏显示menu；如果选择retry，就将移动到另一头，重新统计杯子和饮料的数量，进行比较；
              //如果杯子数量大于等于制作杯数，饮料杯数又小于制作杯数，就执行倒饮料。检测当前位置是否有杯子与饮料，如果有杯子无饮料，则开始倒饮料，如果倒超过一定时间，还未检测到倒满信号，则说明饮料已经没有了
              //需要补充饮料，画面显示“needdrinks”，下方显示“return”“retry”，如果选择了return，则清空变量，清屏显示menu，回loop循环。如果选择“retry”，则再次接着倒，如果超过一定时间还未检测到倒满则重复。
              //如果检测到倒满了，则关闭阀门，移动步进电机到下一个位置上，然后重复检测杯子和饮料，判断是否倒饮料。倒一杯饮料，则需要制作的杯数减1，直到需要制作的杯数变成0，然后就清空所有值，显示“done”，清屏显示menu，回loop。
  
  }
