Facebook
From ANT, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 175
  1. // 20190505 reprogram display dspLine1[] dspLine2[]
  2. // 20190507 RS232 DATA read
  3. // 20190511 fine tune program
  4.  
  5. # include <EEPROM.h>
  6. //Define 74HC595 Connections with arduio
  7. const int Latch=5; //BLACK
  8. const int Data=6;//GREEN
  9. const int Clock=7;//BLUE
  10.  
  11. char V2_Array[5];
  12. //char Addrmodule[3]= "ad";//----------------------0503
  13. int oldstringlength;
  14. int KS; // key switch press
  15. int KSC;
  16. byte Krs=0,Kls=0,Kdec=0,Kinc=0,Kenter=0,Ksel =0;
  17. boolean Khold = false;
  18. byte oldStringlength;
  19. const int keyport = A0;
  20. byte oldcaseB = 0 ;
  21. byte caseB;
  22. byte caseC;
  23. byte oldcaseC;
  24. byte oldCase =0;
  25. byte caseA = 0;
  26. int incB = 0;
  27. int incB_old = 1;
  28. int chr15;
  29. //int Set_value =0;
  30. //int Set_valueA=0;
  31. char moduleAddr[2];//--------------0503
  32. String OutPrintStr;
  33. //-------------------------------------------------
  34. struct CMD_indicate {
  35.   char *sel_No;
  36.   char *name;
  37.   boolean mod;//enter data need...
  38. };
  39. //------------------20190505------------------------------
  40. char *dspLine1;//total display matrix line1 不能使用 dspLine1[24] 會異常
  41. char *dspLine2;
  42. int incC=0;
  43. char *sel_No_buf;//use store for display dspLine2
  44.  
  45. struct CMD_indicate caseAs[] =
  46. {
  47.  {"%AANNTTCCFF","Set Configure ",1},//0
  48.  {"@AAHI+00.000","Set Hi Alarm       ",1},//11
  49.  {"@AALO+00.000","Set Lo Alarm ",1},//12
  50.  {" AANN  ","Change Addr AA new=NN",1},//13
  51.  {"#AA","Read input data",0},//1
  52.  {"$AA0","Span calibrate ",0},
  53.  {"$AA1","Offset calibrate ",0},
  54.  {"@AAEAT","Enable Alarm",0},
  55.  {"@AADA","disable Alarm",0},//5
  56.  {"@AACA","clear Latch alarm",0},
  57.  {"@AARH","Read Hi Alarm ",0},
  58.  {"@AARL","Read Lo Alarm ",0},
  59.  {"@AARE","Read Event count",0},
  60.  {"@AACE","clear Event count  ",0},//10
  61.  {"$AA2","Read Config para ",0},//14
  62.  {"@AADI","Read digit I/O status",0}//15
  63. };
  64. char Num_hex[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  65. int Num_hexNo0 = 0 ;
  66. int Num_hexNo1 = 0 ;
  67. char Set_AA[2];
  68. char Set_NN[2];
  69.  
  70. void LCDinit(void);
  71. void LCDPrint(char Line,char string[24]);
  72. void LCDPrintString(int x,int y, char string[]);
  73. void LCDWriteChar(int x,int y,char Data);
  74. void LCDCursorMove(int y,int x);
  75. byte Keyscan(void);
  76. void dispCase (int caseA);
  77. void dispCaseB(void);
  78. boolean level1 =0;
  79. boolean level2 =0;
  80. boolean OK_flag = false;//when OK key is pressed
  81. boolean level3 = 0;
  82. boolean Set_value_sign = false;
  83. unsigned long startTime = 0;
  84. int Maxtime = 3000;
  85. //-----------20190507 RS232 receive ----------------
  86. char *msgbuf ="HI HOW ARE YOU"; // buff of rs232 read in from PC or ADAM 4012
  87. int strleng;//read in string length
  88. boolean read_OK_flag;
  89. /*
  90. 把字串轉成陣列
  91. 把陣列轉成字串
  92. 讀取RS232送過來的字串存到陣列裡面以'\n'為結束
  93. */
  94. void RS232read()
  95. {
  96.    int k=0;
  97.    byte chr;
  98.    if (Serial.available() > 0)
  99.    {
  100.     while((chr = Serial.read()) != '\n')
  101.     {
  102.        msgbuf[k]=chr;
  103.         k++;
  104.     }  
  105.    }
  106.  strleng = k;
  107. }
  108.  
  109.  
  110.  
  111. // load structure data to buff
  112. // replace to buff AA to 02
  113. // set index to 6
  114. // modify index 6[0] to index6[2]
  115. //re display
  116. // incB chr15 Num_hex[posi]
  117. //int posi;//position of dspLine2[]
  118.  
  119. void CleanLine1(void)
  120. {
  121.    LCDPrint(1,"                    ");//0505 clean over data
  122. }
  123. void CleanLine0(void)
  124. {
  125.    LCDPrint(0,"                    ");//0505 clean over data
  126. }
  127.  
  128.  
  129. //------------------------------------------------
  130. void setup() {
  131.   moduleAddr[0] = EEPROM.read(0);
  132.   moduleAddr[1] = EEPROM.read(1);
  133.   pinMode(Data, OUTPUT);
  134.   pinMode(Clock, OUTPUT);
  135.   pinMode(Latch, OUTPUT);  
  136.  Serial.begin(9600); // start the serial monitor link
  137.   LCDinit();  //initalize LCD in 4-bit mode
  138.   LCDPrint(0,"WELCOME   ADAM-4012-485 ");
  139.   LCDPrint(1," 4-20mA TO RS485 --2019 ");
  140. //  Serial.println("#02");
  141. }
  142.  
  143. void loop() {
  144.   delay(2000);
  145. //  Serial.println("write to --------1->>> ");// -----------NANO-
  146.   LCDclean();
  147.   LCDPrint(0," Read Data #02 per 3 Sec");
  148.   startTime = millis();
  149.   level1 = true;
  150. //  Serial.println(startTime);//--------------NANO
  151.   while(level1){ //------------------------------------------level 1
  152.          
  153.           if(millis() - startTime > Maxtime )//timer 1sec
  154.           {
  155.  
  156.             Serial.println("#02");// send  out for inquire data        
  157.             startTime = millis();
  158.             delay(200);          
  159.             RS232read();
  160. // if(msgbuf[0] =='>' || msgbuf[0] =='!' )
  161.           {      
  162.             CleanLine1();
  163.             String Annn = String (msgbuf);
  164.             Annn.substring(0,9);
  165.             Annn.toCharArray(msgbuf,9);
  166.             LCDPrint(1,msgbuf);//---------------------
  167.             }
  168.            }
  169.       caseA = Keyscan();
  170.      if (caseA != oldCase)
  171.          {    
  172.           dispCase(caseA);
  173.           oldCase = caseA;
  174.          }
  175.          
  176.       switch (caseA)
  177.      {
  178.       case 1://next
  179.             LCDPrint(0," Main Function         ");
  180.              LCDPrint(1," < key no function     ");
  181.              
  182.       break;
  183.       case 2://previous
  184.              LCDPrint(0," Main Function         ");
  185.              LCDPrint(1," > key no function     ");
  186.       break;
  187.       case 4://up
  188.            level2 = true;
  189.            if(OK_flag)
  190.            {
  191.             LCDPrint(0,"OK was pressed!         ");
  192.             OK_flag = false;
  193.            }
  194.            else
  195.            {
  196.            LCDPrint(1,"                  ");
  197.            }        
  198.            while(level2) //---------------------------------------level 2
  199.                 {
  200.                   caseB = Keyscan();                
  201.                   if(caseB != oldcaseB)
  202.                     {
  203.                       dispCaseB();//display structure caseAs[] content
  204.                       oldcaseB = caseB;//not more key same character
  205.                      }
  206.                  switch (caseB)
  207.                         {
  208.                            case 1:
  209.  
  210.                             incB ++;
  211.                            if((incB <0) ||(incB >16))
  212.                               {incB = 16;}                  
  213.                             caseB_stepDisp();
  214.                            CleanLine1();
  215.                            break;
  216.                           case 2:
  217.  
  218.                             incB --;
  219.                             if((incB <0) ||(incB >16))
  220.                               {incB = 0;}
  221.                               caseB_stepDisp();    
  222.                                CleanLine1();                
  223.                           break;
  224.                           case 4:
  225.                             incB ++;  
  226.                            if((incB <0) ||(incB >16))
  227.                               {incB = 16;}                        
  228.                             caseB_stepDisp(); //display step
  229.                              CleanLine1();                              
  230.                           break;
  231.                           case 8://ok process
  232.                            dispCaseB();//display structure data on LCM
  233.                            LCDWriteChar(0,22,'o');
  234.                          OK_process();//-------------------------------------Level 3
  235.                         delay(200);          
  236.                         RS232read();
  237.                      //  Serial.print("strleng = ");
  238.                       //  Serial.println(strleng);
  239.                       // if(msgbuf[0] =='>' || msgbuf[0] =='!' )
  240.                             {      
  241.                              CleanLine1();    
  242.                             // LCDPrintString(1,3,msgbuf);//0511
  243.  //                           Serial.print("strleng= ");
  244.  //                           Serial.println(strleng);
  245.                                 LCDWriteChar(1,20,(strleng/10+48));//list string length
  246.                                 LCDWriteChar(1,21,(strleng%10+48));
  247.                                 read_OK_flag = true;
  248.                              }
  249.                           break;
  250.                           case 16:
  251.                             incB --;
  252.                              if((incB <0) ||(incB >16))
  253.                               {incB = 0;}
  254.                               //LCDPrintString(0,17,"+");//clean some digit
  255.                               caseB_stepDisp();
  256.                                CleanLine1();                        
  257.                            break;
  258.                            case 32:
  259.                               level2 = false;
  260.                            break;
  261.                        }
  262.                    }        
  263.       break;
  264.       case 8://ok
  265.             LCDPrint(0," Select Function by ^ v ");
  266.             OK_flag = true;
  267.       break;
  268.      case 16://down
  269.             LCDPrint(0," @AADA ---Alarm off    ");
  270.        
  271.  
  272.       break;
  273.       case 32://return
  274.             LCDPrint(0," Main Function         ");
  275.            
  276.       break;
  277.  
  278.      }  
  279.         delay(100);
  280.  
  281.        }
  282.      }
  283.  
  284.  
  285. void caseB_stepDisp(void)//
  286. {
  287.    LCDWriteChar(0,19,'+');//clean some digit
  288.    LCDWriteChar(0,20,((incB/10)+48));//display item no
  289.    LCDWriteChar(0,21,((incB%10)+48));      
  290. }
  291.  
  292.  
  293. // read structure --- caseAs[]
  294. void OK_process(void)
  295. {
  296.   boolean next_treat = caseAs[incB].mod;
  297.     chr15 = 0; //initial data
  298.   if(next_treat)//enter next data with v ^ < > OK Ret.
  299.    {
  300.     level3 = true;
  301.               while(level3)
  302.                 {
  303.  
  304.                   LCDWriteChar(0,23,(chr15+48));//display cursor
  305.                   caseC = Keyscan();                
  306.                   if(caseC != oldcaseC)
  307.                     {
  308.                       oldcaseC = caseC;//not more key same character
  309.                      }
  310.                    switch (caseC)
  311.                         {
  312.                            case 1:
  313.                                  dspLine1[22]='<'; //0505                              
  314.                                   chr15 --;                              
  315.                                   if(chr15 <0)
  316.                                   {chr15 = 0;}                  
  317.                            break;
  318.                            case 2:
  319.                                   dspLine1[22]='>'; //0505                            
  320.                                   chr15 ++;                                
  321.                                   if(chr15 >10)
  322.                                   {chr15 =10;}
  323.                            break;
  324.                            case 4:
  325.                            dspLine1[22]='^';
  326.                            if(incB ==3)
  327.                            {//set AA-NN
  328.                             Num_hexNo0++;
  329.                                   if(Num_hexNo0 < 0)
  330.                                    {
  331.                                      Num_hexNo0 =0;
  332.                                    }                              
  333.                                   if(Num_hexNo0 > 15)
  334.                                    {
  335.                                      Num_hexNo0 =15;
  336.                                    }                          
  337.                             Set_NN[chr15] = Num_hex[Num_hexNo0];
  338.                             dspLine2[(chr15)+3] = Set_NN[chr15];                        
  339.                            }
  340.                       else if(incB ==0)
  341.                       {//for%AANNTTCCFF
  342.                         incC ++;                    
  343.                         dspLine2[(chr15)] = Num_hex[incC];
  344.                         LCDPrint(1,dspLine2);      
  345.                       }    
  346.                            else
  347.                            {
  348.                             incC ++;// 0505 inc parameter @AAHI+00.000 use incC
  349.                             if(chr15 ==0)
  350.                             {
  351.                               Set_value_sign = !Set_value_sign;
  352.                             if(Set_value_sign)
  353.                                 {
  354.                                 dspLine2[5] = '+';//0505
  355.                                 }
  356.                              else
  357.                                  {
  358.                                   dspLine2[5] = '-';//0505
  359.                                  }                                
  360.                            }
  361.                            else
  362.                              {
  363.                                dspLine2[(chr15+5)] = Num_hex[incC];
  364.                              }
  365.                             }    
  366.                             LCDPrint(1,dspLine2);
  367.                            
  368.                            break;
  369.                            case 8:
  370.                             LCDPrint(0," level 3 process ok     ");
  371.                             if(incB ==3)
  372.                             {
  373.                               EEPROM.write(0,Set_NN[0]);
  374.                               EEPROM.write(1,Set_NN[1]);
  375.                             }                                      
  376.                             level3 = false;
  377.                            break;
  378.                            case 16:
  379.                             dspLine1[22]='v';
  380.                            
  381.                            if(incB ==3)
  382.                            {//set AA-NN
  383.                             Num_hexNo0--;
  384.                                   if(Num_hexNo0 < 0)
  385.                                    {
  386.                                      Num_hexNo0 =0;
  387.                                    }                              
  388.                                   if(Num_hexNo0 > 15)
  389.                                    {
  390.                                      Num_hexNo0 =15;
  391.                                    }                          
  392.                             Set_NN[chr15] = Num_hex[Num_hexNo0];
  393.                             dspLine2[(chr15)+3] = Set_NN[chr15];                        
  394.                            }
  395.                       else if(incB ==0)
  396.                       {//for%AANNTTCCFF
  397.                         incC --;                    
  398.                         dspLine2[(chr15)] = Num_hex[incC];
  399.                         LCDPrint(1,dspLine2);      
  400.                       }    
  401.                            else
  402.                            {
  403.                             incC --;// 0505 inc parameter @AAHI+00.000 use incC
  404.                             if(chr15 ==0)
  405.                             {
  406.                               Set_value_sign = !Set_value_sign;
  407.                             if(Set_value_sign)
  408.                                 {
  409.                                 dspLine2[5] = '+';//0505
  410.                                 }
  411.                              else
  412.                                  {
  413.                                   dspLine2[5] = '-';//0505
  414.                                  }                                
  415.                            }
  416.                            else
  417.                              {
  418.                                dspLine2[(chr15+5)] = Num_hex[incC];
  419.                              }
  420.                             }    
  421.                             LCDPrint(1,dspLine2);
  422.                            break;
  423.                            case 32:
  424.                           level3 = false;                              
  425.                            break;                          
  426.                         }                                          
  427.  
  428.             }
  429.  
  430.          }
  431.  
  432.         ReplaceAA_NN();//replace AA by EEPROM data and Send out to RS232
  433.  }
  434.  
  435.  
  436. void ReplaceAA_NN(void)//20190504 replace AA by moduleAddr set by #13 AA-NN
  437. {
  438. //  OutPrintStr = caseAs[incB].sel_No;  
  439.   OutPrintStr = String (dspLine2);
  440.   OutPrintStr.replace("AA",moduleAddr);
  441.   Serial.println(OutPrintStr);// output rs232 the command data
  442.   CleanLine1();//clean LCM 1 display
  443. }
  444.  
  445. //display structure caseAs[] content
  446. void dispCaseB(void)
  447. {
  448. //  Serial.println("dispCaseB");
  449.   if(incB != incB_old)
  450.    {
  451.     incB_old = incB;//new change
  452.     CleanLine0();
  453.    }
  454.     LCDPrint(0,caseAs[incB].name);
  455.     LCDWriteChar(0,22,' ');
  456.     dspLine2 = caseAs[incB].sel_No;
  457.     LCDPrintString(1,0,caseAs[incB].sel_No);  
  458.     if(read_OK_flag)//read rs232 data
  459.     {
  460.       read_OK_flag = false;
  461.       String caseAs_sel_No = caseAs[incB].sel_No;
  462.       int sel_No_length= caseAs_sel_No.length();
  463.       String msgbuf_trim = String(msgbuf);
  464.       msgbuf_trim.substring(0,strleng-sel_No_length);
  465.       msgbuf_trim.toCharArray(msgbuf,strleng-sel_No_length);
  466.       LCDPrintString(1,(sel_No_length),msgbuf);
  467.     }
  468.  
  469. }
  470.      
  471.  
  472.      
  473. void dispCase (int nnnn)
  474.  {
  475.  
  476.   String V2_str = String(nnnn);
  477.   V2_str.toCharArray(V2_Array,4);
  478.     byte newString_length= V2_str.length();
  479.     if (oldStringlength != newString_length)
  480.        {
  481.  // LCDclean();//---------------------0505        
  482.        oldstringlength= newString_length;
  483.        LCDPrintString(1,22,"   ");//clean some digit      
  484.        }
  485.          LCDPrintString(1,22,V2_Array);  
  486. }
  487.  
  488.  
  489.  
  490. //=============================================================
  491. //             Send Data or Command to LCD
  492. //=============================================================
  493. void LCDSendByte(char d,char RS)
  494. {
  495.   char dH,dL,temp;
  496.   //Keep Data on upper nybble
  497.   dH = d & 0xF0;         //Get MSB
  498.   dL = d & 0x0F;
  499.   dL = d << 4;           //Get LSB
  500. //Send MSB with E=clock  
  501.   temp=0;
  502.   temp=dH | RS | 0x02;  //MSB With RS+E bit
  503.   ShiftData(temp);
  504. //Send MSB with E=0
  505.   temp=0;
  506.   temp=dH | RS;  //MSB With RS bit
  507.   ShiftData(temp);
  508. //Send LSB with E=clock
  509.   temp=0;
  510.   temp=dL | RS | 0x02;  //MSB With RS+E bit
  511.   ShiftData(temp);
  512. //Send LSB with E=0
  513.   temp=0;
  514.   temp=dL | RS;  //MSB With RS bit
  515.   ShiftData(temp);  
  516. }
  517. //=============================================
  518. void ShiftData(char temp)
  519. {
  520.   int i;
  521.   for(i=0;i<8;i++)      
  522.   {
  523.     if((temp & 0x80)==0x80)    //Send 1-bit to shift register
  524.     {digitalWrite(Data,HIGH);}
  525.     else
  526.     {digitalWrite(Data,LOW);}          
  527.     digitalWrite(Clock,LOW);
  528.     digitalWrite(Clock,HIGH);
  529.     temp=temp<<1;
  530.   }
  531.   //Latch the data
  532.   digitalWrite(Latch,LOW);
  533.   delay(1);
  534.   digitalWrite(Latch,HIGH);        
  535. }
  536. //=================================================================
  537. //           LCD Display Initialization Function
  538. //=================================================================
  539. void LCDinit(void)
  540. {
  541.   int count;
  542.   char t[]={0x43,0x03,0x03,0x02,0x28,0x01,0x0C,0x06,0x02,0x02};    
  543.       for (count = 0; count <= 9; count++)
  544.       {
  545.         LCDSendByte(t[count],0);    //Command RS=0
  546.       }
  547. }
  548. //=================================================================
  549. //        Display Line on LCD at desired location Function
  550. //=================================================================
  551. void LCDPrint(char Line,char string[24])//--------------------------------------
  552. {
  553.  int len,count;
  554.  if(Line==0) {
  555.    LCDSendByte(0x80,0);    //Command RS=0 Move Cursor to Home  
  556.  }
  557.  else {
  558.    LCDSendByte(0xC0,0);    //Command RS=0 Move Cursor to Second Line LCDSendByte(0xC0,0);
  559.  }
  560.         len = strlen(string);
  561.     for (count=0;count<len;count++)
  562.   {
  563.      LCDSendByte(string[count],1);  //Data RS=1
  564.   }
  565. }
  566. //=================================================================
  567. void LCDclean(void)
  568. {
  569.   LCDSendByte(0x01,0);
  570. }
  571.  
  572.  
  573.  void LCDCursorMove(int y,int x)//=====================================>
  574. {
  575.  if (y == 0)
  576.        {    
  577.        LCDSendByte(0x80 + x,0);
  578.        }  
  579.  else
  580.        {    
  581.        LCDSendByte(0xC0 + x,0);    
  582.        }    
  583. //  LCDSendByte(0x14,0);//input mode          
  584.  LCDSendByte(0x0E,0);// display control mode cursor  
  585. }
  586.  
  587. void LCDPrintString(int x,int y, char string[])
  588.  {  
  589.   int count, len;  
  590.  // summ = x;
  591.  if (x == 0)
  592.        {  
  593.          LCDSendByte(0x80+y ,0);
  594.        }
  595.  else
  596.        {
  597.        LCDSendByte(0xC0+y,0);  
  598.        }      
  599.      len = strlen(string);
  600.     for (count=0;count<len;count++)
  601.   {
  602.      LCDSendByte(string[count],1);  //Data RS=1
  603.   }
  604.  }
  605.  
  606.  
  607.  void LCDWriteChar(int x,int y,char Data)
  608. {
  609.  
  610.  if (x == 0)
  611.        {    
  612.        LCDSendByte(0x80 + y,0);
  613.        }  
  614.  else
  615.        {    
  616.        LCDSendByte(0xC0 + y,0);    
  617.        }      
  618.  LCDSendByte(Data,1);  
  619. }
  620.  
  621. void LCDhome(void)
  622. {
  623.   LCDSendByte(0x02,0);
  624. }
  625.  
  626. void LCDDisp(void)
  627. {
  628.    LCDSendByte(0x0f,0);
  629. }
  630.  
  631.  
  632. byte Keyscan(void)// There are 6 keys shiftL shiftR Up Down Enter Select
  633. {
  634.   byte kkk;
  635.   KS = analogRead(keyport);//Read(A0); //key scan read port3    
  636.  if(KS >640)//key no pressed
  637.  {
  638.   kkk = 64;
  639.  }
  640.  if((KS<639))// is press  &&(Khold == false)
  641.     {
  642.       delay(20);
  643.       KS=Mavg();
  644.       KSC=Mavg();
  645.    //   Serial.println(KS);
  646.       if(abs(KS-KSC)<10) //key press correctly
  647.         {                
  648.         if((KS<600)&&(KS>550)) //1st key > press (KS<600)&&(KS>531)
  649.                 {Krs=1;Kls=0;Kdec=0; Kinc=0;Kenter = 0; Ksel=0;
  650.                } //shift right          
  651.         else if((KS<550)&&(KS>525))//2nd key  < press KS<530)&&(KS>501)
  652.                   {Krs=0;Kls=1;Kdec=0; Kinc=0;Kenter = 0; Ksel=0;} //shift left
  653.         else if((KS<525)&&(KS>480))//3rd key Inc press((KS<500)&&(KS>451
  654.                   {Krs=0;Kls=0;Kdec=1; Kinc=0;Kenter = 0; Ksel=0;} //key down
  655.         else if((KS<480) &&(KS>400)) //4th key Dec press  (KS<450) &&(KS>361)      
  656.                   {Krs=0;Kls=0;Kdec=0; Kinc=1;Kenter = 0; Ksel=0;}//key up
  657.         else if((KS<400)&&(KS>100))//5th key "VoI" press   (KS<360)&&(KS>100)              
  658.                    {Krs=0;Kls=0;Kdec=0; Kinc=0;Kenter = 1; Ksel=0;}
  659.         else if(KS<100)// 6 th key "ON/OFF " press
  660.                     {Krs=0;Kls=0;Kdec=0; Kinc=0;Kenter = 0; Ksel=1;}
  661.                  //    Serial.println(KS);
  662.            kkk= Krs*1+Kls*2+Kinc*16+Kdec*4+Kenter*8+Ksel*32;
  663.    //        Serial.println(kkk);
  664.        while(analogRead(keyport)<640) {} //0502 modify for wait key relaese
  665.        }
  666.         else //2 times check KS != KSC
  667.         {
  668.         kkk = 64;
  669.         }              
  670.    }
  671.   delay(50);
  672.   return kkk;  
  673. }
  674.  
  675. int Mavg(void)
  676. // read 5 times data and average 讀5次求平均值
  677. // an for analog input no.
  678. {
  679.          byte at;
  680.          int M;
  681.         M=0;
  682.         for (at=0;at<3;at++)
  683.   {
  684.                 M=analogRead(keyport)+M;//analog 3 input
  685.                 delay(50);
  686.         }
  687.         M=M/3;
  688.        
  689.         return M;
  690. }
  691.