arduino吹泡泡机

#include 

#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)