-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.ino
120 lines (104 loc) · 2.85 KB
/
project.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>
SoftwareSerial BluetoothSerial(0, 1); // RX|TX 7 8
char character;
String btInput = "";
// PWM: 3, 5, 6, 9, 10, and 11
int leftPin = 5;
int leftDir = 6;
int rightPin = 9;
int rightDir = 10;
int upDownPin = 11;
int upDownDir = 12;
int pwmValues[6];
void setup() {
Serial.begin(9600);
BluetoothSerial.begin(9600);
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);
pinMode(upDownPin, OUTPUT);
pinMode(leftDir, OUTPUT);
pinMode(rightDir, OUTPUT);
pinMode(upDownDir, OUTPUT);
while(!Serial); // wait until serial port is ready
}
boolean isAllNumber(String base) {
// Check if the whole string is composed of numbers
boolean result = true;
for (int i=0; i<base.length(); i++) {
char currentChar = base.charAt(i);
if (isAlpha(currentChar) == true) {
result = false;
break;
}
else {
continue;
}
}
return result;
}
void setPWMValues(String inputString) {
// inputString format:
// "leftPin leftDir rightPin rightDir upDownPin upDownDir#"
// Store values in int to pwmValues Array
String temp;
int word = 0;
for (int i=0; i<inputString.length(); i++) {
char currentChar = inputString.charAt(i);
if (currentChar == ' ' || i == inputString.length()-1) {
if (i == inputString.length()-1) {
temp += currentChar;
}
pwmValues[word] = temp.toInt();
temp = "";
word++;
continue;
}
temp += currentChar;
}
Serial.print(inputString);
Serial.println(" SET");
}
void pwmManager() {
// Set values to pins accordingly
if ((pwmValues[0] >= 0 && pwmValues[0] <= 255) &&
(pwmValues[1] >= 0 && pwmValues[1] <= 1) &&
(pwmValues[2] >= 0 && pwmValues[2] <= 255) &&
(pwmValues[3] >= 0 && pwmValues[3] <= 1) &&
(pwmValues[4] >= 0 && pwmValues[4] <= 255) &&
(pwmValues[5] >= 0 && pwmValues[5] <= 1)) {
analogWrite(leftPin, pwmValues[0]);
digitalWrite(leftDir, pwmValues[1]);
analogWrite(rightPin, pwmValues[2]);
digitalWrite(rightDir, pwmValues[3]);
analogWrite(upDownPin, pwmValues[4]);
digitalWrite(upDownDir, pwmValues[5]);
}
else {
Serial.println("smth wrong");
}
}
void loop() {
// keep setting the values
pwmManager();
// put your main code here, to run repeatedly:
// read AT command from Terminal, then write it to Bluetooth Module
while (Serial.available() > 0) {
character = Serial.read();
// every command ends with '#'
if (character != '#') {
btInput += character;
}
else {
if (isAllNumber(btInput) == true) {
setPWMValues(btInput);
}
btInput = ""; // clear input
}
}
// read response from Bluetooth Module, then display it on Terminals
while (BluetoothSerial.available() > 0) {
character = BluetoothSerial.read();
// Nothing should happen here
// When connected with Pin 0,1
}
}