I am trying to do a program, and one of the parts is driving me crazy, so I will appreciate if someone can help me. The problem is that I’m doing a window with bottoms at the computer, and when you press the bottom, it will start to add to a variable the value 1, and display it on the LCD. Until here I don’t have any problem, but I add a second bottom, and when you press that second bottom, the other variable needs to stop adding 1, and start to add it to the other one, and display it on the LCD . But when I’m doing this, it’s like the loop doesn’t work, it’s taking the value just once.
I think that the main problem is on the NXT part, on this bucle:
- Code: Select all
int b=0;
int u=0;
int cont1=0;
while(!Button.ESCAPE.isPressed())
{
try{
cont1=dis.readInt();
if (cont1==2){
LCD.drawString("N2 " + b, 0, 5);
LCD.refresh();
b=b+1;
}
if(cont1==4){
LCD.drawString("N4 " + u, 0, 6);
LCD.refresh();
u=u+1;
}
}
catch (IOException ioe){
System.out.println("IO Exception readInt");
}
}
Like I said before I will appreciate any help, and sorry about my English
This is for the PC
- Code: Select all
import lejos.nxt.ColorSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.remote.NXTCommand;
import lejos.pc.comm.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Prueba_PC {
public static void main(String[] args){
JFrame frame=new JFrame();
final JButton Connect_Button = new JButton("Connect");
final JLabel Connect_Label = new JLabel("No Connected");
final JButton Move_Button = new JButton("Move");
final JButton Stop_Button = new JButton("Stop");
Move_Button.setEnabled(false);
Stop_Button.setEnabled(false);
Connect_Label.setForeground(Color.red);
JPanel panel=new JPanel();
panel.add(Connect_Button);
panel.add(Connect_Label);
panel.add(Move_Button);
panel.add(Stop_Button);
frame.add(panel);
class ConnectToRobot implements ActionListener{
public void actionPerformed (ActionEvent event){
String Action=Connect_Button.getText();
if (Action=="Connect")
{
System.out.println("Connecting...");
conn.addLogListener(new NXTCommLogListener(){
public void logEvent(String message) {
System.out.println("Connect to NXT-Alex");
}
public void logEvent(Throwable throwable) {
System.out.println("BTSend Log.listener - stack trace: ");
}
});
boolean connected = conn.connectTo("btspp://");
if (!connected) {
System.err.println("Failed to connect to any NXT");
System.exit(1);
}
NXTCommand.getSingleton().setNXTComm(conn.getNXTComm());
Connect_Label.setForeground(Color.green);
Connect_Label.setText ("Connected");
Connect_Button.setText("Disconnect");
Move_Button.setEnabled(true);
Stop_Button.setEnabled(true);
control=1;
DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();
try {
System.out.println("Sending " + (2));
dos.writeInt(2);
dos.flush();
} catch (IOException ioe) {
System.out.println("IO Exception writing bytes:");
System.out.println(ioe.getMessage());
}
}
else
{
try {
conn.close();
}catch (IOException ioe) {
System.out.println("IOException closing connection:");
System.out.println(ioe.getMessage());
}
Connect_Label.setForeground(Color.red);
Connect_Label.setText ("No Connected");
Connect_Button.setText("Connect");
System.out.println("Disconnected from NXT-Alex");
Move_Button.setEnabled(false);
Stop_Button.setEnabled(false);
}
}
}
class MoveRobot implements ActionListener{
public void actionPerformed (ActionEvent event){
System.out.println("Starting Pathfinder...");
DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();
try {
System.out.println("Sending " + (4));
dos.writeInt(4);
dos.flush();
} catch (IOException ioe) {
System.out.println("IO Exception writing bytes:");
System.out.println(ioe.getMessage());
}
}
}
class StopRobot implements ActionListener{
public void actionPerformed (ActionEvent event){
System.out.println("Stopping...");
DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();
try {
System.out.println("Sending " + (2));
dos.writeInt(2);
dos.flush();
} catch (IOException ioe) {
System.out.println("IO Exception writing bytes:");
System.out.println(ioe.getMessage());
}
}
}
ActionListener Connect_listener = new ConnectToRobot();
Connect_Button.addActionListener(Connect_listener);
ActionListener Move_listener = new MoveRobot();
Move_Button.addActionListener(Move_listener);
ActionListener Stop_listener = new StopRobot();
Stop_Button.addActionListener(Stop_listener);
frame.setSize(Frame_width, Frame_Height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static int control=0;
public static final int Frame_width=400;
public static final int Frame_Height=300;
public static NXTConnector conn = new NXTConnector();
public static DataOutputStream Out_Data = conn.getDataOut();
public static DataInputStream In_Data = conn.getDataIn();
This is for the NXT
- Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
public class Prueba_Robot {
static ColorSensor LSensor = new ColorSensor (SensorPort.S1);
static ColorSensor RSensor = new ColorSensor (SensorPort.S2);
static void Pathfinder(int cont)
{
int b=0;
int u=0;
int cont1=0;
while(!Button.ESCAPE.isPressed())
{
try{
cont1=dis.readInt();
if (cont1==2){
LCD.drawString("N2 " + b, 0, 5);
LCD.refresh();
b=b+1;
}
if(cont1==4){
LCD.drawString("N4 " + u, 0, 6);
LCD.refresh();
u=u+1;
}
}
catch (IOException ioe){
System.out.println("IO Exception readInt");
}
}
}
public static void main(String [] args) throws Exception
{
String connected = "Connected";
String waiting = "Waiting...";
while (true)
{
LCD.drawString(waiting,0,0);
LCD.refresh();
btc = Bluetooth.waitForConnection();
LCD.clear();
LCD.drawString(connected,0,0);
LCD.refresh();
dis = btc.openDataInputStream();
a=dis.readInt();
while(true){
Pathfinder(a);
}
}
}
public static BTConnection btc;
public static DataInputStream dis;
public static int a;
public static int contador=0;
public static boolean control;
}
