# **CyberPi程式機**
## mBot2 按鍵微編程
### 撰寫原因
一般寫程式都會使用到電腦及手機來控制自走車
但是在控制的時候常常發現需要安裝各種軟硬體
導致教學上十分的不方便
因此,決定利用Python來撰寫用CyberPi按鈕來直接控制mBot2的程式

### 按鍵思考
想法上先從按鍵設計起
觀察了CyberPi後,發現CyberPi本身就有一些按鈕
上 下 左 右 中 A按鍵 B按鍵
自走車來說其實後退沒甚麼用
上左右就可以完成大部分的路線
### 按鈕規劃
上按鈕 往前
左按鈕 往左90度
右按鈕 往右90度
下按鍵 迴圈
A按鍵 為清除按鍵
B按鍵 為執行程式的按鈕

這樣按鈕規劃就完成了
然後上左右比較容易完成,基本上就是
各個按鍵做完就將指令print到CyberPi的螢幕中
和存入字串中即可
開始的時候先將空字串設定好
```Python=2
import event, time, cyberpi, mbot2,mbuild
# initialize variables
way = 0
n = 0
times=0
@event.start
def on_start():
global way
cyberpi.console.println('按下B按鍵開始')
cyberpi.console.println('按下A按鍵重來')
way =''
```
上按鍵被按下後 就將上按鍵按鈕寫入字串中
```Python=
@event.is_press('up')
def is_joy_press():
global way, n
way = str(way) + str('↑')
cyberpi.display.show_label(way, 12, "bottom_mid", index= 0)
```
加入左右按鍵
```Python=
@event.is_press('left')
def is_joy_press2():
global way, n
way = str(way) + str('←')
cyberpi.display.show_label(way, 12, "bottom_mid", index= 0)
@event.is_press('right')
def is_joy_press3():
global way, n
way = str(way) + str('→')
cyberpi.display.show_label(way, 12, "bottom_mid", index= 0)
```
但是下按鈕的迴圈
就必須分別
第一次按下時是印出前括號
第二次按下則是印出後括號
第三次按下按壓次數歸零
程式上如下
```python=
@event.is_press('down')
def is_joy_press1():
global way, n,times
times=times+1
if times==1:
way = str(way) + str('[')
elif times==2:
way = str(way) + str(']')
times = 0
cyberpi.display.show_label(way, 12, "bottom_mid", index= 0)
```
最後是執行按鈕
基本上就是字串從頭讀到尾,但是字串中讀到後括號後開始往前讀
直到前括號才結束,讀到前括號後開始先判斷是否有需要跳出,
如果沒有就繼續在前後括號中間運行指令,如果有就挑出迴圈往下讀,不過由於涉及到讀指令的位置,因此在撰寫前不得不紀錄目前讀到的位置,然後再根據位置判定,這部分就比較需要思考了
### 最後完成的程式碼如下
```Python=2
import event, time, cyberpi, mbot2, mbuild
# 初始化變數
way = ""
start = 0
end = 0
times = 0
@event.start
def on_start():
cyberpi.console.println('按下B按鍵開始')
cyberpi.console.println('按下A按鍵重來')
@event.is_press('down')
def is_joy_press1():
global way, times
times += 1
if times == 1:
way += '['
elif times == 2:
way += ']'
times = 0 # 重置計數器
cyberpi.display.show_label(way, 12, "bottom_mid", index=0)
@event.is_press('up')
def is_joy_press():
global way
way += '↑'
cyberpi.display.show_label(way, 12, "bottom_mid", index=0)
@event.is_press('left')
def is_joy_press2():
global way
way += '←'
cyberpi.display.show_label(way, 12, "bottom_mid", index=0)
@event.is_press('right')
def is_joy_press3():
global way
way += '→'
cyberpi.display.show_label(way, 12, "bottom_mid", index=0)
@event.is_press('b')
def is_btn_press():
global way
n = 0
for n in range(len(way)):
if way[n] == '↑':
mbot2.straight(15)
elif way[n] == '←':
mbot2.turn(-90)
elif way[n] == '→':
mbot2.turn(90)
elif way[n] == ']':
end = n
while way[n] != '[':
n -= 1
start = n
while mbuild.ultrasonic2.get(1) > 10:
for n in range(start, end):
if mbuild.ultrasonic2.get(1) > 10:
if way[n] == '↑':
mbot2.straight(15)
elif way[n] == '←':
mbot2.turn(-90)
elif way[n] == '→':
mbot2.turn(90)
else:
mbot2.EM_stop("ALL") # 當檢測到超聲波距離大於10時,立即停止機器人
break # 立即跳出迴圈
break
n = end
break # 退出外層迴圈,因為內層迴圈已經完成任務
way = ''
cyberpi.display.show_label(way, 12, "bottom_mid", index=0)
@event.is_press('a')
def is_btn_press1():
cyberpi.stop_other()
```