欢迎来到天天文库
浏览记录
ID:52237915
大小:13.24 KB
页数:10页
时间:2020-03-25
《机械臂控制代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、##include#include#include#include"Wire.h"#include"WiiChuck.h"Servoservo1; //createservoobjecttocontrolaservoServoservo2;Servoservo3;Servoservo4; //amaximumofeightservoobjectscanbecreatedServoservo5;Servoservo6;WiiChu
2、ckwii=WiiChuck();intpos1,pos2,pos3,pos4,pos5,pos6,x,y,z,c; //variabletostoretheservopositionvoidsetup(){ //attachestheservoonpin9totheservoobjectservo1.attach(8); servo2.attach(9); servo3.attach(10); servo4.attach(11); servo5.attach(12); servo6.a
3、ttach(13); pos1=60; pos2=50; pos3=100; pos4=100; pos5=90; pos6=90;servo1.write(pos1); servo2.write(pos2); servo3.write(pos3); servo4.write(pos4); servo5.write(pos5); servo6.write(pos6); wii.initWithPower(); }voidloop(){ if(true==wii.read()
4、) { //读取手柄值 x=wii.getJoyAxisX(); y=wii.getJoyAxisY(); c=wii.getButtonC(); z=wii.getButtonZ();} if(x==255 &&y>120&&y<140 &&c==0&&z==1)//一号舵机 {pos1=pos1+10; servo1.write(pos1); delay(40); if(pos1>160) { pos1=160; servo1.wri
5、te(pos1); } } elseif(x==255&&y>120&&y<140&&z==0&&c==1) {pos1=pos1-10; servo1.write(pos1); delay(40); if(pos1<20) { pos1=20; servo1.write(pos1); } } elseif(x>220 &&y==0 &&c==0&&z==1)//2号舵机 { pos2=pos2+10; servo2.write(
6、pos2); delay(40); if(pos2>160) { pos2=160; servo2.write(pos2); } } elseif(x>220&&y==0 &&z==0&&c==1) { pos2=pos2-10; servo2.write(pos2); delay(40); if(pos2<90) { pos2=90; servo2.write(pos2); } } els
7、eif(x>110 &&x<140 &&y==0 &&c==0&&z==1)//3号舵机 { pos3=pos3+10; servo3.write(pos3); delay(40); if(pos3>160) { pos3=160; servo3.write(pos3); } } elseif(x>110 &&x<140 &&y==0 &&z==0&&c==1) { pos3=pos3-10; servo3.wri
8、te(pos3); delay(40); if(pos3<20) { pos3=20; servo3.write(pos3); } } elseif(x==0 &&y==0 &&c==0&&z==1)//4号舵机 { pos4=pos4+10; servo4.write(pos4); delay(40); if(pos4>160) { pos4=160;
此文档下载收益归作者所有