[自製初音機器人]–CODE PART2

接續上禮拜的CODE

void loop(){

char str[50];

int8_t button_set1[8], button_set2[8]; psp.getButtons(button_set1,button_set2);

if(button_set2[BS2_Button_R1]==0){
 if(r_arm<0){}
else{
r_arm=r_arm-1;
servo_r_arm.write(r_arm);
delay(5);
}
}
else if(button_set2[BS2_Button_R2]==0){
if(r_arm>270){}
else{
r_arm=r_arm+1;
servo_r_arm.write(r_arm);
delay(5);
}
}

else if(button_set2[BS2_Button_Circle]==0){
if(r_shoulder<0){}
else{
r_shoulder=r_shoulder-1;
servo_r_shoulder.write(r_shoulder);
delay(5);
}
}

else if(button_set2[BS2_Button_Square]==0){
if(r_shoulder>270){}
else{
r_shoulder=r_shoulder+1;
servo_r_shoulder.write(r_shoulder);
delay(5);
}
}

else if(button_set2[BS2_Button_L1]==0){
if(l_arm<0){}
else{
l_arm=l_arm-1;
servo_l_arm.write(l_arm);
delay(5);
}
}

else if(button_set2[BS2_Button_L2]==0){
if(l_arm>270){}
else{
l_arm=l_arm+1;
servo_l_arm.write(l_arm);
delay(5);
}
}

else if(button_set1[BS1_Button_Right]==0){
if(l_shoulder<0){}
else{
l_shoulder = l_shoulder-1;
servo_l_shoulder.write(l_shoulder);
delay(5);
}
}

else if(button_set1[BS1_Button_Left]==0){
if(l_shoulder>270){}
else{
l_shoulder = l_shoulder+1;
servo_l_shoulder.write(l_shoulder);
delay(5);
}
}

Serial.print("r_arm: ");
Serial.print(r_arm);
Serial.print(" r_shoulder: ");
Serial.print(r_shoulder);
Serial.print(" l_arm: ");
Serial.print(l_arm);
Serial.print(" l_shoulder: ");
Serial.println(l_shoulder);
}

宣告2個陣列8位元陣列

判斷有無按下”R1”
若 r_arm<0,則沒反應

若有按下”R1”,則左手往上轉

以下的delay是讓servo有足夠的時間轉動

判斷有無按下”R2”
若 r_arm>270,則沒反應

若有按下”R2”,則左手往下轉

判斷有無按下”圓形”
若 r_shoulder<0則沒反應

若有按下”圓形”,則左手往右轉

判斷有無按下”方形”
若 r_shoulder>270則沒反應

若有按下”方形”,則左手往左轉

判斷有無按下”L1”
若l_arm<0則無反應

若有按下”L1”,則右手往上轉

判斷有無按下”L2”
若l_arm>270則無反應

若有按下”L2”,則右手往下轉

判斷有無按下”右”
若l_shoulder<0則無反應

若有按下”右”,則右手往右轉

判斷有無按下”左”
若l_shoulder>270則無反應

若有按下”左”,則右手往左轉

顯示各Servo目前的position

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *