白桦的天空

第一次的心动,永远的心痛!
  首页  :: 新随笔  :: 联系 :: 管理

智能小车代码

Posted on 2013-01-14 17:06  白桦的天空  阅读(1008)  评论(0编辑  收藏  举报

#include "Syn6288.h"
Syn6288 syn;
uint8_t text1[]={0xD0,0xA1,0xC5,0xA3,0xC5,0xA3,0xC7,0xB0,0xBD,0xF8}; //小牛牛前进
uint8_t text2[]={0xC7,0xEB,0xD7,0xA2,0xD2,0xE2,0xB5,0xB9,0xB3,0xB5}; //请注意倒车
uint8_t text3[]={0xD7,0xF3,0xD7,0xAA};//左转
uint8_t text4[]={0xD3,0xD2,0xD7,0xAA};//左转
unsigned char PWM = 255;
int incomingByte = 0;
int speed=254;
//Right 2
int pinA1=23;
int pinA2=22;
int speedpinA=3;//定义EA(PWM调速)接口

//Left 2
int pinB1=24;
int pinB2=25;
int speedpinB=2;//定义EB(PWM调速)接口

//Right 1
int pinC1=26;
int pinC2=27;
int speedpinC=5;//定义EC(PWM调速)接口

//Left 1
int pinD1=28;
int pinD2=29;
int speedpinD=4;//定义ED(PWM调速)接口

int D0=33;
int D1=32;
int D2=31;
int D3=30;

void setup()
{
pinMode(pinA1,OUTPUT);
pinMode(pinA2,OUTPUT);
pinMode(pinB1,OUTPUT);
pinMode(pinB2,OUTPUT);
pinMode(pinC1,OUTPUT);
pinMode(pinC2,OUTPUT);
pinMode(pinD1,OUTPUT);
pinMode(pinD2,OUTPUT);

pinMode(D0,INPUT);
pinMode(D1,INPUT);
pinMode(D2,INPUT);
pinMode(D3,INPUT);
Serial.begin(9600);
}
void loop()
{

int D0_Value=digitalRead(D0);
int D1_Value=digitalRead(D1);
int D2_Value=digitalRead(D2);
int D3_Value=digitalRead(D3);
if(D1_Value==HIGH)
{
back(speed);

syn.play(text2,sizeof(text2),3);

Serial.println("back");

}
if(D0_Value==HIGH)
{

advance(speed);
syn.play(text1,sizeof(text1),3);
Serial.println("advance");
}
if(D2_Value==HIGH)
{
left(speed);

syn.play(text3,sizeof(text3),3);

Serial.println("left");
}
if(D3_Value==HIGH)
{
syn.play(text4,sizeof(text4),3);
right(speed);
Serial.println("right");
}
if(D0_Value==LOW && D1_Value==LOW && D2_Value==LOW && D3_Value==LOW)
{
stop();
}
}

void advance(int a)//前进
{
analogWrite(speedpinA,a);
analogWrite(speedpinB,a);
analogWrite(speedpinC,a);
analogWrite(speedpinD,a);
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,HIGH);
digitalWrite(pinB1,LOW);
digitalWrite(pinB2,HIGH);
digitalWrite(pinC1,LOW);
digitalWrite(pinC2,HIGH);
digitalWrite(pinD1,LOW);
digitalWrite(pinD2,HIGH);
}

void back(int a)//后退
{
analogWrite(speedpinA,a);
analogWrite(speedpinB,a);
analogWrite(speedpinC,a);
analogWrite(speedpinD,a);
digitalWrite(pinA1,HIGH);
digitalWrite(pinA2,LOW);
digitalWrite(pinB1,HIGH);
digitalWrite(pinB2,LOW);
digitalWrite(pinC1,HIGH);
digitalWrite(pinC2,LOW);
digitalWrite(pinD1,HIGH);
digitalWrite(pinD2,LOW);
}

void left(int a)//左转
{
analogWrite(speedpinA,a);
analogWrite(speedpinB,a);
analogWrite(speedpinC,a);
analogWrite(speedpinD,a);
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,HIGH);
digitalWrite(pinB1,HIGH);
digitalWrite(pinB2,LOW);
digitalWrite(pinC1,LOW);
digitalWrite(pinC2,HIGH);
digitalWrite(pinD1,HIGH);
digitalWrite(pinD2,LOW);
}

void right(int a)//右转
{
analogWrite(speedpinA,a);
analogWrite(speedpinB,a);
analogWrite(speedpinC,a);
analogWrite(speedpinD,a);
digitalWrite(pinA1,HIGH);
digitalWrite(pinA2,LOW);
digitalWrite(pinB1,LOW);
digitalWrite(pinB2,HIGH);
digitalWrite(pinC1,HIGH);
digitalWrite(pinC2,LOW);
digitalWrite(pinD1,LOW);
digitalWrite(pinD2,HIGH);
}

void stop()//停止
{
digitalWrite(pinA1,LOW);
digitalWrite(pinA2,LOW);
digitalWrite(pinB1,LOW);
digitalWrite(pinB2,LOW);
digitalWrite(pinC1,LOW);
digitalWrite(pinC2,LOW);
digitalWrite(pinD1,LOW);
digitalWrite(pinD2,LOW);
}