本文主要是介绍3.控速,红外传感,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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(); }
}
这篇关于3.控速,红外传感的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!