里面包含以下3个程序
步进电机
花式运动
红外遥控
步进电机的驱动需要LN298步进电机驱动器
智能小车的花式运动
红外遥控智能小车做相对应的运动
下面是红外遥控的代码:
- #include //包含红外库
- int RECV_PIN = A4;//端口声明
- IRrecv irrecv(RECV_PIN);
- decode_results results;//结构声明
- int on = 0;//标志位
- unsigned long last = millis();
- long run_car = 0x00FF18E7;//按键2
- long back_car = 0x00FF4AB5;//按键8
- long left_car = 0x00FF10EF;//按键4
- long right_car = 0x00FF5AA5;//按键6
- long stop_car = 0x00FF38C7;//按键5
- long left_turn = 0x00ff30CF;//按键1
- long right_turn = 0x00FF7A85;//按键3
- //==============================
- int Left_motor_back=8; //左电机后退(IN1)
- int Left_motor_go=9; //左电机前进(IN2)
- int Right_motor_go=10; // 右电机前进(IN3)
- int Right_motor_back=11; // 右电机后退(IN4)
- int led1=13;
- int led2=12;
- void setup()
- {
- //初始化电机驱动IO为输出方式
- pinMode(led1,OUTPUT);
- pinMode(led2,OUTPUT);
- pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
- pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
- pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
- pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
- pinMode(13, OUTPUT);////端口模式,输出
- Serial.begin(9600); //波特率9600
- irrecv.enableIRIn(); // Start the receiver
- }
- void run() // 前进
- {
- digitalWrite(Right_motor_go,HIGH); // 右电机前进
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,200);//PWM比例0~255调速,左右轮差异略增减
- //analogWrite(Right_motor_back,0);
- digitalWrite(Left_motor_go,HIGH); // 左电机前进
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,200);//PWM比例0~255调速,左右轮差异略增减
- //analogWrite(Left_motor_back,0);
- //delay(time * 100); //执行时间,可以调整
- }
- void brake() //刹车,停车
- { digitalWrite(led1,HIGH);
- digitalWrite(led2,HIGH);
- delay(1000);
- digitalWrite(led1,LOW);
- digitalWrite(led2,LOW);
- digitalWrite(Right_motor_go,LOW);
- digitalWrite(Right_motor_back,LOW);
- digitalWrite(Left_motor_go,LOW);
- digitalWrite(Left_motor_back,LOW);
- //delay(time * 100);//执行时间,可以调整
- }
- void left() //左转(左轮不动,右轮前进)
- { digitalWrite(led2,HIGH);
- delay(500);
- digitalWrite(led2,LOW);
- delay(500);
- digitalWrite(Right_motor_go,HIGH); // 右电机前进
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,200);
- //analogWrite(Right_motor_back,0);//PWM比例0~255调速
- digitalWrite(Left_motor_go,LOW); //左轮不动
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,0);
- //analogWrite(Left_motor_back,0);//PWM比例0~255调速
- //delay(time * 100); //执行时间,可以调整
- }
- void spin_left() //左转(左轮后退,右轮前进)
- {
- digitalWrite(Right_motor_go,HIGH); // 右电机前进
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,200);
- //analogWrite(Right_motor_back,0);//PWM比例0~255调速
- digitalWrite(Left_motor_go,LOW); //左轮后退
- digitalWrite(Left_motor_back,HIGH);
- //analogWrite(Left_motor_go,0);
- //analogWrite(Left_motor_back,200);//PWM比例0~255调速
- //delay(time * 100); //执行时间,可以调整
- }
- void right() //右转(右轮不动,左轮前进)
- { digitalWrite(led1,HIGH);
- delay(500);
- digitalWrite(led1,LOW);
- digitalWrite(Right_motor_go,LOW); //右电机不动
- digitalWrite(Right_motor_back,LOW);
- //analogWrite(Right_motor_go,0);
- //analogWrite(Right_motor_back,0);//PWM比例0~255调速
- digitalWrite(Left_motor_go,HIGH);//左电机前进
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,200);
- //analogWrite(Left_motor_back,0);//PWM比例0~255调速
- //delay(time * 100); //执行时间,可以调整
- }
- void spin_right() //右转(右轮后退,左轮前进)
- {
- digitalWrite(Right_motor_go,LOW); //右电机后退
- digitalWrite(Right_motor_back,HIGH);
- //analogWrite(Right_motor_go,0);
- //analogWrite(Right_motor_back,200);//PWM比例0~255调速
- digitalWrite(Left_motor_go,HIGH);//左电机前进
- digitalWrite(Left_motor_back,LOW);
- //analogWrite(Left_motor_go,200);
- //analogWrite(Left_motor_back,0);//PWM比例0~255调速
- //delay(time * 100); //执行时间,可以调整
- }
- void back() //后退
- { digitalWrite(led1,HIGH);
- digitalWrite(led2,HIGH);
- delay(100);
- digitalWrite(led1,LOW);
- digitalWrite(led2,LOW);
- delay(100);
- digitalWrite(Right_motor_go,LOW); //右轮后退
- digitalWrite(Right_motor_back,HIGH);
- //analogWrite(Right_motor_go,0);
- //analogWrite(Right_motor_back,150);//PWM比例0~255调速
- digitalWrite(Left_motor_go,LOW); //左轮后退
- digitalWrite(Left_motor_back,HIGH);
- //analogWrite(Left_motor_go,0);
- //analogWrite(Left_motor_back,150);//PWM比例0~255调速
- //delay(time * 100); //执行时间,可以调整
- }
- void dump(decode_results *results)
- {
- int count = results->rawlen;
- if (results->decode_type == UNKNOWN)
- {
- //Serial.println("Could not decode message");
- brake();
- }
- }
- void loop()
- {
- if (irrecv.decode(&results)) //调用库函数:解码
- {
- // If it's been at least 1/4 second since the last
- // IR received, toggle the relay
- if (millis() - last > 250) //确定接收到信号
- {
- on = !on;//标志位置反
- digitalWrite(13, on ? HIGH : LOW);//板子上接收到信号闪烁一下led
- dump(&results);//解码红外信号
- }
- if (results.value == run_car )//按键2
- {digitalWrite(led1,HIGH);
- digitalWrite(led2,HIGH);delay(500);run();}//前进
- if (results.value == back_car )//按键8
- {back();digitalWrite(led1,HIGH);digitalWrite(led2,HIGH);}//后退
- if (results.value == left_car )//按键4
- {left();digitalWrite(led1,HIGH);digitalWrite(led2,LOW);}//左转
- if (results.value == right_car )//按键6
- {digitalWrite(led1,LOW);digitalWrite(led2,HIGH); right();}//右转
- if (results.value == stop_car )//按键5
- { brake();digitalWrite(led1,LOW);digitalWrite(led2,LOW);}//停车
- if (results.value == left_turn )//按键1
- {spin_left();digitalWrite(led1,HIGH);digitalWrite(led2,LOW);}//左旋转
- if (results.value == right_turn )//按键3
- {digitalWrite(led1,LOW);digitalWrite(led2,HIGH); spin_right();}//右旋转
- last = millis();
- irrecv.resume(); // Receive the next value
- }
- }
0
|
|
|
|