作为arduino的萌新,在arduino mega2560的开发板上完成了点灯、呼吸灯、流水灯、光敏、红外感应等元件。
arduino对于新手是非常友好的,基于C语言的开发,所以在将数字和模拟信号的转化中对计算机专业的学生来说是非常容易上手的。而这次arduino小车程序的书写中有了正常的电机驱动、红外循迹、超声波避障、蓝牙外部控制小车的功能,以及PWM,串口外部的监控。
由于在书写arduino的程序中,在书写代码的时,arduino不能像C语言中的编译器一样单步跟踪,所以需要对每个模块进行分别的调试。代码模块如下:
#include <Servo.h>
#define stopa 1
#define turnleft 2
#define turnright 3
#define backward 4
#define forward 5
int leftmotor1 = ;
int leftmotor2 = ;
int rightmotor1 = ;
int rightmotor2 = ;//引脚之后需要修改
int trac1 = ;
int trac2 = ;
int leftpwm = ;
int rightpwm = ;
Servo myservo ;//舵机
int inputpin = ;
int outputpin = ;
void setup ()
{
Serial.begin (9600);//串口初始化
myservo.attach();//舵机引脚初始化
pinMode ( leftmotor1, OUTPUT );//测速引脚初始化
pinMode ( leftmotor2, OUTPUT );
pinMode ( rightmotor1, OUTPUT );
pinMode ( rightmotor2, OUTPUT );
pinMode ( trac1, INPUT );//循迹引脚初始化
pinMode ( trac2, INPUT );
pinMode ( inputpin, INPUT );//超声波控制引脚初始化
pinMode (outputpin, OUTPUT );
}
void loop()
{
int cmd ;
for(cmd = 0;cmd++;cmd<5)
motorrun( cmd );
delay(2000);
if(Serial.available()>0)
{
char cmd = Serial.read();
Serial.print(cmd);
motorrun(cmd);
}
avoidance();
tracing();
}
void motorrun (int cmd,int vaule)//驱动模块
{
analogWrite( leftpwm,value );
analogWrite( rightpwm,value );
switch(cmd)
{
case stopa :
Serial.println("stopa");
digitalWrite ( leftmotor1,LOW );
digitalWrite ( leftmotor2,LOW );
digitalWrite ( rightmotor1,LOW );
digitalWrite ( rightmotor2,LOW );
break;
case turnleft :
Serial.println("turnleft");
digitalWrite ( leftmotor1,LOW );
digitalWrite ( leftmotor2,LOW );
digitalWrite ( rightmotor1,HIGH );
digitalWrite ( rightmotor2,LOW );
break;
case turnright :
Serial.println("turnright");
digitalWrite ( leftmotor1,LOW );
digitalWrite ( leftmotor2, );
digitalWrite ( rightmotor1,);
digitalWrite ( rightmotor2,);
break;
case backward :
Serial.println("backward");
digitalWrite ( leftmotor1, );
digitalWrite ( leftmotor2, );
digitalWrite ( rightmotor1,);
digitalWrite ( rightmotor2,);
break;
case forward :
Serial.println("forward");
digitalWrite ( leftmotor1, );
digitalWrite (leftmotor2,);
digitalWrite (rightmotor1,);
digitalWrite (rightmotor2,);
break;
}
}
void tracking()//循迹程序
{
int data[2];
data[0] = digitalRead();
data[1] = digitalRead();
if(data[0] == 0&&data[1] == 0)
{
motorrun(forward,200);
}
if(data[0]==1&&data[1]==0)
{
motorrun(turnright);
}
if(data[0] == 0&&data[1] == 1)
{
motorrun(turnleft);
}
if(data[0] == 1&&data[1] == 1)
{
motorrun(forward);
}
Serial.print(data[0]);
Serial.print("......");
Serial.print(data[1]);
Serial.print("......");
}
void avoidance(0//避障程序
{
int pos;
int dis[3];
motorrun(forward,200)
myservo.write(90);
dis[1] = getdistance;
if(dis[1]<30)
{
motorrun(stopa,0);
for(pos = 90;pos <= 150;pos += 1)
{
myservo.write(pos);
delay(15);
}
dis[2] = getdistance();
for(pos =150;pos>=30;pos -=1)
{
myservo.write(pos);
delay(15);
if(pos == 90)
dis[1] =getdistance();
}
dis[0] =getdistance();
for(pos = 30;pos<= 90;pos += 1)
{
myservo.write(pos);
delay(15);
}
if(dis[0]<dis[2])
{
motorrun(turnleft,250);
delay(500);
}
else
{
motorrun(turnright,250);
delay(500);
}
}
}