-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetoothRC.ino
120 lines (103 loc) · 3.31 KB
/
BluetoothRC.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
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
#include <SoftwareSerial.h>
/*
Project: accelerometer controlled rc car
Date: Oct 13, 2015
Author: Christian Baker
Description: Firmware used on arduino controlled RC car. Uses data from any devices
accelerometer to control the movement of the car
*/
#include <AFMotor.h>
#define START_CMD '>'
#define DEBUG 1
String inText;
float value0, value1, value2;
int forspeed = 255;
SoftwareSerial mySerial(2, NULL);
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("Starting accelerometer..");
motor1.setSpeed(250);
motor2.setSpeed(250);
motor3.setSpeed(250);
motor4.setSpeed(250);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void loop() {
mySerial.flush();
unsigned long logCount = 0L;
char val= ' '; //Read the serial
if (mySerial.available() > 0) {
while (mySerial.available() > 0) {
value0 = mySerial.parseFloat();
value1 = mySerial.parseFloat();
value2 = mySerial.parseFloat();
if (DEBUG) {
Serial.print("Val0: ");
Serial.println(value0);
Serial.print("Val1: ");
Serial.println(value1);
Serial.print("Val2: ");
Serial.println(value2);
}
// Stop motors if device is held in neutral position
if (value0 > 9 && value0 < 10 && value2 > 0 && value2 < 1 ) {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
} else if (value0 < 9 && value2 > 1 && value1 > -3 && value1 < 3) {
// Move Forward if device is tilted forward
forspeed = value2;
forspeed = map(forspeed, 1, 9, 100, 255);
motor1.setSpeed(forspeed);
motor2.setSpeed(forspeed);
motor3.setSpeed(forspeed);
motor4.setSpeed(forspeed);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
} else if (value1 < -3) {
// Turn Left if device is tilted left
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
} else if (value1 > 3) {
// Turn Right if device is tilted right
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
} else if (value0 < 9 && value2 < 0 && value1 > -3 && value1 < 3) {
// Move backward if device is tilted back
forspeed = value2;
forspeed = map(forspeed, -9, -1, 255, 100);
motor1.setSpeed(forspeed);
motor2.setSpeed(forspeed);
motor3.setSpeed(forspeed);
motor4.setSpeed(forspeed);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
}
}
}
}