第一个程序是寻迹小车程序,第二个程序是舵机旋转程序,想在小车运行一段时间以后,小车停止,将舵机运行,舵机运行一段时间以后,舵机停止,将小车继续运行一段时间。求助代码
以下是小车代码和舵机代码
小车代码:
//#include <Servo.h>
int Left_motor_go=8; //左电机前进(IN1)
int Left_motor_back=9; //左电机后退(IN2)
int Right_motor_go=10; // 右电机前进(IN3)
int Right_motor_back=11; // 右电机后退(IN4)
int key=A2;//定义按键 数字A2 接口
int beep=A3;//定义蜂鸣器 数字A3 接口
const int SensorRight = 3; //右循迹红外传感器(P3.2 OUT1)
const int SensorLeft = 4; //左循迹红外传感器(P3.3 OUT2)
int SL; //左循迹红外传感器状态
int SR; //右循迹红外传感器状态
void setup()
{
//初始化电机驱动IO为输出方式
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(key,INPUT);//定义按键接口为输入接口
pinMode(beep,OUTPUT);
pinMode(SensorRight, INPUT); //定义右循迹红外传感器为输入
pinMode(SensorLeft, INPUT); //定义左循迹红外传感器为输入
}
//=======================智能小车的基本动作=========================
//void run(int time) // 前进
void run()
{
digitalWrite(Right_motor_go,HIGH); // 右电机前进
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,60);//PWM比例0~255调速,左右轮差异略增减
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,LOW); // 左电机前进
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);//PWM比例0~255调速,左右轮差异略增减
analogWrite(Left_motor_back,60);
//delay(time * 100); //执行时间,可以调整
}
//void brake(int time) //刹车,停车
void brake()
{
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(int time) //左转(左轮不动,右轮前进)
void left()
{
digitalWrite(Right_motor_go,HIGH); // 右电机前进
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,60);
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,60);
analogWrite(Right_motor_back,0);//PWM比例0~255调速
digitalWrite(Left_motor_go,HIGH); //左轮后退
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,30);
analogWrite(Left_motor_back,0);//PWM比例0~255调速
//delay(time * 100); //执行时间,可以调整
}
//void right(int time) //右转(右轮不动,左轮前进)
void right()
{
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,LOW);//左电机前进
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,60);//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,30);//PWM比例0~255调速
digitalWrite(Left_motor_go,LOW);//左电机前进
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,60);//PWM比例0~255调速
//delay(time * 100); //执行时间,可以调整
}
//void back(int time) //后退
void back(int time)
{
digitalWrite(Right_motor_go,LOW); //右轮后退
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,60);//PWM比例0~255调速
digitalWrite(Left_motor_go,HIGH); //左轮后退
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,60);
analogWrite(Left_motor_back,0);//PWM比例0~255调速
delay(time * 100); //执行时间,可以调整
}
//==========================================================
void keysacn()//按键扫描
{
int val;
val=digitalRead(key);//读取数字7 口电平值赋给val
while(!digitalRead(key))//当按键没被按下时,一直循环
{
val=digitalRead(key);//此句可省略,可让循环跑空
}
while(digitalRead(key))//当按键被按下时
{
delay(10); //延时10ms
val=digitalRead(key);//读取数字7 口电平值赋给val
if(val==HIGH) //第二次判断按键是否被按下
{
digitalWrite(beep,HIGH); //蜂鸣器响
while(!digitalRead(key)) //判断按键是否被松开
digitalWrite(beep,LOW); //蜂鸣器停止
}
else
digitalWrite(beep,LOW);//蜂鸣器停止
}
}
void loop()
{
keysacn();//调用按键扫描函数
while(1)
{
//有信号为LOW 没有信号为HIGH 检测到黑线 输出高 检测到白色区域输出低
SR = digitalRead(SensorRight);//有信号表明在白色区域,车子底板上L1亮;没信号表明压在黑线上,车子底板上L1灭
SL = digitalRead(SensorLeft);//有信号表明在白色区域,车子底板上L2亮;没信号表明压在黑线上,车子底板上L2灭
if (SL == LOW&&SR==LOW)
run(); //调用前进函数
else if (SL == HIGH & SR == LOW)// 左循迹红外传感器,检测到信号,车子向右偏离轨道,向左转
spin_left();
else if (SR == HIGH & SL == LOW) // 右循迹红外传感器,检测到信号,车子向左偏离轨道,向右转
spin_right();
else // 都是黑色, 停止
brake();
}
}
舵机代码:
#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(9);
}
void loop()
{
myservo.writeMicroseconds(500); // 舵机顺时针旋转
delay(2000);
myservo.writeMicroseconds(2500);// 舵机逆时针旋转
delay(2000);
}
|