|
想做个哇哇机, 电路用了2个迷你L298N 接了3个减速电机
舵机是接到arduino的
按下按键摇杆就不工作了求解决方法
#include <Servo.h>
#define YG_X A0 //X
#define YG_Y A1 //Y
Servo myservo; // 创建伺服对象来控制伺服器
int anniu = 2;
void setup() {
//**************************************************舵机与减速电机
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(anniu,INPUT);
myservo.attach(6);
digitalWrite(anniu,HIGH);
myservo.write(0);
//****************************************************
//摇杆
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
Serial.begin(9600);
}
void loop() {
int XZ = analogRead(YG_X);
int YZ = analogRead(YG_Y);
//右
if(XZ>=0&&YZ<=10){
digitalWrite(9,HIGH);
}
else{digitalWrite(9,LOW);}
//左
if(XZ>=500&&YZ>=990){
digitalWrite(8,HIGH);
}
else{digitalWrite(8,LOW);}
//下
if(XZ<=10&&YZ>=500){
digitalWrite(11,HIGH);
}
else{digitalWrite(11,LOW);}
//上
if(XZ>=1020&&YZ>=500){
digitalWrite(10,HIGH);
}
else{digitalWrite(10,LOW);}
//消除影响
if(XZ>=1020&&YZ>=1020){
digitalWrite(9,LOW);
digitalWrite(8,LOW);
}
//减速电机与舵机
//这里是按下按键 放下爪子 然后在上去,但是有delay 摇杆不工作了,怎么才能让摇杆 和舵机同时工作
if(digitalRead(anniu) == LOW){
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
delay(3000);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay(3000);
myservo.write(90);
delay(1000);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
delay(3000);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
delay(1000);
myservo.write(1);
}
delay(1);
Serial.print(XZ);
Serial.print("\t");
Serial.println(YZ);
//delay(500);
}
|
|