1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| #include <SoftwareSerial.h> SoftwareSerial BTSerial(7, 3);
int STBY = 10; int A_PWM = 5; int A_IN1 = 9; int A_IN2 = 8; int B_PWM = 6; int B_IN1 = 11; int B_IN2 = 12;
int L_MaxSpeed = 255; int R_MaxSpeed = 255; int L_TurnSpeed = 128; int R_TurnSpeed = 128;
int LR_Direct = 0;
#define TRIG 2 #define ECHO 4
char val; long val_distance;
void setup() { pinMode(A_PWM, OUTPUT); pinMode(A_IN1, OUTPUT); pinMode(A_IN2, OUTPUT); pinMode(B_PWM, OUTPUT); pinMode(B_IN1, OUTPUT); pinMode(B_IN2, OUTPUT); pinMode(STBY, OUTPUT); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); Serial.begin(9600); BTSerial.begin(9600);
while (!BTSerial.available()) { stop(); } }
void loop() { distance(); Serial.println(val_distance);
if (BTSerial.available()) { val = BTSerial.read(); Serial.println(val); } if (val_distance > 50) { parseCommand(val); } else { stop(); delay(20); b_stop(); } delay(50); }
void parseCommand(char input) { switch (input) {
case 'F': go_forward(); break; case 'B': go_backward(); break; case 'L': if(LR_Direct==0) { go_f_left(); } else if(LR_Direct==1) { go_b_left(); } break; case 'R': if(LR_Direct==0) { go_f_right(); } else if(LR_Direct==1) { go_b_right(); } break; case 'O': stop(); break; } delay(50); }
void distance() { digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
val_distance = pulseIn(ECHO, HIGH) / 58.2; }
void move(int motorLR, int speed, boolean inPin1, boolean inPin2) { digitalWrite(STBY, HIGH);
if (motorLR == 0) { analogWrite(A_PWM, speed); digitalWrite(A_IN1, inPin1); digitalWrite(A_IN2, inPin2); }
if (motorLR == 1) { analogWrite(B_PWM, speed); digitalWrite(B_IN1, inPin1); digitalWrite(B_IN2, inPin2); } }
void go_forward() { Serial.println("F"); move(0, L_MaxSpeed, 0, 1); move(1, R_MaxSpeed, 0, 1); LR_Direct = 0; delay(100); }
void go_backward() { Serial.println("B"); move(0, L_MaxSpeed, 1, 0); move(1, R_MaxSpeed, 1, 0); LR_Direct = 1; delay(100); }
void go_f_left() { Serial.println("F_L"); move(0, L_TurnSpeed, 0, 1); move(1, R_MaxSpeed, 0, 1); delay(100); }
void go_b_left() { Serial.println("B_R"); move(0, L_TurnSpeed, 1, 0); move(1, R_MaxSpeed, 1, 0); delay(100); }
void go_f_right() { Serial.println("F_R"); move(0, L_MaxSpeed, 0, 1); move(1, R_TurnSpeed, 0, 1); delay(100); }
void go_b_right() { Serial.println("B_R"); move(0, L_MaxSpeed, 1, 0); move(1, R_TurnSpeed, 1, 0); delay(100); }
void b_stop() { Serial.println("B_STOP"); while (val_distance < 50) { go_backward(); delay(100); distance(); delay(10); } while (!BTSerial.available()) { stop(); } LR_Direct = 0; }
void stop() { Serial.println("STOP"); digitalWrite(STBY, LOW); }
|