-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasecode.ino
84 lines (80 loc) · 2.01 KB
/
basecode.ino
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
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
#include<AFMotor.h>
AF_DCMotor motor_r(3);
AF_DCMotor motor_l(4);
RH_ASK driver;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
motor_l.setSpeed(255);
motor_l.run(RELEASE);
motor_r.setSpeed(255);
motor_r.run(RELEASE);
//motor_f.setSpeed(255);
//motor_f.run(RELEASE);
driver.init();
}
void loop() {
uint8_t buf[20];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) {
Serial.print("Message : ");
Serial.println((char*)buf);
int w = buf[0] - '0';
int a = buf[1] - '0';
int s = buf[2] - '0';
int d = buf[3] - '0';
int speedx = buf[4] * 100 + buf[5] * 10 + buf[6];
Serial.print("W:");
Serial.println(w);
Serial.print("A:");
Serial.println(a);
Serial.print("S:");
Serial.println(s);
Serial.print("D:");
Serial.println(d);
if (w == 1 && a == 1) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_r.run(RELEASE);
motor_l.run(FORWARD);
}
else if (w == 1 && d == 1) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_r.run(FORWARD);
motor_l.run(RELEASE);
}
else if (s == 1 && a == 1) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_l.run(BACKWARD);
motor_r.run(RELEASE);
} else if (s == 1 && d == 1) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_l.run(RELEASE);
motor_r.run(BACKWARD);
}
else if (w == 1) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_l.run(FORWARD);
motor_r.run(FORWARD);
}
else if (s == 1) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_r.run(BACKWARD);
motor_l.run(BACKWARD);
}
else if (w == 0 && s == 0 && a == 0 && d == 0) {
motor_l.setSpeed(speedx);
motor_r.setSpeed(speedx);
motor_l.run(RELEASE);
motor_r.run(RELEASE);
}
delay(600);
}
}