#include <SoftwareSerial.h>
#include <Wire.h>
#include "Timer.h"
#include <LiquidCrystal.h>
SoftwareSerial I2CBT(8,9); //Tx & Rx
const byte Set = 10,  Next= 11, up = 12, down = 13;
//----------------------------------------------------LCD
byte Seting,Nexting,uping,downing = 0;// variable for reading the pushbutton status
int menuChg,SetMode,SetMenuSW=0;//Lcd menu
int Norlin2_hour,Norlin2_minu,Norlin2_sec=0;//LCD Normal line2 desplay
byte Lcd_lin2_En=1;
byte lcd_blink_en=0;
//----------------------------------------------------Other
byte serialA;
int insize; 
int count;
int I2CBT_WriteFlag;
byte cmmd[50]; 
int WaterLevel;
String WaterTemp;
int day,hour,minu,sec=0;//conter
int Fed_en,Wat_en,Lig_en=0; //Output EN
int FeedSpace,FedSpinTime,WatLigON_Max;//Output
int WatLigOF_Max,WatLigON_Min,WatLigOF_Min,LigSpace,LigSpinTime;
int WaterSensor; //Water
int WatTimer,WatTime_day,WatTime_hour,WatTime_minu,WatTime_sec;
int WatLsTime_day,WatLsTime_hour;
int FeedTimer,SpiningTimer,FedRecipro;//Feed
int LigTimer,LigRecipro,LigSpiningTimer; //Light
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
Timer t;
void setup() {
  pinMode(Set, INPUT);
  pinMode(Next, INPUT);  
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  Serial.begin(9600); 
  I2CBT.begin(115200);
  delay(1000);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.print("Connecting");
  I2CBT.write("A");//A = Send Require
  t.every(2000, LCD_lin2); //call LCD function
  t.every(100, LCD_main); //call LCD function
  t.every(100, BlueTooth);
  t.every(500, BTRead);
  t.every(100, Debounce_main); //call LCD function
}
void loop() {
  t.update();
}
int ReadEN=0;
void BlueTooth(){
  if (I2CBT.available()) {
    ReadEN=1;
  }
}
int delay1sac;
String inputString = "";          //存放讀取進來的字元字串
boolean stringComplete = false;  //字串是否完成旗號
void BTRead(){
  if (ReadEN==1){
    if (I2CBT_WriteFlag==1){
      if (I2CBT.available()){//假如有任何資料進來
        char inChar = I2CBT.read();  //一次讀取一個字元
        inputString += inChar;                //一個字元一個字元加起來
        if (inChar == '\n') {                   //假如字元等於結束字元’\n’判定字串已接收結束
          stringComplete = true;            //設定字串結束旗號
        }
      }
      else{
        if (delay1sac==4){
          Serial.println("");
          Serial.print("inputString = ");
          Serial.println(inputString);        
          Serial.print("stringComplete = ");
          Serial.println(stringComplete);        
          inputString = "";
          stringComplete = false;
          I2CBT.write("A");//A = Send Require
          delay1sac=0;
          I2CBT_WriteFlag=0;
        }
        else{
          delay1sac++;
        }
      }
    }
    else if (I2CBT_WriteFlag==2){
      if (I2CBT.available()){//假如有任何資料進來
        char inChar = I2CBT.read();  //一次讀取一個字元
        inputString += inChar;                //一個字元一個字元加起來
        if (inChar == '\n') {                   //假如字元等於結束字元’\n’判定字串已接收結束
          stringComplete = true;            //設定字串結束旗號
        }
      }
      else{
        if (delay1sac==4){
          Serial.println("");
          Serial.print("inputString = ");
          Serial.println(inputString);        
          Serial.print("stringComplete = ");
          Serial.println(stringComplete);        
          inputString = "";
          stringComplete = false;
          I2CBT.write("B");//B = Send Require
          delay1sac=0;
          I2CBT_WriteFlag=0;
        }
        else{
          delay1sac++;
        }
      }
    }
    else{
      char TheBlueToothData;
      TheBlueToothData=char(I2CBT.read());
      Serial.println("");
      Serial.print("TheBlueToothData = ");
      Serial.println(TheBlueToothData);
      switch(Seting) {
        case 0: //normal mode
          if (TheBlueToothData=='A'){
            insize=I2CBT.available(); //讀取藍牙訊息
            Serial.print("input size = ");
            Serial.println(insize);
            if (insize>=9 && insize<=50 ){
              count=0;
              for (int i=0; i<insize; i++){
              count++;
              Serial.print(cmmd[count]=(I2CBT.read()));
              Serial.print(" ");
              }
            }
          }
          else{
            Serial.println("cmmd:");
            // 讀取所有資料,並顯示在 LCD
            for(int j=0;j<=count;j++){
              Serial.print(char(cmmd[j]));
              Serial.print(" ");
            }
            //---------------------------------------------------------------------------------------
            //cmmd address+1
            //1.現在時間 day3 hour2 minu2 sec2
            //2.下次餵食時間
            //3.下次開燈(抽水馬達)的時間
            //4.上次補水的時間
            //5.水位
            //6.水溫
            //7.輸出智能 Fed_en,Wat_en,Lig_en
            //8.餵食設定 FeedSpace=8,FedSpinTime=15
            //9.補水設定 WatLigOF_Min=250,WatLigOF_Max=550,WatLigON_Min=0,WatLigON_Max=500
            //10.抽水馬達設定 LigRecipro=3600,LigSpace=4,LigSpinTime=4
            //---------------------------------------------------------------------------------------
            //第一筆:現在時間 [2位數][1位數][1位數][1位數]
            day=cmmd[1]*255+cmmd[2];
            hour=cmmd[3];
            minu=cmmd[4];
            sec=cmmd[5];
            if (sec==0){lcd.clear();}
            //第二筆:下次餵食時間 [2位數]max:43200 
            FedRecipro=cmmd[6]*255+cmmd[7];
            //第三筆:下次開燈(抽水馬達)的時間 [2位數]max:43200 
            LigRecipro=cmmd[8]*255+cmmd[9];
            //第四筆:上次補水的時間 [1位數][1位數][1位數][1位數]
            WatTime_day=cmmd[10];
            WatTime_hour=cmmd[11];
            WatTime_minu=cmmd[12];
            WatTime_sec=cmmd[13];
            WatLsTime_day=cmmd[14];
            WatLsTime_hour=cmmd[15];
            //第五筆:水位 [2位數]
            WaterLevel=cmmd[16]*255+cmmd[17];
            //第六筆:水溫 [1位數] [,] [1位數]
            WaterTemp=String(cmmd[19])+'.'+String(cmmd[20]);
            //第七筆:輸出智能 Fed_en,Wat_en,Lig_en =>421編碼共用
            Fed_en=(int)(cmmd[21]/4);
            Wat_en=cmmd[21]%4/2;
            Lig_en=cmmd[21]%2;
            //第八筆:餵食設定 FeedSpace=8,FedSpinTime=15
            FeedSpace=cmmd[22];
            FedSpinTime=cmmd[23];
            //第九筆:補水設定 WatLigOF_Min=250,WatLigOF_Max=550,WatLigON_Min=0,WatLigON_Max=500
            WatLigOF_Min=cmmd[24];
            WatLigOF_Max=cmmd[25];
            WatLigON_Min=cmmd[26];
            WatLigON_Max=cmmd[27];
            //第十筆:抽水馬達設定 LigRecipro=3600,LigSpace=4,LigSpinTime=4
            LigSpace=cmmd[28];
            LigSpinTime=cmmd[29];
            //
            Serial.println("I2CBT.write(A)");
            Initalize();
            I2CBT.write("A");//A = Send Require
          }
          break;
        case 255: //setting mode
          if (TheBlueToothData=='B'){
            I2CBT.write("B");//A = Send Require
            byte Data[10];
            //---------------------------------------------------------------------------------------
            //Data 編碼2位數極限65025 
            //1.餵食設定 FeedSpace=8,FedSpinTime=15
            //2.補水設定 WatLigOF_Min=250,WatLigOF_Max=550,WatLigON_Min=0,WatLigON_Max=500
            //3.抽水馬達設定 LigRecipro=3600,LigSpace=4,LigSpinTime=4
            //---------------------------------------------------------------------------------------
            //第一筆:餵食設定 FeedSpace=8,FedSpinTime=15
            Data[0]=FeedSpace;
            Data[1]=FedSpinTime;
            //第二筆:補水設定 WatLigOF_Min=250,WatLigOF_Max=550,WatLigON_Min=0,WatLigON_Max=500
            Data[2]=WatLigOF_Min;
            Data[3]=WatLigOF_Max;
            Data[4]=WatLigON_Min;
            Data[5]=WatLigON_Max;
            //第三筆:抽水馬達設定 LigSpace=4,LigSpinTime=4
            Data[6]=LigSpace;
            Data[7]=LigSpinTime;
            //
            for(int j=0;j<=10;j++){
              Serial.print(Data[j]);
              Serial.print(" ");
              I2CBT.write(Data[j]);
            }
          }
        break;
      }
    }
  }
}
void Initalize(){
  count=0;
  insize=0;
}
void LCD_main() {
  // 將游標設到 column 0, line 0
  lcd.setCursor(0, 0); 
  switch(Seting) {
  case 0:
    lcd.noBlink();
    Lcd_lin2_En=1;
    LCD_Hompage();
    break;
  case 255:
    Lcd_lin2_En=0;
    Setting();
    LCD_Setpage();
    break;
  }
  if (Lcd_lin2_En==1){
    // 將游標設到 column 0, line 1
    lcd.setCursor(0, 1); 
    lcd.print("                ");
    lcd.setCursor(0, 1); 
    switch(menuChg) {
    case 0:
      NorFed_Disp();
      break;
    case 1:
      NorWat_Disp();
      break;
    case 2:
      NorLig_Disp();
      break;
    case 3:
      NorWatLin_Disp();
      break;
    }
    if (menuChg<=2){
      switch(menuChg) {
      case 0://Feed
        Norlin2_hour=FedRecipro/60 /60;
        Norlin2_minu=FedRecipro/60 %60;
        Norlin2_sec =FedRecipro %60 %60;
        break;
      case 1://Water
        Norlin2_hour=0;
        Norlin2_minu=WatLsTime_day;
        Norlin2_sec=WatLsTime_hour;
        break;
      case 2://Lig
        Norlin2_hour=LigRecipro/60 /60;
        Norlin2_minu=LigRecipro/60 %60;
        Norlin2_sec=LigRecipro %60 %60;
        break;
      }
      lcd.setCursor(7, 1);
      lcd.print("_");
      lcd.print(Norlin2_hour);
      lcd.setCursor(10, 1);
      lcd.print(":");
      lcd.print(Norlin2_minu);
      lcd.setCursor(13, 1);
      lcd.print(":");
      lcd.print(Norlin2_sec);
    }
    else{
      lcd.print(WaterLevel);
      lcd.print(" Tm:");
      lcd.print(WaterTemp);
    }
  }
}
void LCD_Hompage() {
  lcd.print("Day:");
  lcd.print(day);
  lcd.setCursor(7, 0);
  lcd.print("_");
  lcd.print(hour);
  lcd.setCursor(10, 0);
  lcd.print(":");
  lcd.print(minu);
  lcd.setCursor(13, 0);
  lcd.print(":");
  lcd.print(sec);
}
void  Setting(){ //reflash time = 100ms
  if (Nexting==1){
    lcd.clear();
    if (SetMode==7){
      SetMode=0;
    }
    else{
      SetMode=SetMode+1;
    }
  }
  SetMenuSW=SetMode/2;
  switch(SetMode){
  case 0: //hour
    if (FeedSpace>=9 && (uping==1) || (FeedSpace<=0)&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        FeedSpace=FeedSpace+1;
      }
      else if (downing==1){
        FeedSpace=FeedSpace-1;
      }
    }
    break;
  case 1://sec 
    if (FedSpinTime>=59 && (uping==1)|| FedSpinTime<=0&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        FedSpinTime=FedSpinTime+1;
      }
      else if (downing==1){
        FedSpinTime=FedSpinTime-1;
      }
    }
    break;
  case 2://
    if (WatLigON_Max>=999 && (uping==1) || WatLigON_Max<=WatLigON_Min+10&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        WatLigON_Max=WatLigON_Max+10;
      }
      else if (downing==1){
        WatLigON_Max=WatLigON_Max-10;
      }
    }
    break;
  case 3:
    if (WatLigOF_Max>=999 && (uping==1) || WatLigOF_Max<=WatLigOF_Min+10&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        WatLigOF_Max=WatLigOF_Max+10;
      }
      else if (downing==1){
        WatLigOF_Max=WatLigOF_Max-10;
      }
    }
    break;
  case 4:
    if (WatLigON_Min>=WatLigON_Max-10 && (uping==1) || WatLigON_Min<=0&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        WatLigON_Min=WatLigON_Min+10;
      }
      else if (downing==1){
        WatLigON_Min=WatLigON_Min-10;
      }
    }
    break;
  case 5:
    if (WatLigOF_Min>=WatLigOF_Max-10 && (uping==1) || WatLigOF_Min<=0&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        WatLigOF_Min=WatLigOF_Min+10;
      }
      else if (downing==1){
        WatLigOF_Min=WatLigOF_Min-10;
      }
    }
    break;
  case 6://hour
    if (LigSpace>=23 && (uping==1) || LigSpace<=0&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        LigSpace=LigSpace+1;
      }
      else if (downing==1){
        LigSpace=LigSpace-1;
      }
    }
    break;
  case 7://hour
    if (LigSpinTime>=23 && (uping==1) || LigSpinTime<=0&&(downing==1)){
      lcd.blink();
    }
    else{
      if (uping==1){
        LigSpinTime=LigSpinTime+1;
      }
      else if (downing==1){
        LigSpinTime=LigSpinTime-1;
      }
    }
    break;
  } 
}
void LCD_Setpage() { 
  switch(SetMenuSW){
  case 0 :
    SetFed_Disp();
    break;
  case 1:
    SetWat_Disp();
    break;
  case 2:
    SetWat2_Disp();
    break;
  case 3:
    SetLig_Disp();
    break;
  }
  lcd_blink_en=lcd_blink_en+1; //游標閃爍智能
  if (lcd_blink_en%2==0){
    switch(SetMode){
    case 0:
      lcd.setCursor(10, 0);
      break;
    case 1:
      lcd.setCursor(10, 1);
      break;
    case 2:
      lcd.setCursor(13, 0);
      break;
    case 4:
      lcd.setCursor(13, 0);
      break;
    case 3:
      lcd.setCursor(13, 1);
      break;
    case 5:
      lcd.setCursor(13, 1);
      break;
    case 6:
      lcd.setCursor(9, 0);
      break;
    case 7:
      lcd.setCursor(9, 1);
      break;
    }  
    lcd.blink();
  }
  else{
    lcd.noBlink();
  }
}
void SetFed_Disp(){
  lcd.print("FeedSpace:");
  lcd.print(FeedSpace);
  lcd.setCursor(12, 0);
  lcd.print("Hour");
  lcd.setCursor(0, 1);
  lcd.print("SpinTime :");
  lcd.print(FedSpinTime);
  lcd.setCursor(13, 1);
  lcd.print("Sec");
}
void SetWat_Disp(){
  lcd.print("WatLigON_Max:");
  lcd.print(WatLigON_Max);
  lcd.setCursor(0, 1);
  lcd.print("WatLigOF_Max");
  lcd.setCursor(12, 1);
  lcd.print(":");
  lcd.print(WatLigOF_Max);
}
void SetWat2_Disp(){
  lcd.print("WatLigON_Min:");
  lcd.print(WatLigON_Min);
  lcd.setCursor(0, 1);
  lcd.print("WatLigOF_Min");
  lcd.setCursor(12, 1);
  lcd.print(":");
  lcd.print(WatLigOF_Min);
}
void SetLig_Disp(){
  lcd.print("LigSpace:");
  lcd.print(LigSpace);
  lcd.setCursor(12, 0);
  lcd.print("Hour");
  lcd.setCursor(0, 1);
  lcd.print("SpinTime:");
  lcd.print(LigSpinTime);
  lcd.setCursor(12, 1);
  lcd.print("Hour");
}
void LCD_lin2() {
  if (menuChg==3){
    menuChg=0;
  }
  else{
    menuChg=menuChg+1;
  }//Change page
}
void NorFed_Disp(){
  lcd.print("Fed:");
  if (Fed_en==1){
    lcd.print(" ON");
  }
  else{
    lcd.print("OFF");
  }
}
void NorWat_Disp(){
  lcd.print("Wat:");
  if (Wat_en==1){
    lcd.print(" ON");
  }
  else{
    lcd.print("OFF");
  }
  lcd.setCursor(9, 1);
}
void NorLig_Disp(){
  lcd.print("Lig:");
  if (Lig_en==1){
    lcd.print(" ON");
  }
  else{
    lcd.print("OFF");
  }
  lcd.setCursor(9, 1);
}
void NorWatLin_Disp(){
  lcd.print("WatL:");
}
void Debounce_main() {
  // read the state of the pushbutton value:
  if (digitalRead(Set)==1) {
    Seting= ~Seting;
    if (Seting==0){
      I2CBT_WriteFlag=1;
    }
    else{
      I2CBT_WriteFlag=2;
    }
    lcd.clear();
  }
  if (digitalRead(up)||digitalRead(down) ==1) {
    lcd.clear();
  }
  Nexting= digitalRead(Next);
  uping= digitalRead(up);
  downing= digitalRead(down);
}
arrow
arrow
    文章標籤
    Code
    全站熱搜
    創作者介紹
    創作者 兩隻小豬 的頭像
    兩隻小豬

    **兩隻小豬**

    兩隻小豬 發表在 痞客邦 留言(0) 人氣()