1.基础知识
2.小车转速代码
int leftX = 11;
int rightX = 12;void carInit()
{// put your setup code here, to run once:pinMode(2, OUTPUT); // 配置2口为输出引脚pinMode(3, OUTPUT); // 配置3口为输出引脚//右轮信号方向初始化pinMode(4, OUTPUT); // 配置4口为输出引脚pinMode(5, OUTPUT); // 配置5口为输出引脚
}
void qianJin() {// 小车前进的功能digitalWrite(2, LOW);analogWrite(3, 70);digitalWrite(4, LOW);analogWrite(5, 70);
}void ting() {// 小车停止的功能digitalWrite(2, LOW);analogWrite(3, 0);digitalWrite(4, LOW);analogWrite(5, 0);
}void zuoZhuan() {// 小车左转,左边电机速度慢,右边电机速度快digitalWrite(2, LOW);analogWrite(3, 80); //左digitalWrite(4, LOW);analogWrite(5, 220);//右
}void youZhuan() {// 小车右转,左边电机速度快,右边电机速度慢digitalWrite(2, LOW);analogWrite(3, 220); //左digitalWrite(4, LOW);analogWrite(5, 80);//右
}void setup() {// put your setup code here, to run once:carInit();pinMode(leftX,INPUT);//设置输入模式pinMode(rightX,INPUT);
}void loop() {// put your main code here, to run repeatedly://什么时候该左转//左循迹模块的电平表现为高电平,右循迹模块表现为低电平if( digitalRead(leftX) == 1 && digitalRead(rightX) == 0 ){zuoZhuan();}//什么时候右转//右循迹高电平,左循迹低电平if( digitalRead(leftX) == 0 && digitalRead(rightX) == 1 ){youZhuan(); }//什么时候前进//左右都为低电平if( digitalRead(leftX) == 0 && digitalRead(rightX) == 0 ){qianJin(); }//什么时候停//左右都为高电平if( digitalRead(leftX) == 1 && digitalRead(rightX) == 1 ){ting(); }
}