查看: 13|回复: 0
打印 上一主题 下一主题

[未解决] arduino小车刚接通电源原地打转

[复制链接]

该用户从未签到

跳转到指定楼层
楼主
发表于 2019-9-27 19:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
刚学arduino,小车接通电源会先原地打转一圈再前进,这是什么问题?
#define FORWARD? ?0
#define TURNLEFT??1
#define TURNRIGHT 2
#define STOP? ?? ?3
int trac1 = 16;
int trac2 = 15;
int trac3 = 14;
int trac4 = 0;
int trac5 = 1;
int trac6 = 2;
int trac7 = 3;
int MotorRight1 = 5;//紫
int MotorRight2 = 7;//灰
int MotorLeft1 = 4;//橙
int MotorLeft2 = 6;//红
int MotorR1PWM = 9;//紫
int MotorR2PWM = 11;//黄
int MotorL1PWM = 8;//蓝
int MotorL2PWM = 10;//绿
int tracL= 20;//三路L
int tracC= 19;//三路C
int tracR= 18;//三路R
int sensor[7];
int flag = 0;
void setup() {
??// put your setup code here, to run once:
Serial.begin(9600);
??pinMode(MotorRight1, OUTPUT);
??pinMode(MotorRight2, OUTPUT);
??pinMode(MotorLeft1, OUTPUT);
??pinMode(MotorLeft2, OUTPUT);
??pinMode(MotorL1PWM, OUTPUT);
??pinMode(MotorL2PWM, OUTPUT);
??pinMode(MotorR1PWM, OUTPUT);
??pinMode(MotorR2PWM, OUTPUT);
??pinMode(trac1, INPUT);
??pinMode(trac2, INPUT);
??pinMode(trac3, INPUT);
??pinMode(trac4, INPUT);
??pinMode(trac5, INPUT);
??pinMode(trac6, INPUT);
??pinMode(trac7, INPUT);
??pinMode(tracL, INPUT);
??pinMode(tracC, INPUT);
??pinMode(tracR, INPUT);
}
void motor_control(int cmd) {
??switch (cmd) {
? ? case FORWARD:
? ?? ?analogWrite(MotorL1PWM,60);
? ?? ?analogWrite(MotorL2PWM,60);
? ?? ?analogWrite(MotorR1PWM,60);
? ?? ?analogWrite(MotorR2PWM,60);
? ?? ?digitalWrite(MotorRight1, HIGH);
? ?? ?digitalWrite(MotorRight2, HIGH);
? ?? ?digitalWrite(MotorLeft1, LOW);
? ?? ?digitalWrite(MotorLeft2, LOW);
? ?? ?Serial.println("FORWARD");
? ?? ?break;
? ? case TURNLEFT:
? ?? ?digitalWrite(MotorRight1, HIGH);
? ?? ?digitalWrite(MotorRight2, HIGH);
? ?? ?digitalWrite(MotorLeft1, LOW);
? ?? ?digitalWrite(MotorLeft2, LOW);
? ?? ?analogWrite(MotorL1PWM, 30);
? ?? ?analogWrite(MotorL2PWM, 30);
? ?? ?analogWrite(MotorR1PWM, 60);
? ?? ?analogWrite(MotorR2PWM, 60);
? ?? ?Serial.println("TURNLEFT");
? ?? ?break;
? ? case TURNRIGHT:
? ?? ?digitalWrite(MotorRight1, HIGH);
? ?? ?digitalWrite(MotorRight2, HIGH);
? ?? ?digitalWrite(MotorLeft1, LOW);
? ?? ?digitalWrite(MotorLeft2, LOW);
? ?? ?analogWrite(MotorL1PWM, 60);
? ?? ?analogWrite(MotorL2PWM, 60);
? ?? ?analogWrite(MotorR1PWM, 30);
? ?? ?analogWrite(MotorR2PWM, 30);
? ?? ?Serial.println("TURNRIGHT");
? ?? ?break;
? ? default:
? ?? ?digitalWrite(MotorRight1, LOW);
? ?? ?digitalWrite(MotorRight2, LOW);
? ?? ?digitalWrite(MotorLeft1, LOW);
? ?? ?digitalWrite(MotorLeft2, LOW);
? ?? ?analogWrite(MotorL1PWM, 0);
? ?? ?analogWrite(MotorL2PWM, 0);
? ?? ?analogWrite(MotorR1PWM, 0);
? ?? ?analogWrite(MotorR2PWM, 0);
? ?? ?Serial.println("STOP");
? ?? ?break;
??}
}
void loop() {
??// put your main code here, to run repeatedly:
? ?? ? motor_control( FORWARD);
}
您需要登录后才可以回帖 登录 | 立即注册 ?

本版积分规则

热门推荐

关于温湿度传感器在LCD显示上的问题
关于温湿度传感器在LCD显
我建立了一个程序,想要将dht11传感器得到的数据显示在LCD1602上,但是LCD显的都是乱
【原创】全球最小口袋3D打印机mini one直播教程贴
【原创】全球最小口袋3D打
最近闲得蛋疼,没事搞个掌上3D打印机,先放效果图吧。 搞了半天,终于能正常打印,
【Arduino】108种传感器模块系列实验(74)--- 4位LED数码管模块
【Arduino】108种传感器模
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是
(开源)ESP8266改装小风扇,app远程控制+天猫精灵控制
(开源)ESP8266改装小风
材料准备 [*]USB风扇一个 [*]ESP8266一块 [*]3.3继电器一块 拆解风扇 第一次,没有
基于机器视觉的智能分拣机械臂
基于机器视觉的智能分拣机
本作品是一个基于机器视觉的智能机械臂操作平台,其采用atmega32单片机作为主控制系统
Copyright ???2015-2016??Arduino中文社区??Powered by?Discuz! ??( 蜀ICP备14017632号-3 )
快速回复 返回顶部 返回列表