#include <Servo.h>
#define motorpin1 7 //IN1
#define motorpin2 8 //IN2
#define motorpwm 9 //ENA
Servo myservo_1;
Servo myservo_2;
Servo myservo_3;
Servo myservo_4;
Servo myservo_5;
Servo myservo[5]={myservo_1,myservo_2,myservo_3,myservo_4,myservo_5};
int motor_pos[5] = {0,};
int temp1,temp2,temp3,temp4,temp5;
int X1=A0;
int Y1=A1;
int BUTTON1=2;
int X2=A2;
int Y2=A3;
int BUTTON2=4;
void motor(int motor1,int motor2,int mopwm,int val) //定义电机转动函数
{
pinMode(motor1,OUTPUT);
pinMode(motor2,OUTPUT);
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
analogWrite(mopwm,val);
}
void motor_plus(int servo,int pos){
if(motor_pos[pos]<179){
motor_pos[pos]+=3;
myservo[servo].write(motor_pos[pos]);
}
Serial.println("motor_plus");
}
void motor_minus(int servo,int pos){
if(motor_pos[pos]>1){
motor_pos[pos]-=3;
myservo[servo].write(motor_pos[pos]);
}
Serial.println("motor_minus");
}
void motor_plus_mini(int servo,int pos){
if(motor_pos[pos]<149){
motor_pos[pos]+=3;
myservo[servo].write(motor_pos[pos]);
}
Serial.println("motor_plus");
}
void motor_minus_mini(int servo,int pos){
if(motor_pos[pos]>41){
motor_pos[pos]-=3;
myservo[servo].write(motor_pos[pos]);
}
Serial.println("motor_minus");
}
void setup(){
myservo_1.attach(3);//
myservo_2.attach(5);//
myservo_3.attach(6);//
myservo_4.attach(9);
myservo_5.attach(10);//
Serial.begin(9600);
pinMode(BUTTON1,INPUT_PULLUP);
pinMode(BUTTON2,INPUT_PULLUP);
}
void loop(){
//servo 1
temp1=analogRead(X1);
Serial.print("X1 ");
Serial.println(temp1);
if(temp1>800){
motor_plus(0,0);
}
else if(temp1<200){
motor_minus(0,0);
}
delay(5);
//servo 2
temp2=analogRead(Y1);
Serial.print("Y1 ");
Serial.println(temp2);
if(temp2>800){
motor_plus(1,1);
}
else if(temp2<200){
motor_minus(1,1);
}
delay(5);
//Serial.println(digitalRead(BUTTON));//读按键值,串口显示
}
arduino吹泡泡机
于 2018-04-16 17:18:59 首次发布
这个博客介绍了如何使用Arduino来控制伺服电机实现吹泡泡机的功能。通过读取A0到A3引脚的模拟输入值,根据设定的阈值控制伺服电机正反转,调整吹泡泡机的角度。同时,博客中定义了电机转动函数和伺服电机控制函数,用于精确调整电机位置。在setup()函数中初始化了伺服电机和串口通信,而在loop()函数中实现了持续读取传感器值并相应地调整电机角度。
摘要由CSDN通过智能技术生成