/* USB Arcade Controller Teensy becomes a USB arcade controller with 12 buttons (joystick up, right, left, down are considered buttons) You must select Joystick from the "Tools > USB Type" menu. Pushbuttons should be connected between the digital pins and ground. LED strip is triggered by 2 buttons for each colour (RED, GREEN, BLUE). */ // Configure the number of buttons to read as digital inputs to the controller. const int teensyNumButtons = 12; // total number of pins used on the teensy. const int controllerNumButtons = 12; // total button count for controller configuration. const int startNum = 0; // teensy's we used have different starting pins (either 0 or 1) depending on wiring mistake. // LED stip pins int LED_R = 15; int LED_G = 16; int LED_B = 14; void setup() { // set all arcade microswiches as inputs to the controller. Pin numbers depend on wiring: // X = 1 // Y = 2 // R = 3 // A = 4 // B = 5 // L = 6 // START = 7 // COIN = 8 // Joy DOWN = 9 // Joy LEFT = 10 // Joy UP = 11 // Joy RIGHT = 12 for (int i=startNum; i<=teensyNumButtons; i++) { pinMode(i, INPUT); } // RGB LED strip pinMode(LED_R, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_B, OUTPUT); } byte allButtons[teensyNumButtons]; byte prevButtons[teensyNumButtons]; void loop() { // read digital pins of the teensy and use them for the buttons for (int i=startNum; i<=teensyNumButtons; i++) { allButtons[i] = digitalRead(i); Joystick.button(i, allButtons[i]); // send the button action } // turning on different colours of the led strip with a button detection if(allButtons[1] == HIGH || allButtons[4] == HIGH) { digitalWrite(LED_R, HIGH); } else { digitalWrite(LED_R, LOW); } if(allButtons[2] == HIGH || allButtons[5] == HIGH) { digitalWrite(LED_G, HIGH); } else { digitalWrite(LED_G, LOW); } if(allButtons[3] == HIGH || allButtons[6] == HIGH) { digitalWrite(LED_B, HIGH); } else { digitalWrite(LED_B, LOW); } // check to see if any button changed since last time for (int i=startNum; i<=teensyNumButtons; i++) { if (allButtons[i] != prevButtons[i]); prevButtons[i] = allButtons[i]; } delay(5); // a brief delay, so this runs "only" 200 times per second }