ส่วนลิงก์นี้ก็เป็นตัวอย่างการทำงานของหุ่นยนต์ ก็ตามนี้ครับ
จากวีดิโอเป็นกิจกรรมที่ 20-3 หุ่นยนต์ Object Hitter BOT
สำหรับโค้ดตัวอย่างก็มีดังนี้
#include // Include the main library
int L,R,x=0,y=0;
/* Move forward funtion */
void FF()
{
fd(60);
sleep(300);
}
/* 90-Degree Turn right function */
void R90()
{
fd(60); // Move forward pass the crossing line
sound(1500,100); // Drive the sound signal
while(analog(1)>500) // Check the Right line sensor to detect the white area
{
sr(60); // Spin right
}
while(analog(1)<500) // Spin right until detect the black line
{
sr(60); // Spin right again to pass the black line
}
}
/* 90-Degree Turn left function */
void L90()
{
fd(60); // Move forward pass the crossing line
sound(1500,100); // Drive the sound signal
while(analog(0)>500) // Check the Right line sensor to detect the white area
{
sl(60); // Spin right
}
while(analog(0)<500) // Spin right until detect the black line
{
sl(60); // Spin right again to pass the black line
}
}
/* Hit object function */
void hit()
{
bk(100); // Move backward short time
sleep(50);
ao(); // Stop motor
servo(1,180); // Drive servo motor to 180-degree position
sleep(1000);
servo(1,0); // Drive servo motor back to 0-degree position
bk(60); // Move backward
sleep(200);
R90(); // 90-degree turn right
}
void setup()
{
setTextSize(2); // Set text size 2x
glcd(1,1,"Press OK"); // Show the start message
glcd(2,1,"to Start");
sw_ok_press(); // Wait for OK switch pressed
glcdClear();
glcd(1,1,"Let's go!"); // Show operation message
}
void loop()
{
L=analog(0); // Read the Left line sensor value
R=analog(1); // Read the Right line sensor value
if (L<500&&R<500) // Detect the crossing line
{
if(x==5) // The crossing line counting was complete
{
x=0; // Restart counting
y++; // Increase branch counter
if (y==4) // The branch counting was complete
{
ao(); // Stop the robot
servo (1,-1); // Stop servo motor
while(1);
}
}
x++; // Increase the line crossing counter
if (x==1) // 1st crossing
{
L90(); // Turn left
}
else if(x==2||x==4) // 2nd and 4th crossing
{
hit(); // Hit the object
}
else if (x==3) // 3rd crossing
{
FF(); // Move forward
}
else if (x==5) // 5th crossing
{
R90(); // 90-degree turn right
}
}
else if(L>500&&R>500) // Both sensors detect the white area. It means robot move bestride the line
{
fd(60); // Move forward with 60% power
}
else if(L<500) // The left line sensor detects the black line
// The robot will move out the line in right
{
sl(60); // Spin left a little bit to adjust the robot over the line
sleep(20);
}
else if(R<500) // The right line sensor detects the black line
// The robot will move out the line in left
{
sr(60); // Spin right a little bit to adjust the robot over the line
sleep(20);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น