4415| 11
|
[项目] 拉风的自走车 |
本帖最后由 gada888 于 2020-8-4 12:55 编辑 见多了自动小车,有点审美疲劳了。就想到了DIY一个自走车。于是买了铝条和螺丝,折腾了一下午,弄出了个粗糙的玩意。结果不太满意,但过程充满了汗水。功能是通过超声传感探测到前方物体后开始转弯。由于负重较大,走的还比较笨拙。这个后面会调整。 硬件清单见以下图例 Arduino nano L298N 红外避障传感 超声传感 其他一些电线,螺丝,电机等附件 项目连线图 [mw_shl_code=applescript,false]/* ReModified by gada888 */ #include<NewPing.h> int ENA = 3; int ENB = 9; int MOTOR_A1 = 4; int MOTOR_A2 = 5; int MOTOR_B1 = 6; int MOTOR_B2 = 7; int RIGHT = A0; int LEFT = A1; #define TRIG A2 #define ECHO A3 #define MAX_DISTANCE 100 NewPing sonar(TRIG, ECHO, MAX_DISTANCE); void setup() { // put your setup code here, to run once: pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT); pinMode(MOTOR_A1, OUTPUT); pinMode(MOTOR_A2, OUTPUT); pinMode(MOTOR_B1, OUTPUT); pinMode(MOTOR_B2, OUTPUT); pinMode(RIGHT, INPUT); pinMode(LEFT, INPUT); } void loop() { // put your main code here, to run repeatedly: delay(70); int distance = sonar.ping_cm(); if (distance == 0) { distance = 30; } if(distance <=15) { Stop(); delay(100); turnRight(); delay(350); moveForward(); delay(500); turnLeft(); delay(350); moveForward(); delay(700); turnLeft(); delay(300); moveForward(); delay(400); turnRight(); delay(400); } if (analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) { analogWrite(ENA, 100); analogWrite(ENB, 100); digitalWrite(MOTOR_A1, LOW); digitalWrite(MOTOR_A2, HIGH); digitalWrite(MOTOR_B1, HIGH); digitalWrite(MOTOR_B2, LOW); }else if (analogRead(RIGHT)<=35 && !analogRead(LEFT)<=35) { analogWrite(ENA, 100); analogWrite(ENB, 100); digitalWrite(MOTOR_A1, HIGH); digitalWrite(MOTOR_A2, LOW); digitalWrite(MOTOR_B1, HIGH); digitalWrite(MOTOR_B2, LOW); }else if (!analogRead(RIGHT)<=35 && analogRead(LEFT)<=35) { analogWrite(ENA, 100); analogWrite(ENB, 100); digitalWrite(MOTOR_A1, LOW); digitalWrite(MOTOR_A2, HIGH); digitalWrite(MOTOR_B1, LOW); digitalWrite(MOTOR_B2, HIGH); }else if (!analogRead(RIGHT)<=35 && !analogRead(LEFT)<=35) { Stop(); } } void Stop() { analogWrite(ENA, 0); analogWrite(ENB, 0); digitalWrite(MOTOR_A1, LOW); digitalWrite(MOTOR_A2, LOW); digitalWrite(MOTOR_B1, LOW); digitalWrite(MOTOR_B2, LOW); } void turnRight() { analogWrite(ENA, 100); analogWrite(ENB, 100); digitalWrite(MOTOR_A1, LOW); digitalWrite(MOTOR_A2, HIGH); digitalWrite(MOTOR_B1, LOW); digitalWrite(MOTOR_B2, HIGH); } void turnLeft() { analogWrite(ENA,100); analogWrite(ENB, 100); digitalWrite(MOTOR_A1, HIGH); digitalWrite(MOTOR_A2, LOW); digitalWrite(MOTOR_B1, HIGH); digitalWrite(MOTOR_B2, LOW); } void moveForward() { analogWrite(ENA, 100); analogWrite(ENB, 100); digitalWrite(MOTOR_A1, LOW); digitalWrite(MOTOR_A2, HIGH); digitalWrite(MOTOR_B1, HIGH); digitalWrite(MOTOR_B2, LOW); } [/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed