The Center of learning aids to develop Thai education to international

50th Anniversary

การเขียนโปรแกรมด้วย Python (Fix the Bugs)

การเขียนโปรแกรมด้วย Python กับ LEGO Education คำสั่งน่ารู้: Fix the Bugs

 

หน้าต่าง Console ช่วยให้สามารถเจอข้อผิดพลาดของโปรแกรมได้ 2 จุด เริ่มต้นโดยการเชื่อมต่อ Force Sensor เข้ากับพอร์ต B อีกครั้ง เพื่อแก้ไขข้อผิดพลาดจุดที่สอง จากนั้นทดลองรันโปรแกรมด้านล่าง ซึ่งแก้ไขข้อผิดพลาดจุดแรกด้วยการนำชุดคำสั่งไปไว้ภายใน while True เพื่อให้โปรแกรมทำงานซ้ำอย่างต่อเนื่อง

 

import force_sensor
import motor
from hub import port

while True:
    # Store the force of the Force Sensor in a variable.
    force = force_sensor.force(port.B)

    # Print the variable to the Console.
    print(force)

    # Run the motor and use the variable to set the speed.
    motor.run(port.A, force)

 

ลองกด Force Sensor ขณะที่โปรแกรมกำลังทำงาน คุณจะสังเกตเห็นว่ามอเตอร์หมุนเร็วขึ้น หรือช้าลงตามแรงกดที่กระทำต่อเซนเซอร์ นอกจากนี้ ยังมีค่าของตัวแปรจำนวนมากแสดงขึ้นอย่างต่อเนื่องบนหน้าต่าง Console ตัว Force Sensor สามารถวัดแรงในหน่วยเดซินิวตัน (decinewton: dN) โดยสามารถวัดแรงได้สูงสุด 10 นิวตัน (N) หรือเท่ากับ 100 เดซินิวตัน (dN)

 

Writer : Pacharee Wongcherngyoot
CODE : 80D45678

฿ 34,500