科技网

当前位置: 首页 >IT

[ 单片机 ] 【AAA】单片机 小车

IT
来源: 作者: 2018-09-14 17:02:50

[ 单片机 ] 【AAA】单片机 小车

这个单片机小车,是不是很酷,下面我就手把手叫大家怎么制作

这个项目需要的制作材料【清单】:

- LM 272运算放大器

——电阻(3)

* 1 k欧姆(2)

* 470欧姆

- 2 n3904 NPN型晶体管(2)

- 2 n3906 PNP晶体管(3)

- Arduino UNO

- 9伏电池

- 1 n4148二极管(2)

- 5.6 nF电容器(2)

-电线(大约10英尺)

——电气工具(即。——焊接烙铁,断线钳等)。

-直流电机(2)

- Arduino电力电缆

插入芯片

第二部分

第三:LED等部分的连接

第四:电动机驱动

第五:电动机晶体管开关

第六:安装电机

第七:添加开关

第八:手动模式配置(GOOD,已经成型了)

第九:自动模式配置

第十:检查一下,排除故障(一定要细心)

第十一:代码检查,略

完成,你也可以修改代码

代码:

int analogleft = A0;int analogright = A1;int mode_1 = 2;int mode_2 = 3;int mode_3 = 4;int mode_4 = 5;int digleft = 6;int digright = 7;int trig1 = 8;int trig2 = 9;int trig3 = 12;int mot1 = 10;int mot2 = 11;int error = 13;void setup(){ // put your setup code here, to run once: pinMode (mode_1, INPUT); pinMode (mode_2, INPUT); pinMode (mode_3, INPUT); pinMode (mode_4, INPUT); pinMode (digleft, INPUT); pinMode (digright, INPUT); pinMode (trig1, OUTPUT); pinMode (trig2, OUTPUT); pinMode (trig3, OUTPUT); pinMode (mot1, OUTPUT); pinMode (mot2, OUTPUT); pinMode (error, OUTPUT);}void sensor_trig(char a) //chooses sensor in automatic mode{ if (a == 1) { digitalWrite(trig1, 0); digitalWrite(trig2, 1); digitalWrite(trig3, 1); } if (a == 2) { digitalWrite(trig1, 1); digitalWrite(trig2, 0); digitalWrite(trig3, 1); } if (a == 3) { digitalWrite(trig1, 1); digitalWrite(trig2, 1); digitalWrite(trig3, 0); }}void shutdown(){ digitalWrite(mot1, 0); digitalWrite(mot2, 0); digitalWrite(trig1, 0); digitalWrite(trig2, 0); digitalWrite(trig3, 0); rep: digitalWrite(error, 1); delay(250); digitalWrite(error, 0); delay(250); goto rep;}void manual_run() // executes program in manual mode{ char i = digitalRead(digleft); char j = digitalRead(digright); int a = analogRead(A0); int b = analogRead(A1); int c = abs(a - b); if (i == 1) { if (j == 1) { shutdown(); } if (c <= 50) { digitalWrite(mot1, 1); digitalWrite(mot2, 1); delay (300); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } else { digitalWrite(mot2, 1); delay (200); digitalWrite(mot1, 1); delay (175); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } } if (j == 1) { if (i == 1) { shutdown(); } if (c <= 50) { digitalWrite(mot1, 1); digitalWrite(mot2, 1); delay (300); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } else { digitalWrite(mot2, 1); delay (200); digitalWrite(mot1, 1); delay (175); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } }}void mode_select(){ repeat: char i = digitalRead(mode_1); char j = digitalRead(mode_2); if (i == 0) { if (j == 0) { manual_run(); } if (j == 1); { digitalWrite (error, HIGH); goto repeat; } } if (i == 1) { if (j == 1) { shutdown(); } if (j == 0) { auto_run(); } }}void auto_run() //executes program in automatic mode{ byte i = digitalRead (digleft); byte j = digitalRead (digright); int a = analogRead (A0); int b = analogRead (A1); int c = abs (a - b); sensor_select(); if (i == 1) { if (c <= 50) { digitalWrite(mot1, 1); digitalWrite(mot2, 1); delay (300); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } else { digitalWrite(mot2, 1); delay (200); digitalWrite(mot1, 1); delay (175); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } } if (j == 1) { if (i == 1) { shutdown(); } if (c <= 50) { digitalWrite(mot1, 1); digitalWrite(mot2, 1); delay (300); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } else { digitalWrite(mot2, 1); delay (200); digitalWrite(mot1, 1); delay (175); digitalWrite(mot1, 0); digitalWrite(mot2, 0); } }}void sensor_select (){ byte a = digitalRead (mode_3); byte b = digitalRead (mode_4); char c; if (a == 1) { if (b == 1) { shutdown(); } if (b == 0) { c = 1; } } if (a == 0) { if (b == 1) { c = 3; } if (b == 0) { c = 2; } } sensor_trig (a);}void loop() { // put your main code here, to run repeatedly: mode_select();}

张瑶与包贝尔虐狗力十足吻戏哭戏掌掴戏
徐根宝穗沪竞争在中超更在青训竞争意义更大
全区各地各部门联合开展315消费者权益日宣传

相关推荐