This repository has been archived by the owner on May 21, 2019. It is now read-only.
forked from awhittle3/EV3AndroidRC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brick.java
120 lines (100 loc) · 3.03 KB
/
Brick.java
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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import lejos.hardware.Bluetooth;
import lejos.hardware.motor.NXTMotor;
import lejos.hardware.port.MotorPort;
import lejos.remote.nxt.BTConnection;
import lejos.remote.nxt.BTConnector;
import lejos.remote.nxt.NXTConnection;
import lejos.robotics.EncoderMotor;
/*
* Modified code from Tawat Atigarbodee, September 21, 2009
* http://lejos.sourceforge.net/forum/viewtopic.php?t=1723
*/
public class Brick {
private static DataOutputStream dataOut;
private static DataInputStream dataIn;
private static BTConnection BTLink;
private static int transmitReceived=0;
private static int power = 50;
private static boolean app_alive;
private static EncoderMotor motorLeft;
private static EncoderMotor motorRight;
public static void main(String[] args){
connect();
app_alive = true;
motorLeft = new NXTMotor (MotorPort.A);
motorRight = new NXTMotor (MotorPort.B);
motorLeft.setPower(power);
motorRight.setPower(power);
motorLeft.stop();
motorRight.stop();
while(app_alive){
try {
transmitReceived = (int) dataIn.readByte();
System.out.println("Received " + transmitReceived);
switch(transmitReceived){
// Forwards
case 1:
motorLeft.forward();
motorRight.forward();
break;
// Backwards
case 2:
motorLeft.backward();
motorRight.backward();
break;
// Turn Left
case 3:
motorLeft.backward();
motorRight.forward();
break;
// Turn Right
case 4:
motorLeft.forward();
motorRight.backward();
break;
// Speed up
case 5:
if(power < 100){
power += 10;
}
motorLeft.setPower(power);
motorRight.setPower(power);
break;
// Slow down
case 6:
if(power > 10){
power -= 10;
}
motorLeft.setPower(power);
motorRight.setPower(power);
break;
// Terminate command
case 7:
app_alive = false;
break;
// Directional button released
case 10:
motorLeft.stop();
motorRight.stop();
break;
}
}
catch (IOException ioe) {
System.out.println("IO Exception readInt");
}
}
motorLeft.flt();
motorRight.flt();
}
public static void connect()
{
System.out.println("Listening");
BTConnector ncc = (BTConnector) Bluetooth.getNXTCommConnector();
BTLink = (BTConnection) ncc.waitForConnection(30000, NXTConnection.RAW);
dataOut = BTLink.openDataOutputStream();
dataIn = BTLink.openDataInputStream();
}
}