import processing.core.*; import static processing.core.PApplet.*; import cc.arduino.*; class LEDControl { final int comRate = 57600; Arduino arduino; boolean[] state = {false, false, false, false}; LEDControl(PApplet applet, int whichArduino) { // Arduino - initialize correct arduino String[] arduinos = Arduino.list(); if (arduinos.length == 0) { println("There are no arduinos available for use."); applet.exit(); } else { println("Available arduinos:"); for( String ard : arduinos) { println(ard); } print("Using arduino "); println(arduinos[whichArduino]); // this.arduino = new Arduino(applet, arduinos[whichArduino], comRate); // // for (Direction dir : Direction.values()) { // this.arduino.pinMode(dir.pin, Arduino.OUTPUT); // } } } boolean isActive(Direction dir) { return this.state[dir.index]; } void turnOff(Direction dir) { this.arduino.digitalWrite(dir.pin, Arduino.LOW); this.state[dir.index] = false; } void turnOn(Direction dir) { this.arduino.digitalWrite(dir.pin, Arduino.HIGH); this.state[dir.index] = true; } void toggle(Direction dir) { if (isActive(dir)) { turnOff(dir); } else { turnOn(dir); } } void allOn() { for (Direction dir : Direction.values()) { turnOn(dir); } } void allOff() { for (Direction dir : Direction.values()) { turnOff(dir); } } void keyPressed(char key) { switch (key) { case 'd': turnOn(Direction.RIGHT); break; case 'w': turnOn(Direction.UP); break; case 's': turnOn(Direction.DOWN); break; case 'a': turnOn(Direction.LEFT); break; } } void keyReleased(char key) { switch (key) { case 'd': turnOff(Direction.RIGHT); break; case 'w': turnOff(Direction.UP); break; case 's': turnOff(Direction.DOWN); break; case 'a': turnOff(Direction.LEFT); break; } } enum Direction { LEFT (0, 10), RIGHT (1, 5), UP (2, 6), DOWN (3, 9); final int index, pin; Direction(int index, int pin) { this.index = index; this.pin = pin; } } }