-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino_UART_Recieve.ino
166 lines (140 loc) · 3.72 KB
/
Arduino_UART_Recieve.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
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
#include <MotorController.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define SSD1306_LCDHEIGHT 128
#define SSD1306_LCDWIDTH 64
//Define the states of the machine
#define STOP_S 0
#define WAIT_STOP_S 1
#define FORWARD_S 2
#define MOVE_FORWARD_S 3
#define LEFT_S 4
#define TURN_LEFT_S 5
#define RIGHT_S 6
#define TURN_RIGHT_S 7
//MOTOR CONTROLLER
#define dirBPin 4 // direction //R
#define pwmBPin 3 // speed
#define dirAPin 12 // direction //L
#define pwmAPin 11 // speed
MotorController mc(dirAPin, pwmAPin, dirBPin, pwmBPin, 1, 1);
Adafruit_SSD1306 display(OLED_RESET);
char motion_command;
uint8_t fsm_state = STOP_S;
int move_cnt;
#define runEvery(t) for (static typeof(t) _lasttime;(typeof(t))((typeof(t))millis() - _lasttime) > (t);_lasttime += (t))
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("ON");
display.clearDisplay();
display.display();
Serial.println(F("Exp 0.1"));
}
void loop() {
if (Serial.available())
{
/*display.setCursor(0, 20);
display.println("Waiting for String...");
display.display();
delay(2500); //allows all serial sent to be received together*/
motion_command = Serial.read();
if (motion_command == 'G')
{
fsm_state = FORWARD_S;
}
else if (motion_command == 'S')
{
fsm_state = STOP_S;
}
else if (motion_command == 'L')
{
fsm_state = TURN_LEFT_S;
}
else if (motion_command == 'R')
{
fsm_state = TURN_RIGHT_S;
}
else
{
motion_command = 'E';
fsm_state = STOP_S;
}
Serial.setCursor(0, 40);
Serial.clearDisplay();
Serial..print("Command recieved: ");
display.println(motion_command);
display.display();
delay(3000);
}
// runEvery(50) {
//state machine
switch (fsm_state)
{
case STOP_S: //Statements to execute
//move_cnt = 100;
mc.move(0,0);
//fsm_state = WAIT_STOP_S;
//Serial.println(F("Stop"));
break;
/*case WAIT_STOP_S:
// move_cnt--;
//if(move_cnt == 0) {
// fsm_state = FORWARD_S;
// }
break;*/
case FORWARD_S: //Statements to execute
//move_cnt = 100;
mc.move(255,255);
//fsm_state = MOVE_FORWARD_S;
//Serial.println(F("Go"));
break;
/*case MOVE_FORWARD_S: //Statements to execute
move_cnt--;
// if == zero - then go forward
if(move_cnt == 0) {
fsm_state = STOP_S;
}
break;*/
case LEFT_S:
//move_cnt = 100;
mc.move(255,0);
//fsm_state = TURN_LEFT_S;
//Serial.println(F("LEFT"));
break;
/*case TURN_LEFT_S:
// decrement turn counter
move_cnt--;
// if == zero - then go forward
if(move_cnt == 0) {
fsm_state = STOP_S;
}
break;*/
case RIGHT_S:
//move_cnt = 100;
mc.move(0,255);
//fsm_state = TURN_RIGHT_S;
//Serial.println(F("RIGHT"));
break;
/*case TURN_RIGHT_S:
// decrement turn counter
move_cnt--;
// if == zero - then go forward
if(move_cnt == 0) {
fsm_state = FORWARD_S;
}
break;*/
}
// }
/*display.clearDisplay();
display.setCursor(0, 0);
display.println("Waiting for Serial...");
display.display();
delay(1000);*/
}