Page 134 - ArduinoและPOP-BOT
P. 134

134







              /*******************************************************************************
              * POP-BOT V1.0
              * Filename : SeekingObject.pde
              * Moves servo and seek object position to show the result on SCLD
              ********************************************************************************/
              // *** in this example SLCD connected to pin 16 ***
              #include <popbot.h>  // Include Library for POP-BOT
              #include <gp2d120_lib.h>
              int posValue[] = {170,150,130,110,90,70,50,30,10};  // Servo position value
              int j,Minimum,MinPoint;
              int GP2[9];
              int getPoint()
              {
                 int i;
                 int old=GP2[0];
                 int min_;
                 for(i=0;i<9;i++)
                 {
                   if(GP2[i]<old)
                   {
                       old = GP2[i];
                       min_ = i;
                   }
                 }
                 Minimum = old;
                 return(min_);
              }
              void setup()
              {

              }
              void loop()
              {
                 for(j=0;j<9;j++)
                 {
                   servo(7,posValue[j]);
                   sleep(1000);
                   GP2[j]=getdist(5);
                   lcd(“Pos[%d]: %d   “,j,GP2[j]);
                 }
                 MinPoint = getPoint();
                 servo(7,posValue[MinPoint]);
                 lcd(“Selected: %d   #nValue = %d   “,MinPoint,Minimum);
                 beep();
                 sleep(5000);
                 lcd(“#c”);  // Clear old display
              }
              /***************************************************************************/

            โปรแกรมที่  A10-1 ไฟล  SeekingObjectTest.pde โปรแกรมภาษา C ของ Arduino สํ าหรั บค นหาวั ตถุ

            โดยใช โมดู ล GP2D120 ทํ างานร วมกั บเซอร โวมอเตอร ของหุ นยนต  POP-BOT
   129   130   131   132   133   134   135   136   137   138   139