This repository has been archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainB.ino
150 lines (130 loc) · 2.46 KB
/
trainB.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
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 9); // RX, TX
//Define Pins
int enableA = 12; int pinA1 = 11; int pinA2 = 10;
boolean run;
void setup()
{
pinMode(enableA, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);
run = true;
Serial.begin(9600);
Serial.println("Hello I am the nano of Train B");
mySerial.begin(9600);
}
void loop() // run over and over
{
if (mySerial.available()) {
int receivedByNano = mySerial.read();
//Serial.write(receivedByNano);
// Serial.println("recv method");
//Serial.println("Greetings from nano, i received the following");
Serial.println(receivedByNano);
//Case 3 (51)
if(receivedByNano == 51) {
enableMotors();
forward(10);
}
else if(receivedByNano == 52) {
disableMotors();
}
// if(run) {
//
// enableMotors();
// forward(10000);
// }
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
}
void motorAOn() {
digitalWrite(enableA, HIGH);
}
//void motorBOn() {
// digitalWrite(enableB, HIGH);
//}
//disable motors
void motorAOff()
{
digitalWrite(enableA, LOW);
}
//void motorBOff() {
// digitalWrite(enableA, LOW);
//}
//motor A controls
void motorAForward() {
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}
void motorABackward() {
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
}
//motor B contorls
//void motorBForward() {
// digitalWrite(pinB1, HIGH);
// digitalWrite(pinB2, LOW);
//}
//void motorBBackward() {
// digitalWrite(pinB1, LOW);
// digitalWrite(pinB2, HIGH);
//}
//coasting and braking
void motorACoast() {
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}
void motorABrake() {
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}
//void motorBCoast() {
// digitalWrite(pinB1, LOW);
// digitalWrite(pinB2, LOW);
//}
//void motorBBrake() {
// digitalWrite(pinB1, HIGH);
// digitalWrite(pinB2, HIGH);
//}
//Define High Level Commands
void enableMotors() {
motorAOn();
// motorBOn();
}
void disableMotors()
{
motorAOff();
// motorBOff();
}
void forward(int time) {
motorAForward();
//motorBForward();
delay(time);
}
void backward(int time) {
motorABackward();
//motorBBackward();
delay(time);
}
void turnLeft(int time) {
motorABackward();
// motorBForward();
delay(time);
}
void turnRight(int time) {
motorAForward();
//motorBBackward();
delay(time);
}
void coast(int time) {
motorACoast();
//motorBCoast();
delay(time);
}
void brake(int time) {
motorABrake();
//motorBBrake();
delay(time);
}