import java.awt.event.KeyEvent; import javax.swing.JOptionPane; import processing.serial.*; Serial port = null; String portname = null; boolean streaming = false; float speed = 1; int i=0; void openSerialPort(){ if(portname == null) return; if(port != null) port.stop(); port = new Serial(this, portname, 9600); port.bufferUntil('\n'); } void selectSerialPort(){ String result = (String) JOptionPane.showInputDialog(frame, "Select the serial port to which your Arduino board is connected to", "Select serial port", JOptionPane.QUESTION_MESSAGE, null, Serial.list(), 0); if (result!= null) { portname = result; openSerialPort(); } } void setup(){ size(400, 300); openSerialPort(); } void draw(){ background(3); fill(200); int y = 24, dy = 16; text("USAGE INSTRUCTIONS", 100, y); y +=dy; text("----------------------------------", 100, y); y +=dy; text("P: select the serial port", 12, y); y += dy; text("Up arrow key: Move locomotive forward", 12, y); y += dy; text("Down arrow key: Move locomotive backward", 12, y); y += dy; text("Enter key: Stop the locomotive", 12, y); y += dy; text("Shift key: Power down the locomotive", 12, y); y += dy; text("Enter: Stop the locomotive", 12, y); y += dy; text("Q: Switch turnout1 to straight", 12, y); y += dy; text("W: Switch turnout1 to right", 12, y); y += dy; text("E: Switch turnout2 to left", 12, y); y += dy; text("R: Switch turnout2 to straight", 12, y); y += dy; y = height - dy; text("current serial port: " + portname, 12, y); y -= dy; } void keyPressed(){ if(keyCode == UP) port.write("1"); if(keyCode == DOWN) port.write("2"); if(key == 'w') port.write("3"); if(key == 'q') port.write("4"); if(key == 'e') port.write("5"); if(key == 'r') port.write("6"); if(keyCode == SHIFT) port.write("7"); if(key == ENTER) port.write("8"); if(key == 'p') selectSerialPort(); delay(10); }