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

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






                 /*******************************************************************************
                 * POP-BOT V1.0
                 *  Filename  :  CheckCrossLine.pde
                 ********************************************************************************/
                 #include <popbot.h>                    // Include library for POP-BOT
                 #define POW 80
                 #define Ref 500
                 int left;
                 int right;
                 void forward(unsigned int time)
                 {
                    motor(1,POW);
                    motor(2,POW);
                    sleep(time);
                 }
                 void turn_left(unsigned int time)
                 {
                    motor(1,-POW);
                    motor(2,POW);
                    sleep(time);
                 }
                 void turn_right(unsigned int time)
                 {
                    motor(1,POW);
                    motor(2,-POW);
                    sleep(time);
                 }
                 void setup()
                 {
                 }
                 void loop()
                 {
                    left = analog(3);
                    right = analog(1);
                    if(left<Ref && right<Ref)
                    {
                      forward(1);
                      beep();
                      sleep(100);
                    }
                    else if(left>Ref && right>Ref)
                    {
                      forward(1);
                    }
                    else if(left<Ref && right>Ref)
                    {
                      turn_left(10);
                    }
                    else if(left>Ref && right<Ref)
                    {
                      turn_right(10);
                    }
                 }
                 /******************************************************************************/

               โปรแกรมที่  A6-1 ไฟล  CheckCrossLine.pde โปรแกรมภาษา C ของ Arduino สํ าหรั บ
               ควบคุ มให หุ นยนต  POP-BOT เคลื่ อนที่ ตามเส นสี ดํ าและขั บเสี ยงเมื่ อตรวจจั บพบทางแยก
   102   103   104   105   106   107   108   109   110   111   112