6308| 0
|
[求助问答] 红外避障控制舵机运行求助 |
我想用两个红外避障传感器充当不同动作的开关的话,是否这两个传感器都必须连在电路里,如果我只连接一个红外避障的话就自动识别了没有连接的那个,这是我代码的问题吗? 其中我用到了16路pwm值的舵机控制板和两个红外避障传感器来控制若干个舵机,当红外1感受到信号时进行一串动作;红外2感受到信号时进行另一串动作,但是如果我电路中没有连接红外1那么程序就直接默认进行了红外1下的动作, 附代码如下: [mw_shl_code=applescript,true]#include <Wire.h> #include <Adafruit_PWMServoDriver.h> Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); #define SERVOMIN 150// this is the 'minimum' pulse length count (out of 4096) 舵机的起始角度0° #define SERVOMAX 300 // this is the 'maximum' pulse length count (out of 4096) 舵机的终止角度180° int buttonpin=11; //定义避障传感器接口 int buttonpin2=6; //定义避障传感器接口 int var=0; void setup() { Serial.begin(9600); Serial.println("16 channel Servo test!"); pinMode(buttonpin,INPUT);//定义避障传感器为输出接口 pinMode(buttonpin2,INPUT);//定义避障传感器为输出接口 pwm.begin(); pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates时间 for(int i=0;i<13;i++) pwm.setPWM(i, 0, SERVOMIN); // 舵机复位 } void setServoPulse(uint8_t n, double pulse) { double pulselength;//精度浮点数 pulselength = 1000000; // 1,000,000 us per second pulselength /= 60; // 60 Hz Serial.print(pulselength); Serial.println(" us per period"); pulselength /= 4096; // 12 bits of resolution Serial.print(pulselength); Serial.println(" us per bit"); pulse *= 1000; pulse /= pulselength; Serial.println(pulse); pwm.setPWM(n, 0, pulse); } void loop() { int timer1=0,timer2=0; switch (var) { case 0: if(digitalRead(buttonpin) == 0)//IF传感器红外臂障1接受到数据; { Serial.println("0 buttonpin "); for(int i=0;i<3;i++) { for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(4, 0, pulselen); } delay(500); for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) { pwm.setPWM(4, 0, pulselen); } delay(500); } for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(13, 0, pulselen); pwm.setPWM(14, 0, pulselen); pwm.setPWM(4, 0, pulselen); } delay(500); for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) { pwm.setPWM(13, 0, pulselen); pwm.setPWM(14, 0, pulselen); pwm.setPWM(4, 0, pulselen); } delay(500); var=1; } if(digitalRead(buttonpin2) == 0)//IF传感器红外臂障4接受到数据; //触摸小人的脑袋后开始小人的动作 {Serial.println("0 buttonpin2"); for(int i=0;i<3;i++) { for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(7, 0, pulselen); pwm.setPWM(12, 0, pulselen); pwm.setPWM(5, 0, pulselen); pwm.setPWM(6, 0, pulselen); } delay(1000); for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) { pwm.setPWM(7, 0, pulselen); pwm.setPWM(12, 0, pulselen); pwm.setPWM(5, 0, pulselen); pwm.setPWM(6, 0, pulselen); } delay(1000); } var=2; } break; } }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed