├── Serial └── EchoBoth │ └── EchoBoth.ino ├── Tutorial1 └── Blink │ └── Blink.ino ├── Tutorial2 ├── Blink │ └── Blink.ino ├── PWM │ └── PWM.ino └── UsingVariable │ └── UsingVariable.ino ├── Tutorial3 ├── HelloSerialMonitor │ └── HelloSerialMonitor.ino ├── Pushbutton │ └── Pushbutton.ino ├── PushbuttonPullup │ └── PushbuttonPullup.ino └── PushbuttonRGBcolor │ └── PushbuttonRGBcolor.ino ├── Tutorial4 ├── AnalogInput │ └── AnalogInput.ino ├── AnalogRGBcolor │ └── AnalogRGBcolor.ino ├── TemperatureNumberOnly │ └── TemperatureNumberOnly.ino ├── TemperatureScaled │ └── TemperatureScaled.ino └── TemperatureScaledMulti │ └── TemperatureScaledMulti.ino ├── USB_FlightSim ├── BlinkTransponder │ └── BlinkTransponder.ino ├── FrameRateDisplay │ └── FrameRateDisplay.ino ├── NavFrequency │ └── NavFrequency.ino └── ThrottleServo │ └── ThrottleServo.ino ├── USB_Joystick ├── Basic │ └── Basic.ino ├── Buttons │ └── Buttons.ino └── Complete │ └── Complete.ino ├── USB_Keyboard ├── Buttons │ └── Buttons.ino ├── LayoutTest │ └── LayoutTest.ino ├── MediaButtons │ └── MediaButtons.ino └── Simple │ └── Simple.ino ├── USB_MIDI ├── AnalogControlChange │ └── AnalogControlChange.ino ├── Buttons │ └── Buttons.ino ├── DMX_Lighting │ ├── DMX_Lighting.ino │ ├── leddmx.cpp │ └── leddmx.h ├── InputFunctionsBasic │ └── InputFunctionsBasic.ino ├── InputFunctionsComplete │ └── InputFunctionsComplete.ino ├── InputRead │ └── InputRead.ino ├── Interface_3x3 │ └── Interface_3x3.ino ├── MIDI_name │ ├── MIDI_name.ino │ └── name.c ├── Many_Button_Knobs │ └── Many_Button_Knobs.ino ├── Piezo_Drum │ └── Piezo_Drum.ino └── TransmitEverything │ └── TransmitEverything.ino ├── USB_Mouse ├── Buttons │ └── Buttons.ino └── TriangleMove │ └── TriangleMove.ino ├── USB_RawHID └── Basic │ └── Basic.ino ├── USB_Serial ├── HelloWorld │ └── HelloWorld.ino ├── Triple_Serial_Test │ └── Triple_Serial_Test.ino └── USBtoSerial │ └── USBtoSerial.ino └── USB_Touchscreen ├── SingleFingerLine └── SingleFingerLine.ino └── TenFingerCircle └── TenFingerCircle.ino /Serial/EchoBoth/EchoBoth.ino: -------------------------------------------------------------------------------- 1 | /* UART Example, any character received on either the real 2 | serial port, or USB serial (or emulated serial to the 3 | Arduino Serial Monitor when using non-serial USB types) 4 | is printed as a message to both ports. 5 | 6 | This example code is in the public domain. 7 | */ 8 | 9 | // set this to the hardware serial port you wish to use 10 | #define HWSERIAL Serial1 11 | 12 | void setup() { 13 | Serial.begin(9600); 14 | HWSERIAL.begin(38400); 15 | } 16 | 17 | void loop() { 18 | int incomingByte; 19 | 20 | if (Serial.available() > 0) { 21 | incomingByte = Serial.read(); 22 | Serial.print("USB received: "); 23 | Serial.println(incomingByte, DEC); 24 | HWSERIAL.print("USB received:"); 25 | HWSERIAL.println(incomingByte, DEC); 26 | } 27 | if (HWSERIAL.available() > 0) { 28 | incomingByte = HWSERIAL.read(); 29 | Serial.print("UART received: "); 30 | Serial.println(incomingByte, DEC); 31 | HWSERIAL.print("UART received:"); 32 | HWSERIAL.println(incomingByte, DEC); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Tutorial1/Blink/Blink.ino: -------------------------------------------------------------------------------- 1 | /* LED Blink, Teensyduino Tutorial #1 2 | http://www.pjrc.com/teensy/tutorial.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | // Teensy 2.0 has the LED on pin 11 8 | // Teensy++ 2.0 has the LED on pin 6 9 | // Teensy 3.x / Teensy LC have the LED on pin 13 10 | const int ledPin = 13; 11 | 12 | // the setup() method runs once, when the sketch starts 13 | 14 | void setup() { 15 | // initialize the digital pin as an output. 16 | pinMode(ledPin, OUTPUT); 17 | } 18 | 19 | // the loop() methor runs over and over again, 20 | // as long as the board has power 21 | 22 | void loop() { 23 | digitalWrite(ledPin, HIGH); // set the LED on 24 | delay(1000); // wait for a second 25 | digitalWrite(ledPin, LOW); // set the LED off 26 | delay(1000); // wait for a second 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Tutorial2/Blink/Blink.ino: -------------------------------------------------------------------------------- 1 | /* RGB LED Blink, Teensyduino Tutorial #2 2 | http://www.pjrc.com/teensy/tutorial2.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | const int redPin = 12; 8 | const int greenPin = 15; 9 | const int bluePin = 14; 10 | 11 | // The setup() method runs once, when the sketch starts 12 | 13 | void setup() { 14 | // initialize the digitals pin as an outputs 15 | pinMode(redPin, OUTPUT); 16 | pinMode(greenPin, OUTPUT); 17 | pinMode(bluePin, OUTPUT); 18 | } 19 | 20 | // the loop() method runs over and over again, 21 | 22 | void loop() 23 | { 24 | digitalWrite(redPin, HIGH); 25 | delay(500); 26 | digitalWrite(greenPin, HIGH); 27 | delay(500); 28 | digitalWrite(bluePin, HIGH); 29 | delay(500); 30 | digitalWrite(redPin, LOW); 31 | delay(500); 32 | digitalWrite(greenPin, LOW); 33 | delay(500); 34 | digitalWrite(bluePin, LOW); 35 | delay(500); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Tutorial2/PWM/PWM.ino: -------------------------------------------------------------------------------- 1 | /* RGB Analog Example, Teensyduino Tutorial #2 2 | http://www.pjrc.com/teensy/tutorial2.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | const int redPin = 12; 8 | const int greenPin = 15; 9 | const int bluePin = 14; 10 | 11 | void setup() { 12 | pinMode(redPin, OUTPUT); 13 | pinMode(greenPin, OUTPUT); 14 | pinMode(bluePin, OUTPUT); 15 | } 16 | 17 | void loop() 18 | { 19 | analogWrite(redPin, 30); 20 | delay(500); 21 | analogWrite(greenPin, 200); 22 | delay(500); 23 | analogWrite(bluePin, 40); 24 | delay(500); 25 | analogWrite(redPin, 150); 26 | delay(500); 27 | analogWrite(greenPin, 0); 28 | delay(500); 29 | analogWrite(bluePin, 250); 30 | delay(500); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Tutorial2/UsingVariable/UsingVariable.ino: -------------------------------------------------------------------------------- 1 | /* RGB LED Fading, Teensyduino Tutorial #2 2 | Demonstrates using a variable 3 | http://www.pjrc.com/teensy/tutorial2.html 4 | 5 | This example code is in the public domain. 6 | */ 7 | 8 | const int redPin = 12; 9 | const int greenPin = 15; 10 | const int bluePin = 14; 11 | 12 | void setup() { 13 | pinMode(redPin, OUTPUT); 14 | pinMode(greenPin, OUTPUT); 15 | pinMode(bluePin, OUTPUT); 16 | } 17 | 18 | int redIntensity = 0; 19 | 20 | void loop() 21 | { 22 | // set all 3 pins to the desired intensity 23 | analogWrite(redPin, redIntensity); 24 | analogWrite(greenPin, 255 - redIntensity); 25 | analogWrite(bluePin, 0); 26 | 27 | // remain at this color, but not for very long 28 | delay(10); 29 | 30 | // increase the red 31 | redIntensity = redIntensity + 1; 32 | 33 | // since 255 is the maximum, set it back to 0 34 | // when it increments beyond 255 35 | if (redIntensity > 255) { 36 | redIntensity = 0; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Tutorial3/HelloSerialMonitor/HelloSerialMonitor.ino: -------------------------------------------------------------------------------- 1 | /* Serial Monitor Example, Teensyduino Tutorial #3 2 | http://www.pjrc.com/teensy/tutorial3.html 3 | 4 | After uploading this to your board, use Serial Monitor 5 | to view the message. When Serial is selected from the 6 | Tools > USB Type menu, the correct serial port must be 7 | selected from the Tools > Serial Port AFTER Teensy is 8 | running this code. Teensy only becomes a serial device 9 | while this code is running! For non-Serial types, 10 | the Serial port is emulated, so no port needs to be 11 | selected. 12 | 13 | This example code is in the public domain. 14 | */ 15 | 16 | 17 | void setup() { 18 | Serial.begin(38400); 19 | } 20 | 21 | void loop() 22 | { 23 | Serial.println("Hello World"); 24 | delay(1000); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Tutorial3/Pushbutton/Pushbutton.ino: -------------------------------------------------------------------------------- 1 | /* Pushbutton, Teensyduino Tutorial #3 2 | http://www.pjrc.com/teensy/tutorial3.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | void setup() { 8 | Serial.begin(38400); 9 | pinMode(7, INPUT); 10 | } 11 | 12 | void loop() 13 | { 14 | if (digitalRead(7) == HIGH) { 15 | Serial.println("Button is not pressed..."); 16 | } else { 17 | Serial.println("Button pressed!!!"); 18 | } 19 | delay(250); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Tutorial3/PushbuttonPullup/PushbuttonPullup.ino: -------------------------------------------------------------------------------- 1 | /* Pushbutton with Pullup, Teensyduino Tutorial #3 2 | http://www.pjrc.com/teensy/tutorial3.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | void setup() { 8 | Serial.begin(38400); 9 | pinMode(8, INPUT); 10 | } 11 | 12 | void loop() 13 | { 14 | if (digitalRead(8) == HIGH) { 15 | Serial.println("Button is not pressed..."); 16 | } else { 17 | Serial.println("Button pressed!!!"); 18 | } 19 | delay(250); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Tutorial3/PushbuttonRGBcolor/PushbuttonRGBcolor.ino: -------------------------------------------------------------------------------- 1 | /* Pushbuttons control RGB LED Color, Teensyduino Tutorial #3 2 | http://www.pjrc.com/teensy/tutorial3.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | const int redPin = 12; 8 | const int greenPin = 15; 9 | const int bluePin = 14; 10 | 11 | void setup() { 12 | Serial.begin(38400); 13 | pinMode(7, INPUT); 14 | pinMode(8, INPUT_PULLUP); 15 | pinMode(redPin, OUTPUT); 16 | pinMode(greenPin, OUTPUT); 17 | pinMode(bluePin, OUTPUT); 18 | } 19 | 20 | int redIntensity = 0; 21 | int mode = 0; 22 | 23 | void loop() 24 | { 25 | // set all 3 pins to the desired intensity 26 | analogWrite(redPin, redIntensity); 27 | if (mode == 0) { 28 | // in mode zero, fade from red to green 29 | analogWrite(greenPin, 255 - redIntensity); 30 | analogWrite(bluePin, 0); 31 | } else { 32 | // in mode one, fade from red to blue 33 | analogWrite(greenPin, 0); 34 | analogWrite(bluePin, 255 - redIntensity); 35 | } 36 | 37 | if (digitalRead(7) == LOW) { 38 | // use mode zero when the first button is pressed 39 | mode = 0; 40 | Serial.println("mode 0"); 41 | } 42 | if (digitalRead(8) == LOW) { 43 | // use mode one when the first button is pressed 44 | mode = 1; 45 | Serial.println("mode 1"); 46 | } 47 | 48 | // remain at this color, but not for very long 49 | delay(10); 50 | 51 | // increase the red 52 | redIntensity = redIntensity + 1; 53 | 54 | // since 255 is the maximum, set it back to 0 55 | // when it increments beyond 255 56 | if (redIntensity > 255) { 57 | redIntensity = 0; 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Tutorial4/AnalogInput/AnalogInput.ino: -------------------------------------------------------------------------------- 1 | /* Analog Input Example, Teensyduino Tutorial #4 2 | http://www.pjrc.com/teensy/tutorial4.html 3 | 4 | After uploading this to your board, use Serial Monitor 5 | to view the message. When Serial is selected from the 6 | Tools > USB Type menu, the correct serial port must be 7 | selected from the Tools > Serial Port AFTER Teensy is 8 | running this code. Teensy only becomes a serial device 9 | while this code is running! For non-Serial types, 10 | the Serial port is emulated, so no port needs to be 11 | selected. 12 | 13 | This example code is in the public domain. 14 | */ 15 | 16 | void setup() 17 | { 18 | Serial.begin(38400); 19 | } 20 | 21 | int val; 22 | 23 | void loop() 24 | { 25 | val = analogRead(0); 26 | Serial.print("analog 0 is: "); 27 | Serial.println(val); 28 | delay(250); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Tutorial4/AnalogRGBcolor/AnalogRGBcolor.ino: -------------------------------------------------------------------------------- 1 | /* Analog Control RGB LED Color, Teensyduino Tutorial #4 2 | http://www.pjrc.com/teensy/tutorial4.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | const int redPin = 12; 8 | const int greenPin = 15; 9 | const int bluePin = 14; 10 | 11 | void setup() { 12 | pinMode(redPin, OUTPUT); 13 | pinMode(greenPin, OUTPUT); 14 | pinMode(bluePin, OUTPUT); 15 | } 16 | 17 | int redIntensity = 0; 18 | 19 | void loop() 20 | { 21 | // read the pot position 22 | redIntensity = analogRead(0) / 4; 23 | 24 | // set all 3 pins to the desired intensity 25 | analogWrite(redPin, redIntensity); 26 | analogWrite(greenPin, 255 - redIntensity); 27 | analogWrite(bluePin, 0); 28 | 29 | // remain at this color, but not for very long 30 | delay(10); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Tutorial4/TemperatureNumberOnly/TemperatureNumberOnly.ino: -------------------------------------------------------------------------------- 1 | /* Temperature Sensor, Raw Numbers, Teensyduino Tutorial #4 2 | http://www.pjrc.com/teensy/tutorial4.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | void setup() 8 | { 9 | Serial.begin(38400); 10 | } 11 | 12 | int val; 13 | 14 | void loop() 15 | { 16 | val = analogRead(1); 17 | Serial.print("analog 1 is: "); 18 | Serial.println(val); 19 | delay(1000); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Tutorial4/TemperatureScaled/TemperatureScaled.ino: -------------------------------------------------------------------------------- 1 | /* Temperature Sensor, Simple Scaling, Teensyduino Tutorial #4 2 | http://www.pjrc.com/teensy/tutorial4.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | void setup() 8 | { 9 | Serial.begin(38400); 10 | } 11 | 12 | float code; 13 | float celsius; 14 | float fahrenheit; 15 | 16 | void loop() 17 | { 18 | code = analogRead(1); 19 | celsius = 25 + (code - 512) / 11.3; 20 | fahrenheit = celsius * 1.8 + 32; 21 | Serial.print("temperature: "); 22 | Serial.print(celsius); 23 | Serial.print(" Celsius, "); 24 | Serial.print(fahrenheit); 25 | Serial.println(" Fahrenheit"); 26 | delay(1000); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Tutorial4/TemperatureScaledMulti/TemperatureScaledMulti.ino: -------------------------------------------------------------------------------- 1 | /* Temperature Sensor, Multiple Scales, Teensyduino Tutorial #4 2 | http://www.pjrc.com/teensy/tutorial4.html 3 | 4 | This example code is in the public domain. 5 | */ 6 | 7 | void setup() 8 | { 9 | Serial.begin(38400); 10 | } 11 | 12 | int code; 13 | float celsius; 14 | float fahrenheit; 15 | 16 | void loop() 17 | { 18 | code = analogRead(1); 19 | if (code <= 289) { 20 | celsius = 5 + (code - 289) / 9.82; 21 | } 22 | if (code > 289 && code <= 342) { 23 | celsius = 10 + (code - 342) / 10.60; 24 | } 25 | if (code > 342 && code <= 398) { 26 | celsius = 15 + (code - 398) / 11.12; 27 | } 28 | if (code > 398 && code <= 455) { 29 | celsius = 20 + (code - 455) / 11.36; 30 | } 31 | if (code > 455 && code <= 512) { 32 | celsius = 25 + (code - 512) / 11.32; 33 | } 34 | if (code > 512 && code <= 566) { 35 | celsius = 30 + (code - 566) / 11.00; 36 | } 37 | if (code > 566 && code <= 619) { 38 | celsius = 35 + (code - 619) / 10.44; 39 | } 40 | if (code > 619 && code <= 667) { 41 | celsius = 40 + (code - 667) / 9.73; 42 | } 43 | if (code > 667) { 44 | celsius = 45 + (code - 712) / 8.90; 45 | } 46 | fahrenheit = celsius * 1.8 + 32; 47 | Serial.print("temperature: "); 48 | Serial.print(celsius); 49 | Serial.print(" Celsius, "); 50 | Serial.print(fahrenheit); 51 | Serial.println(" Fahrenheit"); 52 | delay(1000); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /USB_FlightSim/BlinkTransponder/BlinkTransponder.ino: -------------------------------------------------------------------------------- 1 | // Special variable to access the transponder light 2 | FlightSimInteger transponderLight; 3 | 4 | // setup runs once 5 | void setup() { 6 | transponderLight = XPlaneRef("sim/cockpit/radios/transponder_light"); 7 | transponderLight.onChange(updateLED); 8 | pinMode(LED_BUILTIN, OUTPUT); // pin 11 on Teensy 2.0, pin 6 on Teensy++ 2.0 9 | } 10 | 11 | // loop runs repetitively, as long as power is on 12 | void loop() { 13 | FlightSim.update(); // causes X-Plane's changes to be received 14 | } 15 | 16 | // updateLED runs only when X-Plane changes transponderLight 17 | void updateLED(long value) { 18 | if (value == 0) { 19 | digitalWrite(LED_BUILTIN, LOW); 20 | } else { 21 | digitalWrite(LED_BUILTIN, HIGH); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /USB_FlightSim/FrameRateDisplay/FrameRateDisplay.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | DogLcd lcd = DogLcd(10, 9, 7, 8); // DogM LCD on pins 7, 8, 9, 10 4 | 5 | // variables 6 | FlightSimElapsedFrames frameCount; // increases each simulation frame 7 | elapsedMillis milliSeconds; // increases 1000 per second 8 | 9 | 10 | // setup runs once, when Teensy boots. 11 | // 12 | void setup() { 13 | lcd.begin(DOG_LCD_M162); 14 | pinMode(LED_BUILTIN, OUTPUT); 15 | } 16 | 17 | // loop runs repetitively, as long as Teensy is powered up 18 | // 19 | void loop() { 20 | // receive any incoming X-Plane data (necessary for frame update) 21 | FlightSim.update(); 22 | 23 | // the LED shows if X-Plane is running and plugin is enabled 24 | if (FlightSim.isEnabled()) { 25 | digitalWrite(LED_BUILTIN, HIGH); 26 | } else { 27 | digitalWrite(LED_BUILTIN, LOW); 28 | } 29 | 30 | // every 10 frames, update the display with frame rate 31 | if (frameCount >= 10) { 32 | // read the elapsed frames and elapsed time 33 | int frames = frameCount; 34 | int ms = milliSeconds; 35 | // reset both to zero (immediately after reading) 36 | milliSeconds = 0; 37 | frameCount = 0; 38 | // compute and show the frames per second 39 | float fps = (float)frames / (float)ms * 1000.0; 40 | lcd.setCursor(0, 0); 41 | lcd.print(fps); 42 | lcd.print(" fsp "); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /USB_FlightSim/NavFrequency/NavFrequency.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // hardware objects, for accessing the buttons, rotary encoder and LCD 6 | // 7 | Bounce buttonUp = Bounce(3, 5); // Pushbutton on pin 3, 5ms debounce 8 | Bounce buttonDown = Bounce(4, 5); // Pushbutton on pin 4, 5ms debounce 9 | Encoder wheel = Encoder(5, 6); // Rotary Encoder on pin 5 and 6 10 | DogLcd lcd = DogLcd(10, 9, 7, 8); // DogM LCD on pins 7, 8, 9, 10 11 | 12 | // X-Plane objects, 3 command refs and 1 data ref 13 | FlightSimCommand NavCoarseUp; 14 | FlightSimCommand NavCoarseDown; 15 | FlightSimCommand NavFineUp; 16 | FlightSimInteger NavFrequencyHz; 17 | 18 | // variables 19 | long encoder_prev=0; // for detecting rotary position change 20 | elapsedMillis inactivityTimeout;// an inactivity timeout 21 | 22 | 23 | // setup runs once, when Teensy boots. 24 | // 25 | void setup() { 26 | // initialize all hardware 27 | pinMode(3, INPUT_PULLUP); // input pullup mode allows connecting 28 | pinMode(4, INPUT_PULLUP); // buttons and switches from the pins 29 | pinMode(5, INPUT_PULLUP); // to ground, and the chip provide the 30 | pinMode(6, INPUT_PULLUP); // required pullup resistor :-) 31 | lcd.begin(DOG_LCD_M162); 32 | lcd.print("nav1:"); 33 | 34 | // configure the X-Plane variables 35 | NavCoarseUp = XPlaneRef("sim/radios/actv_nav1_coarse_up"); 36 | NavCoarseDown = XPlaneRef("sim/radios/actv_nav1_coarse_down"); 37 | NavFineUp = XPlaneRef("sim/radios/actv_nav1_fine_up"); 38 | NavFrequencyHz = XPlaneRef("sim/cockpit2/radios/actuators/nav1_frequency_hz"); 39 | NavFrequencyHz.onChange(update_lcd); // update the LCD when X-Plane changes 40 | } 41 | 42 | // loop runs repetitively, as long as Teensy is powered up 43 | // 44 | void loop() { 45 | // normally the first step in loop() should update from X-Plane 46 | FlightSim.update(); 47 | 48 | // read the rotary encoder, if it's changed, write to NavFrequencyHz 49 | long enc = wheel.read(); 50 | if (enc != encoder_prev) { 51 | NavFrequencyHz = NavFrequencyHz + (enc - encoder_prev); 52 | encoder_prev = enc; 53 | update_lcd(NavFrequencyHz); 54 | inactivityTimeout = 0; // reset the inactivity timeout 55 | } 56 | 57 | // read the pushbuttons, and send X-Plane commands when they're pressed 58 | buttonUp.update(); 59 | buttonDown.update(); 60 | if (buttonUp.fallingEdge()) { 61 | NavCoarseUp = 1; 62 | inactivityTimeout = 0; 63 | } 64 | if (buttonUp.risingEdge()) { 65 | NavCoarseUp = 0; 66 | } 67 | if (buttonDown.fallingEdge()) { 68 | NavCoarseDown = 1; 69 | inactivityTimeout = 0; 70 | } 71 | if (buttonDown.risingEdge()) { 72 | NavCoarseDown = 0; 73 | } 74 | 75 | // if there's no user activity for 2 seconds, send the NavFineUp. 76 | // admittedly this is not very useful, but it's meant to demonstrate 77 | // possibility of automated actions in addition to driving everything 78 | // directly from physical user inputs. 79 | if (inactivityTimeout > 2000) { 80 | NavFineUp.once(); 81 | inactivityTimeout = 0; 82 | } 83 | } 84 | 85 | // write a number onto the LCD, first row, starting at 6th column 86 | void update_lcd(long val) 87 | { 88 | lcd.setCursor(6, 0); 89 | lcd.print(val); 90 | lcd.print(" "); 91 | } 92 | -------------------------------------------------------------------------------- /USB_FlightSim/ThrottleServo/ThrottleServo.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Servo motor; // an RC Servo motor 4 | FlightSimFloat throttle; // access to X-Plane's throttle 5 | const int motorPin = 2; 6 | const int potentiometerPin = A0; // Analog 0: Teensy = 23, T++ = 38 7 | int previousAnalog = -100; 8 | 9 | // setup runs once, when Teensy boots. 10 | // 11 | void setup() { 12 | motor.attach(motorPin); 13 | throttle = XPlaneRef("sim/flightmodel/engine/ENGN_thro[0]"); 14 | throttle.onChange(viewThrottle); 15 | Serial.begin(9600); 16 | Serial.println("Throttle Demo"); 17 | } 18 | 19 | // loop runs repetitively, as long as Teensy is powered up 20 | // 21 | void loop() { 22 | // normally the first step in loop() should update from X-Plane 23 | FlightSim.update(); 24 | 25 | // when human motion chances the port, change throttle 26 | int analog = analogRead(potentiometerPin); 27 | 28 | // "more than 6" allows for some noise 29 | if (analog < previousAnalog - 6 || analog > previousAnalog + 6) { 30 | throttle = analog / 1023.0; 31 | previousAnalog = analog; 32 | int angle = throttle * 70.0 + 30.0; 33 | motor.write(angle); 34 | Serial.print("(Analog) Throttle = "); 35 | Serial.println(throttle); 36 | } 37 | } 38 | 39 | // When X-Plane changes the throttle.... 40 | // 41 | void viewThrottle(float val) 42 | { 43 | int angle = val * 70.0 + 30.0; 44 | motor.write(angle); 45 | Serial.print("(X-Plane) Throttle = "); 46 | Serial.println(throttle); 47 | } 48 | -------------------------------------------------------------------------------- /USB_Joystick/Basic/Basic.ino: -------------------------------------------------------------------------------- 1 | /* Basic USB Joystick Example 2 | Teensy becomes a USB joystick 3 | 4 | You must select Joystick from the "Tools > USB Type" menu 5 | 6 | Pushbuttons should be connected to digital pins 0 and 1. 7 | Wire each button between the digital pin and ground. 8 | Potentiometers should be connected to analog inputs 0 to 1. 9 | 10 | This example code is in the public domain. 11 | */ 12 | 13 | void setup() { 14 | pinMode(0, INPUT_PULLUP); 15 | pinMode(1, INPUT_PULLUP); 16 | } 17 | 18 | void loop() { 19 | // read analog inputs and set X-Y position 20 | Joystick.X(analogRead(0)); 21 | Joystick.Y(analogRead(1)); 22 | 23 | // read the digital inputs and set the buttons 24 | Joystick.button(1, digitalRead(0)); 25 | Joystick.button(2, digitalRead(1)); 26 | 27 | // a brief delay, so this runs 20 times per second 28 | delay(50); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /USB_Joystick/Buttons/Buttons.ino: -------------------------------------------------------------------------------- 1 | /* Buttons to USB Joystick Example 2 | 3 | You must select Joystick from the "Tools > USB Type" menu 4 | 5 | This example code is in the public domain. 6 | */ 7 | 8 | #include 9 | 10 | // Create Bounce objects for each button. The Bounce object 11 | // automatically deals with contact chatter or "bounce", and 12 | // it makes detecting changes very simple. 13 | Bounce button0 = Bounce(0, 10); 14 | Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time 15 | Bounce button2 = Bounce(2, 10); // which is appropriate for 16 | Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons 17 | Bounce button4 = Bounce(4, 10); 18 | Bounce button5 = Bounce(5, 10); 19 | Bounce button6 = Bounce(6, 10); 20 | Bounce button7 = Bounce(7, 10); 21 | Bounce button8 = Bounce(8, 10); 22 | Bounce button9 = Bounce(9, 10); 23 | 24 | void setup() { 25 | // Configure the pins for input mode with pullup resistors. 26 | // The pushbuttons connect from each pin to ground. When 27 | // the button is pressed, the pin reads LOW because the button 28 | // shorts it to ground. When released, the pin reads HIGH 29 | // because the pullup resistor connects to +5 volts inside 30 | // the chip. LOW for "on", and HIGH for "off" may seem 31 | // backwards, but using the on-chip pullup resistors is very 32 | // convenient. The scheme is called "active low", and it's 33 | // very commonly used in electronics... so much that the chip 34 | // has built-in pullup resistors! 35 | pinMode(0, INPUT_PULLUP); 36 | pinMode(1, INPUT_PULLUP); 37 | pinMode(2, INPUT_PULLUP); 38 | pinMode(3, INPUT_PULLUP); 39 | pinMode(4, INPUT_PULLUP); 40 | pinMode(5, INPUT_PULLUP); 41 | pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup 42 | pinMode(7, INPUT_PULLUP); 43 | pinMode(8, INPUT_PULLUP); 44 | pinMode(9, INPUT_PULLUP); 45 | 46 | // Please be aware the X, Y, Z, Zr and Slider axes will have default 47 | // settings, if you only use the buttons. This can give the appearance 48 | // of the buttons interfering with the axes, if your PC software shows 49 | // different default assumed values before your first button press. 50 | // More details here: 51 | // https://forum.pjrc.com/threads/29320-Teensy-3-1-Button-problems?p=80275#post80275 52 | } 53 | 54 | void loop() { 55 | // Update all the buttons. There should not be any long 56 | // delays in loop(), so this runs repetitively at a rate 57 | // faster than the buttons could be pressed and released. 58 | button0.update(); 59 | button1.update(); 60 | button2.update(); 61 | button3.update(); 62 | button4.update(); 63 | button5.update(); 64 | button6.update(); 65 | button7.update(); 66 | button8.update(); 67 | button9.update(); 68 | 69 | // Check each button for "falling" edge. 70 | // Update the Joystick buttons only upon changes. 71 | // falling = high (not pressed - voltage from pullup resistor) 72 | // to low (pressed - button connects pin to ground) 73 | if (button0.fallingEdge()) { 74 | Joystick.button(1, 1); 75 | } 76 | if (button1.fallingEdge()) { 77 | Joystick.button(2, 1); 78 | } 79 | if (button2.fallingEdge()) { 80 | Joystick.button(3, 1); 81 | } 82 | if (button3.fallingEdge()) { 83 | Joystick.button(4, 1); 84 | } 85 | if (button4.fallingEdge()) { 86 | Joystick.button(5, 1); 87 | } 88 | if (button5.fallingEdge()) { 89 | Joystick.button(6, 1); 90 | } 91 | if (button6.fallingEdge()) { 92 | Joystick.button(7, 1); 93 | } 94 | if (button7.fallingEdge()) { 95 | Joystick.button(8, 1); 96 | } 97 | if (button8.fallingEdge()) { 98 | Joystick.button(9, 1); 99 | } 100 | if (button9.fallingEdge()) { 101 | Joystick.button(10, 1); 102 | } 103 | 104 | // Check each button for "rising" edge 105 | // Update the Joystick buttons only upon changes. 106 | // rising = low (pressed - button connects pin to ground) 107 | // to high (not pressed - voltage from pullup resistor) 108 | if (button0.risingEdge()) { 109 | Joystick.button(1, 0); 110 | } 111 | if (button1.risingEdge()) { 112 | Joystick.button(2, 0); 113 | } 114 | if (button2.risingEdge()) { 115 | Joystick.button(3, 0); 116 | } 117 | if (button3.risingEdge()) { 118 | Joystick.button(4, 0); 119 | } 120 | if (button4.risingEdge()) { 121 | Joystick.button(5, 0); 122 | } 123 | if (button5.risingEdge()) { 124 | Joystick.button(6, 0); 125 | } 126 | if (button6.risingEdge()) { 127 | Joystick.button(7, 0); 128 | } 129 | if (button7.risingEdge()) { 130 | Joystick.button(8, 0); 131 | } 132 | if (button8.risingEdge()) { 133 | Joystick.button(9, 0); 134 | } 135 | if (button9.risingEdge()) { 136 | Joystick.button(10, 0); 137 | } 138 | } 139 | 140 | -------------------------------------------------------------------------------- /USB_Joystick/Complete/Complete.ino: -------------------------------------------------------------------------------- 1 | /* Complete USB Joystick Example 2 | Teensy becomes a USB joystick with 16 or 32 buttons and 6 axis input 3 | 4 | You must select Joystick from the "Tools > USB Type" menu 5 | 6 | Pushbuttons should be connected between the digital pins and ground. 7 | Potentiometers should be connected to analog inputs 0 to 5. 8 | 9 | This example code is in the public domain. 10 | */ 11 | 12 | // Configure the number of buttons. Be careful not 13 | // to use a pin for both a digital button and analog 14 | // axis. The pullup resistor will interfere with 15 | // the analog voltage. 16 | const int numButtons = 16; // 16 for Teensy, 32 for Teensy++ 17 | 18 | void setup() { 19 | // you can print to the serial monitor while the joystick is active! 20 | Serial.begin(9600); 21 | // configure the joystick to manual send mode. This gives precise 22 | // control over when the computer receives updates, but it does 23 | // require you to manually call Joystick.send_now(). 24 | Joystick.useManualSend(true); 25 | for (int i=0; i= 360) angle = 0; 60 | Joystick.hat(angle); 61 | 62 | // Because setup configured the Joystick manual send, 63 | // the computer does not see any of the changes yet. 64 | // This send_now() transmits everything all at once. 65 | Joystick.send_now(); 66 | 67 | // check to see if any button changed since last time 68 | boolean anyChange = false; 69 | for (int i=0; i USB Type" menu 4 | 5 | This example code is in the public domain. 6 | */ 7 | 8 | #include 9 | 10 | // Create Bounce objects for each button. The Bounce object 11 | // automatically deals with contact chatter or "bounce", and 12 | // it makes detecting changes very simple. 13 | Bounce button0 = Bounce(0, 10); 14 | Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time 15 | Bounce button2 = Bounce(2, 10); // which is appropriate for 16 | Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons 17 | Bounce button4 = Bounce(4, 10); 18 | Bounce button5 = Bounce(5, 10); // if a button is too "sensitive" 19 | Bounce button6 = Bounce(6, 10); // to rapid touch, you can 20 | Bounce button7 = Bounce(7, 10); // increase this time. 21 | Bounce button8 = Bounce(8, 10); 22 | Bounce button9 = Bounce(9, 10); 23 | 24 | void setup() { 25 | // Configure the pins for input mode with pullup resistors. 26 | // The pushbuttons connect from each pin to ground. When 27 | // the button is pressed, the pin reads LOW because the button 28 | // shorts it to ground. When released, the pin reads HIGH 29 | // because the pullup resistor connects to +5 volts inside 30 | // the chip. LOW for "on", and HIGH for "off" may seem 31 | // backwards, but using the on-chip pullup resistors is very 32 | // convenient. The scheme is called "active low", and it's 33 | // very commonly used in electronics... so much that the chip 34 | // has built-in pullup resistors! 35 | pinMode(0, INPUT_PULLUP); 36 | pinMode(1, INPUT_PULLUP); 37 | pinMode(2, INPUT_PULLUP); 38 | pinMode(3, INPUT_PULLUP); 39 | pinMode(4, INPUT_PULLUP); 40 | pinMode(5, INPUT_PULLUP); 41 | pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup 42 | pinMode(7, INPUT_PULLUP); 43 | pinMode(8, INPUT_PULLUP); 44 | pinMode(9, INPUT_PULLUP); 45 | } 46 | 47 | void loop() { 48 | // Update all the buttons. There should not be any long 49 | // delays in loop(), so this runs repetitively at a rate 50 | // faster than the buttons could be pressed and released. 51 | button0.update(); 52 | button1.update(); 53 | button2.update(); 54 | button3.update(); 55 | button4.update(); 56 | button5.update(); 57 | button6.update(); 58 | button7.update(); 59 | button8.update(); 60 | button9.update(); 61 | 62 | // Check each button for "falling" edge. 63 | // Type a message on the Keyboard when each button presses 64 | // Update the Joystick buttons only upon changes. 65 | // falling = high (not pressed - voltage from pullup resistor) 66 | // to low (pressed - button connects pin to ground) 67 | if (button0.fallingEdge()) { 68 | Keyboard.println("B0 press"); 69 | } 70 | if (button1.fallingEdge()) { 71 | Keyboard.println("B1 press"); 72 | } 73 | if (button2.fallingEdge()) { 74 | Keyboard.println("B2 press"); 75 | } 76 | if (button3.fallingEdge()) { 77 | Keyboard.println("B3 press"); 78 | } 79 | if (button4.fallingEdge()) { 80 | Keyboard.println("B4 press"); 81 | } 82 | if (button5.fallingEdge()) { 83 | Keyboard.println("B5 press"); 84 | } 85 | if (button6.fallingEdge()) { 86 | Keyboard.println("B6 press"); 87 | } 88 | if (button7.fallingEdge()) { 89 | Keyboard.println("B7 press"); 90 | } 91 | if (button8.fallingEdge()) { 92 | Keyboard.println("B8 press"); 93 | } 94 | if (button9.fallingEdge()) { 95 | Keyboard.println("B9 press"); 96 | } 97 | 98 | // Check each button for "rising" edge 99 | // Type a message on the Keyboard when each button releases. 100 | // For many types of projects, you only care when the button 101 | // is pressed and the release isn't needed. 102 | // rising = low (pressed - button connects pin to ground) 103 | // to high (not pressed - voltage from pullup resistor) 104 | if (button0.risingEdge()) { 105 | Keyboard.println("B0 release"); 106 | } 107 | if (button1.risingEdge()) { 108 | Keyboard.println("B1 release"); 109 | } 110 | if (button2.risingEdge()) { 111 | Keyboard.println("B2 release"); 112 | } 113 | if (button3.risingEdge()) { 114 | Keyboard.println("B3 release"); 115 | } 116 | if (button4.risingEdge()) { 117 | Keyboard.println("B4 release"); 118 | } 119 | if (button5.risingEdge()) { 120 | Keyboard.println("B5 release"); 121 | } 122 | if (button6.risingEdge()) { 123 | Keyboard.println("B6 release"); 124 | } 125 | if (button7.risingEdge()) { 126 | Keyboard.println("B7 release"); 127 | } 128 | if (button8.risingEdge()) { 129 | Keyboard.println("B8 release"); 130 | } 131 | if (button9.risingEdge()) { 132 | Keyboard.println("B9 release"); 133 | } 134 | } 135 | 136 | -------------------------------------------------------------------------------- /USB_Keyboard/LayoutTest/LayoutTest.ino: -------------------------------------------------------------------------------- 1 | /* USB Keyboard Layout Test 2 | 3 | You must select Keyboard from the "Tools > USB Type" menu 4 | 5 | Select the the correct layout from "Tools > Keyboard Layout" 6 | 7 | If you discover incorrect results for your country's layout, 8 | please email Paul Stoffregen with the results 9 | of this test and an explanation of which keys are wrong. If 10 | your layout is not available, please find the layout which 11 | is closest, and email Paul Stoffregen. 12 | */ 13 | 14 | const int ledPin = 6; // Teensy 2.0 = Pin 11, Teensy++ 2.0 = Pin 6 15 | 16 | void setup() { 17 | Serial.begin(9600); 18 | 19 | // Blink the LED for 10 seconds, to give time to open 20 | // a word processor or text editor to receive the test 21 | pinMode(ledPin, OUTPUT); 22 | for (int i=0; i < 10; i++) { 23 | digitalWrite(ledPin, HIGH); 24 | delay(500); 25 | digitalWrite(ledPin, LOW); 26 | delay(500); 27 | } 28 | 29 | // Type all possible characters. Many countries do not use all 30 | // characters. Unsupported characters will be skipped 31 | // 32 | Keyboard.println("Teensy USB Keyboard Layout Test"); 33 | delay(100); 34 | Keyboard.println("Lowercase: abcdefghijklmnopqrstuvwxyz"); 35 | delay(100); 36 | Keyboard.println("Uppercase: ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 37 | delay(100); 38 | Keyboard.println("Numbers: 0123456789"); 39 | delay(100); 40 | Keyboard.println("Symbols1: !\"#$%&'()*+,-./"); 41 | delay(100); 42 | Keyboard.println("Symbols2: :;<=>?[\\]^_`{|}~"); 43 | delay(100); 44 | Keyboard.println("Symbols3: ¡¢£¤¥¦§¨©ª«¬­®¯°±"); 45 | delay(100); 46 | Keyboard.println("Symbols4: ²³´µ¶·¸¹º»¼½¾¿×÷"); 47 | delay(100); 48 | Keyboard.println("Grave: ÀÈÌÒÙàèìòù"); 49 | delay(100); 50 | Keyboard.println("Acute: ÁÉÍÓÚÝáéíóúý"); 51 | delay(100); 52 | Keyboard.println("Circumflex: ÂÊÎÔÛâêîôû"); 53 | delay(100); 54 | Keyboard.println("Tilde: ÃÑÕãñõ"); 55 | delay(100); 56 | Keyboard.println("Diaeresis: ÄËÏÖÜäëïöüÿ"); 57 | delay(100); 58 | Keyboard.println("Cedilla: Çç"); 59 | delay(100); 60 | Keyboard.println("Ring Above: Åå"); 61 | delay(100); 62 | Keyboard.println("AE: Ææ"); 63 | delay(100); 64 | Keyboard.println("Thorn: Þþ"); 65 | delay(100); 66 | Keyboard.println("Sharp S: ß"); 67 | delay(100); 68 | Keyboard.println("O-Stroke: Øø"); 69 | delay(100); 70 | Keyboard.println("Eth: Ðð"); 71 | delay(100); 72 | Keyboard.println("Euro: €"); 73 | } 74 | 75 | void loop() { 76 | // Do nothing after the test 77 | } 78 | -------------------------------------------------------------------------------- /USB_Keyboard/MediaButtons/MediaButtons.ino: -------------------------------------------------------------------------------- 1 | /* Buttons to USB Keyboard Example - Special Media Player Keys 2 | 3 | You must select Keyboard from the "Tools > USB Type" menu 4 | 5 | This example code is in the public domain. 6 | */ 7 | 8 | #include 9 | 10 | // Create Bounce objects for each button. The Bounce object 11 | // automatically deals with contact chatter or "bounce", and 12 | // it makes detecting changes very simple. 13 | Bounce button0 = Bounce(0, 10); 14 | Bounce button1 = Bounce(1, 10); // 10 ms debounce time is appropriate 15 | Bounce button2 = Bounce(2, 10); // for most mechanical pushbuttons 16 | Bounce button3 = Bounce(3, 10); 17 | Bounce button4 = Bounce(4, 10); // if a button is too "sensitive" 18 | Bounce button5 = Bounce(5, 10); // you can increase this time. 19 | Bounce button6 = Bounce(6, 10); 20 | 21 | void setup() { 22 | // Configure the pins for input mode with pullup resistors. 23 | // The pushbuttons connect from each pin to ground. When 24 | // the button is pressed, the pin reads LOW because the button 25 | // shorts it to ground. When released, the pin reads HIGH 26 | // because the pullup resistor connects to +5 volts inside 27 | // the chip. 28 | pinMode(0, INPUT_PULLUP); 29 | pinMode(1, INPUT_PULLUP); 30 | pinMode(2, INPUT_PULLUP); 31 | pinMode(3, INPUT_PULLUP); 32 | pinMode(4, INPUT_PULLUP); 33 | pinMode(5, INPUT_PULLUP); 34 | pinMode(6, INPUT_PULLUP); 35 | } 36 | 37 | void loop() { 38 | // Update all the buttons. There should not be any long 39 | // delays in loop(), so this runs repetitively at a rate 40 | // faster than the buttons could be pressed and released. 41 | button0.update(); 42 | button1.update(); 43 | button2.update(); 44 | button3.update(); 45 | button4.update(); 46 | button5.update(); 47 | button6.update(); 48 | 49 | // Check each button for "falling" edge. 50 | // falling = high (not pressed - voltage from pullup resistor) 51 | // to low (pressed - button connects pin to ground) 52 | if (button0.fallingEdge()) { 53 | Keyboard.press(KEY_MEDIA_PREV_TRACK); 54 | Keyboard.release(KEY_MEDIA_PREV_TRACK); 55 | } 56 | if (button1.fallingEdge()) { 57 | Keyboard.press(KEY_MEDIA_PLAY_PAUSE); 58 | Keyboard.release(KEY_MEDIA_PLAY_PAUSE); 59 | } 60 | if (button2.fallingEdge()) { 61 | Keyboard.press(KEY_MEDIA_NEXT_TRACK); 62 | Keyboard.release(KEY_MEDIA_NEXT_TRACK); 63 | } 64 | if (button3.fallingEdge()) { 65 | Keyboard.press(KEY_MEDIA_VOLUME_DEC); 66 | Keyboard.release(KEY_MEDIA_VOLUME_DEC); 67 | } 68 | if (button4.fallingEdge()) { 69 | Keyboard.press(KEY_MEDIA_VOLUME_INC); 70 | Keyboard.release(KEY_MEDIA_VOLUME_INC); 71 | } 72 | if (button5.fallingEdge()) { 73 | Keyboard.press(KEY_SYSTEM_POWER_DOWN); 74 | Keyboard.release(KEY_SYSTEM_POWER_DOWN); 75 | } 76 | if (button6.fallingEdge()) { 77 | Keyboard.press(KEY_MEDIA_EJECT); 78 | delay(300); // Mac OS-X will not recognize a very short eject press 79 | Keyboard.release(KEY_MEDIA_EJECT); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /USB_Keyboard/Simple/Simple.ino: -------------------------------------------------------------------------------- 1 | /* Simple USB Keyboard Example 2 | Teensy becomes a USB keyboard and types characters 3 | 4 | You must select Keyboard from the "Tools > USB Type" menu 5 | 6 | This example code is in the public domain. 7 | */ 8 | 9 | int count = 0; 10 | 11 | void setup() { 12 | Serial.begin(9600); 13 | delay(1000); 14 | } 15 | 16 | void loop() { 17 | // Your computer will receive these characters from a USB keyboard. 18 | Keyboard.print("Hello World "); 19 | Keyboard.println(count); 20 | 21 | // You can also send to the Arduino Serial Monitor 22 | Serial.println(count); 23 | 24 | // increment the count 25 | count = count + 1; 26 | 27 | // typing too rapidly can overwhelm a PC 28 | delay(5000); 29 | } 30 | -------------------------------------------------------------------------------- /USB_MIDI/AnalogControlChange/AnalogControlChange.ino: -------------------------------------------------------------------------------- 1 | /* USB MIDI AnalogControlChange Example 2 | 3 | You must select MIDI from the "Tools > USB Type" menu 4 | http://www.pjrc.com/teensy/td_midi.html 5 | 6 | This example code is in the public domain. 7 | */ 8 | 9 | #include 10 | 11 | // the MIDI channel number to send messages 12 | const int channel = 1; 13 | 14 | // the MIDI continuous controller for each analog input 15 | const int controllerA0 = 10; // 10 = pan position 16 | const int controllerA1 = 11; // 11 = volume/expression 17 | const int controllerA2 = 91; // 91 = reverb level 18 | const int controllerA3 = 93; // 93 = chorus level 19 | 20 | void setup() { 21 | } 22 | 23 | // store previously sent values, to detect changes 24 | int previousA0 = -1; 25 | int previousA1 = -1; 26 | int previousA2 = -1; 27 | int previousA3 = -1; 28 | 29 | elapsedMillis msec = 0; 30 | 31 | void loop() { 32 | // only check the analog inputs 50 times per second, 33 | // to prevent a flood of MIDI messages 34 | if (msec >= 20) { 35 | msec = 0; 36 | int n0 = analogRead(A0) / 8; 37 | int n1 = analogRead(A1) / 8; 38 | int n2 = analogRead(A2) / 8; 39 | int n3 = analogRead(A3) / 8; 40 | // only transmit MIDI messages if analog input changed 41 | if (n0 != previousA0) { 42 | usbMIDI.sendControlChange(controllerA0, n0, channel); 43 | previousA0 = n0; 44 | } 45 | if (n1 != previousA1) { 46 | usbMIDI.sendControlChange(controllerA1, n1, channel); 47 | previousA1 = n1; 48 | } 49 | if (n2 != previousA2) { 50 | usbMIDI.sendControlChange(controllerA2, n2, channel); 51 | previousA2 = n2; 52 | } 53 | if (n3 != previousA3) { 54 | usbMIDI.sendControlChange(controllerA3, n3, channel); 55 | previousA3 = n3; 56 | } 57 | } 58 | 59 | // MIDI Controllers should discard incoming MIDI messages. 60 | // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash 61 | while (usbMIDI.read()) { 62 | // ignore incoming messages 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /USB_MIDI/Buttons/Buttons.ino: -------------------------------------------------------------------------------- 1 | /* Buttons to USB MIDI Example 2 | 3 | You must select MIDI from the "Tools > USB Type" menu 4 | 5 | To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI" 6 | 7 | This example code is in the public domain. 8 | */ 9 | 10 | #include 11 | 12 | // the MIDI channel number to send messages 13 | const int channel = 1; 14 | 15 | // Create Bounce objects for each button. The Bounce object 16 | // automatically deals with contact chatter or "bounce", and 17 | // it makes detecting changes very simple. 18 | Bounce button0 = Bounce(0, 5); 19 | Bounce button1 = Bounce(1, 5); // 5 = 5 ms debounce time 20 | Bounce button2 = Bounce(2, 5); // which is appropriate for good 21 | Bounce button3 = Bounce(3, 5); // quality mechanical pushbuttons 22 | Bounce button4 = Bounce(4, 5); 23 | Bounce button5 = Bounce(5, 5); // if a button is too "sensitive" 24 | Bounce button6 = Bounce(6, 5); // to rapid touch, you can 25 | Bounce button7 = Bounce(7, 5); // increase this time. 26 | Bounce button8 = Bounce(8, 5); 27 | Bounce button9 = Bounce(9, 5); 28 | Bounce button10 = Bounce(10, 5); 29 | Bounce button11 = Bounce(11, 5); 30 | 31 | void setup() { 32 | // Configure the pins for input mode with pullup resistors. 33 | // The pushbuttons connect from each pin to ground. When 34 | // the button is pressed, the pin reads LOW because the button 35 | // shorts it to ground. When released, the pin reads HIGH 36 | // because the pullup resistor connects to +5 volts inside 37 | // the chip. LOW for "on", and HIGH for "off" may seem 38 | // backwards, but using the on-chip pullup resistors is very 39 | // convenient. The scheme is called "active low", and it's 40 | // very commonly used in electronics... so much that the chip 41 | // has built-in pullup resistors! 42 | pinMode(0, INPUT_PULLUP); 43 | pinMode(1, INPUT_PULLUP); 44 | pinMode(2, INPUT_PULLUP); 45 | pinMode(3, INPUT_PULLUP); 46 | pinMode(4, INPUT_PULLUP); 47 | pinMode(5, INPUT_PULLUP); 48 | pinMode(6, INPUT_PULLUP); // Teensy++ 2.0 LED, may need 1k resistor pullup 49 | pinMode(7, INPUT_PULLUP); 50 | pinMode(8, INPUT_PULLUP); 51 | pinMode(9, INPUT_PULLUP); 52 | pinMode(10, INPUT_PULLUP); 53 | pinMode(11, INPUT_PULLUP); // Teensy 2.0 LED, may need 1k resistor pullup 54 | } 55 | 56 | void loop() { 57 | // Update all the buttons. There should not be any long 58 | // delays in loop(), so this runs repetitively at a rate 59 | // faster than the buttons could be pressed and released. 60 | button0.update(); 61 | button1.update(); 62 | button2.update(); 63 | button3.update(); 64 | button4.update(); 65 | button5.update(); 66 | button6.update(); 67 | button7.update(); 68 | button8.update(); 69 | button9.update(); 70 | button10.update(); 71 | button11.update(); 72 | 73 | // Check each button for "falling" edge. 74 | // Send a MIDI Note On message when each button presses 75 | // Update the Joystick buttons only upon changes. 76 | // falling = high (not pressed - voltage from pullup resistor) 77 | // to low (pressed - button connects pin to ground) 78 | if (button0.fallingEdge()) { 79 | usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4 80 | } 81 | if (button1.fallingEdge()) { 82 | usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4 83 | } 84 | if (button2.fallingEdge()) { 85 | usbMIDI.sendNoteOn(62, 99, channel); // 62 = D4 86 | } 87 | if (button3.fallingEdge()) { 88 | usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4 89 | } 90 | if (button4.fallingEdge()) { 91 | usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4 92 | } 93 | if (button5.fallingEdge()) { 94 | usbMIDI.sendNoteOn(65, 99, channel); // 65 = F4 95 | } 96 | if (button6.fallingEdge()) { 97 | usbMIDI.sendNoteOn(66, 99, channel); // 66 = F#4 98 | } 99 | if (button7.fallingEdge()) { 100 | usbMIDI.sendNoteOn(67, 99, channel); // 67 = G4 101 | } 102 | if (button8.fallingEdge()) { 103 | usbMIDI.sendNoteOn(68, 99, channel); // 68 = G#4 104 | } 105 | if (button9.fallingEdge()) { 106 | usbMIDI.sendNoteOn(69, 99, channel); // 69 = A5 107 | } 108 | if (button10.fallingEdge()) { 109 | usbMIDI.sendNoteOn(70, 99, channel); // 70 = A#5 110 | } 111 | if (button11.fallingEdge()) { 112 | usbMIDI.sendNoteOn(71, 99, channel); // 71 = B5 113 | } 114 | 115 | // Check each button for "rising" edge 116 | // Send a MIDI Note Off message when each button releases 117 | // For many types of projects, you only care when the button 118 | // is pressed and the release isn't needed. 119 | // rising = low (pressed - button connects pin to ground) 120 | // to high (not pressed - voltage from pullup resistor) 121 | if (button0.risingEdge()) { 122 | usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4 123 | } 124 | if (button1.risingEdge()) { 125 | usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4 126 | } 127 | if (button2.risingEdge()) { 128 | usbMIDI.sendNoteOff(62, 0, channel); // 62 = D4 129 | } 130 | if (button3.risingEdge()) { 131 | usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4 132 | } 133 | if (button4.risingEdge()) { 134 | usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4 135 | } 136 | if (button5.risingEdge()) { 137 | usbMIDI.sendNoteOff(65, 0, channel); // 65 = F4 138 | } 139 | if (button6.risingEdge()) { 140 | usbMIDI.sendNoteOff(66, 0, channel); // 66 = F#4 141 | } 142 | if (button7.risingEdge()) { 143 | usbMIDI.sendNoteOff(67, 0, channel); // 67 = G4 144 | } 145 | if (button8.risingEdge()) { 146 | usbMIDI.sendNoteOff(68, 0, channel); // 68 = G#4 147 | } 148 | if (button9.risingEdge()) { 149 | usbMIDI.sendNoteOff(69, 0, channel); // 69 = A5 150 | } 151 | if (button10.risingEdge()) { 152 | usbMIDI.sendNoteOff(70, 0, channel); // 70 = A#5 153 | } 154 | if (button11.risingEdge()) { 155 | usbMIDI.sendNoteOff(71, 0, channel); // 71 = B5 156 | } 157 | 158 | // MIDI Controllers should discard incoming MIDI messages. 159 | // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash 160 | while (usbMIDI.read()) { 161 | // ignore incoming messages 162 | } 163 | } 164 | 165 | -------------------------------------------------------------------------------- /USB_MIDI/DMX_Lighting/DMX_Lighting.ino: -------------------------------------------------------------------------------- 1 | /* Control DMX lighting with MIDI control change messages. DJ and 2 | stage lighting can be easily controlled by software with can 3 | send MIDI messages. 4 | 5 | https://github.com/jmej/MIDI2DMX 6 | 7 | Teensy TX1 (pin 1 on Teensy LC and 3.x) must be connected to a 8 | RS422/RS485 transmitter capable of driving a DMX lighting cable. 9 | */ 10 | 11 | 12 | 13 | #include "leddmx.h" 14 | 15 | void setup() { 16 | Serial1.begin(250000); 17 | usbMIDI.setHandleControlChange(OnControlChange); 18 | } 19 | 20 | void OnControlChange(byte channel, byte control, byte value) { 21 | led_channel(control, value*2); 22 | // Serial.println("got midi "); 23 | // Serial.println(value); 24 | } 25 | 26 | void loop() { 27 | leds_update(); 28 | usbMIDI.read(); // USB MIDI receive 29 | } 30 | 31 | -------------------------------------------------------------------------------- /USB_MIDI/DMX_Lighting/leddmx.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | extern void leds_update(); 4 | extern void led_color(unsigned int num, unsigned int rgb); 5 | extern void led_color(unsigned int num, unsigned char red, unsigned char green, unsigned char blue); 6 | 7 | 8 | #define CHANNELS 127 9 | 10 | static unsigned char ch[CHANNELS]; 11 | static int state=0; 12 | static int chindex=0; 13 | static int refresh_needed=1; 14 | static elapsedMicros transmittimer; 15 | 16 | void leds_update() 17 | { 18 | int num, avail; 19 | 20 | switch (state) { 21 | case 0: // idle 22 | // if (refresh_needed == 0) return; 23 | Serial1.begin(83333, SERIAL_8N1); 24 | Serial1.write(0); 25 | transmittimer = 0; 26 | refresh_needed = 0; 27 | state = 1; 28 | break; 29 | case 1: // waiting for break 30 | if (transmittimer < 150) return; 31 | Serial1.flush(); // should already be done 32 | Serial1.begin(250000, SERIAL_8N2); 33 | transmittimer = 0; 34 | Serial1.write(0); 35 | chindex = 0; 36 | state = 2; 37 | break; 38 | case 2: // transmit data 39 | avail = Serial1.availableForWrite(); 40 | if (avail == 0) return; 41 | num = CHANNELS - chindex; 42 | if (num > avail) num = avail; 43 | Serial1.write(ch + chindex, num); 44 | chindex += num; 45 | if (chindex >= CHANNELS) state = 3; 46 | break; 47 | case 3: // waiting to transmit to complete 48 | if (transmittimer < CHANNELS * 44 + 100) return; 49 | Serial1.flush(); // should already be done 50 | state = 0; 51 | break; 52 | default: 53 | Serial1.begin(250000, SERIAL_8N2); 54 | state = 0; 55 | } 56 | } 57 | 58 | void led_channel(unsigned int num, unsigned int val) 59 | { 60 | if (num == 0) return; // LEDs start numbering at 1 61 | if (val > 255) val = 255; 62 | ch[num - 1] = val; 63 | refresh_needed = 1; 64 | } 65 | 66 | void led_color(unsigned int num, unsigned int rgb) 67 | { 68 | led_color(num, rgb >> 16, rgb >> 8, rgb); 69 | } 70 | 71 | void led_color(unsigned int num, unsigned char red, unsigned char green, unsigned char blue) 72 | { 73 | if (num == 0) return; // LEDs start numbering at 1 74 | num = (num - 1) * 3; 75 | if (num + 2 >= CHANNELS) return; 76 | if (ch[num] != red) { 77 | ch[num] = red; 78 | refresh_needed = 1; 79 | } 80 | if (ch[num+1] != green) { 81 | ch[num+1] = green; 82 | refresh_needed = 1; 83 | } 84 | if (ch[num+2] != blue) { 85 | ch[num+2] = blue; 86 | refresh_needed = 1; 87 | } 88 | } 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /USB_MIDI/DMX_Lighting/leddmx.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void leds_update(); 4 | void led_color(unsigned int num, unsigned int rgb); 5 | void led_color(unsigned int num, unsigned char red, unsigned char green, unsigned char blue); 6 | void led_channel(unsigned int num, unsigned int val); 7 | 8 | -------------------------------------------------------------------------------- /USB_MIDI/InputFunctionsBasic/InputFunctionsBasic.ino: -------------------------------------------------------------------------------- 1 | /* Receive Incoming USB MIDI using functions. As usbMIDI 2 | reads incoming messages, handler functions are run. 3 | See the InputRead example for the non-function alterative. 4 | 5 | This small example shows only 3 common messages, to keep 6 | things simple. See InputFunctionsComplete for an example 7 | with all possible messages. 8 | 9 | Use the Arduino Serial Monitor to view the messages 10 | as Teensy receives them by USB MIDI 11 | 12 | You must select MIDI from the "Tools > USB Type" menu 13 | 14 | This example code is in the public domain. 15 | */ 16 | 17 | 18 | void setup() { 19 | Serial.begin(115200); 20 | usbMIDI.setHandleNoteOn(myNoteOn); 21 | usbMIDI.setHandleNoteOff(myNoteOff); 22 | usbMIDI.setHandleControlChange(myControlChange); 23 | } 24 | 25 | void loop() { 26 | // The handler functions are called when usbMIDI reads data. They 27 | // will not be called automatically. You must call usbMIDI.read() 28 | // regularly from loop() for usbMIDI to actually read incoming 29 | // data and run the handler functions as messages arrive. 30 | usbMIDI.read(); 31 | } 32 | 33 | 34 | void myNoteOn(byte channel, byte note, byte velocity) { 35 | // When using MIDIx4 or MIDIx16, usbMIDI.getCable() can be used 36 | // to read which of the virtual MIDI cables received this message. 37 | Serial.print("Note On, ch="); 38 | Serial.print(channel, DEC); 39 | Serial.print(", note="); 40 | Serial.print(note, DEC); 41 | Serial.print(", velocity="); 42 | Serial.println(velocity, DEC); 43 | } 44 | 45 | void myNoteOff(byte channel, byte note, byte velocity) { 46 | Serial.print("Note Off, ch="); 47 | Serial.print(channel, DEC); 48 | Serial.print(", note="); 49 | Serial.print(note, DEC); 50 | Serial.print(", velocity="); 51 | Serial.println(velocity, DEC); 52 | } 53 | 54 | void myControlChange(byte channel, byte control, byte value) { 55 | Serial.print("Control Change, ch="); 56 | Serial.print(channel, DEC); 57 | Serial.print(", control="); 58 | Serial.print(control, DEC); 59 | Serial.print(", value="); 60 | Serial.println(value, DEC); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /USB_MIDI/InputFunctionsComplete/InputFunctionsComplete.ino: -------------------------------------------------------------------------------- 1 | /* Receive Incoming USB MIDI using functions. As usbMIDI 2 | reads incoming messages, handler functions are run. 3 | See the InputRead example for the non-function alterative. 4 | 5 | This very long example demonstrates all possible handler 6 | functions. Most applications need only some of these. 7 | This example is meant to allow easy copy-and-paste of the 8 | desired functions. See InputFunctionsBasic for a smaller 9 | example using only the most common function. 10 | 11 | Use the Arduino Serial Monitor to view the messages 12 | as Teensy receives them by USB MIDI 13 | 14 | You must select MIDI from the "Tools > USB Type" menu 15 | 16 | On Linux systems, use "amidi" to send from command line: 17 | amidi -p hw:3,0,0 -S '90 68 64' 18 | amidi -p hw:3,0,0 -S 'F0 43 10 4C 00 00 7E 00 F7' 19 | 20 | This example code is in the public domain. 21 | */ 22 | 23 | 24 | void setup() { 25 | Serial.begin(115200); 26 | usbMIDI.setHandleNoteOn(myNoteOn); 27 | usbMIDI.setHandleNoteOff(myNoteOff); 28 | usbMIDI.setHandleAfterTouchPoly(myAfterTouchPoly); 29 | usbMIDI.setHandleControlChange(myControlChange); 30 | usbMIDI.setHandleProgramChange(myProgramChange); 31 | usbMIDI.setHandleAfterTouchChannel(myAfterTouchChannel); 32 | usbMIDI.setHandlePitchChange(myPitchChange); 33 | // Only one of these System Exclusive handlers will actually be 34 | // used. See the comments below for the difference between them. 35 | usbMIDI.setHandleSystemExclusive(mySystemExclusiveChunk); 36 | usbMIDI.setHandleSystemExclusive(mySystemExclusive); 37 | usbMIDI.setHandleTimeCodeQuarterFrame(myTimeCodeQuarterFrame); 38 | usbMIDI.setHandleSongPosition(mySongPosition); 39 | usbMIDI.setHandleSongSelect(mySongSelect); 40 | usbMIDI.setHandleTuneRequest(myTuneRequest); 41 | usbMIDI.setHandleClock(myClock); 42 | usbMIDI.setHandleStart(myStart); 43 | usbMIDI.setHandleContinue(myContinue); 44 | usbMIDI.setHandleStop(myStop); 45 | usbMIDI.setHandleActiveSensing(myActiveSensing); 46 | usbMIDI.setHandleSystemReset(mySystemReset); 47 | // This generic System Real Time handler is only used if the 48 | // more specific ones are not set. 49 | usbMIDI.setHandleRealTimeSystem(myRealTimeSystem); 50 | } 51 | 52 | void loop() { 53 | // The handler functions are called when usbMIDI reads data. They 54 | // will not be called automatically. You must call usbMIDI.read() 55 | // regularly from loop() for usbMIDI to actually read incoming 56 | // data and run the handler functions as messages arrive. 57 | usbMIDI.read(); 58 | } 59 | 60 | 61 | void myNoteOn(byte channel, byte note, byte velocity) { 62 | // When using MIDIx4 or MIDIx16, usbMIDI.getCable() can be used 63 | // to read which of the virtual MIDI cables received this message. 64 | Serial.print("Note On, ch="); 65 | Serial.print(channel, DEC); 66 | Serial.print(", note="); 67 | Serial.print(note, DEC); 68 | Serial.print(", velocity="); 69 | Serial.println(velocity, DEC); 70 | } 71 | 72 | void myNoteOff(byte channel, byte note, byte velocity) { 73 | Serial.print("Note Off, ch="); 74 | Serial.print(channel, DEC); 75 | Serial.print(", note="); 76 | Serial.print(note, DEC); 77 | Serial.print(", velocity="); 78 | Serial.println(velocity, DEC); 79 | } 80 | 81 | void myAfterTouchPoly(byte channel, byte note, byte velocity) { 82 | Serial.print("AfterTouch Change, ch="); 83 | Serial.print(channel, DEC); 84 | Serial.print(", note="); 85 | Serial.print(note, DEC); 86 | Serial.print(", velocity="); 87 | Serial.println(velocity, DEC); 88 | } 89 | 90 | void myControlChange(byte channel, byte control, byte value) { 91 | Serial.print("Control Change, ch="); 92 | Serial.print(channel, DEC); 93 | Serial.print(", control="); 94 | Serial.print(control, DEC); 95 | Serial.print(", value="); 96 | Serial.println(value, DEC); 97 | } 98 | 99 | void myProgramChange(byte channel, byte program) { 100 | Serial.print("Program Change, ch="); 101 | Serial.print(channel, DEC); 102 | Serial.print(", program="); 103 | Serial.println(program, DEC); 104 | } 105 | 106 | void myAfterTouchChannel(byte channel, byte pressure) { 107 | Serial.print("After Touch, ch="); 108 | Serial.print(channel, DEC); 109 | Serial.print(", pressure="); 110 | Serial.println(pressure, DEC); 111 | } 112 | 113 | void myPitchChange(byte channel, int pitch) { 114 | Serial.print("Pitch Change, ch="); 115 | Serial.print(channel, DEC); 116 | Serial.print(", pitch="); 117 | Serial.println(pitch, DEC); 118 | } 119 | 120 | 121 | // This 3-input System Exclusive function is more complex, but allows you to 122 | // process very large messages which do not fully fit within the usbMIDI's 123 | // internal buffer. Large messages are given to you in chunks, with the 124 | // 3rd parameter to tell you which is the last chunk. This function is 125 | // a Teensy extension, not available in the Arduino MIDI library. 126 | // 127 | void mySystemExclusiveChunk(const byte *data, uint16_t length, bool last) { 128 | Serial.print("SysEx Message: "); 129 | printBytes(data, length); 130 | if (last) { 131 | Serial.println(" (end)"); 132 | } else { 133 | Serial.println(" (to be continued)"); 134 | } 135 | } 136 | 137 | // This simpler 2-input System Exclusive function can only receive messages 138 | // up to the size of the internal buffer. Larger messages are truncated, with 139 | // no way to receive the data which did not fit in the buffer. If both types 140 | // of SysEx functions are set, the 3-input version will be called by usbMIDI. 141 | // 142 | void mySystemExclusive(byte *data, unsigned int length) { 143 | Serial.print("SysEx Message: "); 144 | printBytes(data, length); 145 | Serial.println(); 146 | } 147 | 148 | void myTimeCodeQuarterFrame(byte data) { 149 | static char SMPTE[8]={'0','0','0','0','0','0','0','0'}; 150 | static byte fps=0; 151 | byte index = data >> 4; 152 | byte number = data & 15; 153 | if (index == 7) { 154 | fps = (number >> 1) & 3; 155 | number = number & 1; 156 | } 157 | if (index < 8 || number < 10) { 158 | SMPTE[index] = number + '0'; 159 | Serial.print("TimeCode: "); // perhaps only print when index == 7 160 | Serial.print(SMPTE[7]); 161 | Serial.print(SMPTE[6]); 162 | Serial.print(':'); 163 | Serial.print(SMPTE[5]); 164 | Serial.print(SMPTE[4]); 165 | Serial.print(':'); 166 | Serial.print(SMPTE[3]); 167 | Serial.print(SMPTE[2]); 168 | Serial.print('.'); 169 | Serial.print(SMPTE[1]); // perhaps add 2 to compensate for MIDI latency? 170 | Serial.print(SMPTE[0]); 171 | switch (fps) { 172 | case 0: Serial.println(" 24 fps"); break; 173 | case 1: Serial.println(" 25 fps"); break; 174 | case 2: Serial.println(" 29.97 fps"); break; 175 | case 3: Serial.println(" 30 fps"); break; 176 | } 177 | } else { 178 | Serial.print("TimeCode: invalid data = "); 179 | Serial.println(data, HEX); 180 | } 181 | } 182 | 183 | void mySongPosition(uint16_t beats) { 184 | Serial.print("Song Position, beat="); 185 | Serial.println(beats); 186 | } 187 | 188 | void mySongSelect(byte songNumber) { 189 | Serial.print("Song Select, song="); 190 | Serial.println(songNumber, DEC); 191 | } 192 | 193 | void myTuneRequest() { 194 | Serial.println("Tune Request"); 195 | } 196 | 197 | void myClock() { 198 | Serial.println("Clock"); 199 | } 200 | 201 | void myStart() { 202 | Serial.println("Start"); 203 | } 204 | 205 | void myContinue() { 206 | Serial.println("Continue"); 207 | } 208 | 209 | void myStop() { 210 | Serial.println("Stop"); 211 | } 212 | 213 | void myActiveSensing() { 214 | Serial.println("Actvice Sensing"); 215 | } 216 | 217 | void mySystemReset() { 218 | Serial.println("System Reset"); 219 | } 220 | 221 | void myRealTimeSystem(uint8_t realtimebyte) { 222 | Serial.print("Real Time Message, code="); 223 | Serial.println(realtimebyte, HEX); 224 | } 225 | 226 | 227 | 228 | void printBytes(const byte *data, unsigned int size) { 229 | while (size > 0) { 230 | byte b = *data++; 231 | if (b < 16) Serial.print('0'); 232 | Serial.print(b, HEX); 233 | if (size > 1) Serial.print(' '); 234 | size = size - 1; 235 | } 236 | } 237 | 238 | -------------------------------------------------------------------------------- /USB_MIDI/InputRead/InputRead.ino: -------------------------------------------------------------------------------- 1 | /* Receive Incoming USB MIDI by reading data. This approach 2 | gives you access to incoming MIDI message data, but requires 3 | more work to use that data. For the simpler function-based 4 | approach, see InputFunctionsBasic and InputFunctionsComplete. 5 | 6 | Use the Arduino Serial Monitor to view the messages 7 | as Teensy receives them by USB MIDI 8 | 9 | You must select MIDI from the "Tools > USB Type" menu 10 | 11 | This example code is in the public domain. 12 | */ 13 | 14 | void setup() { 15 | Serial.begin(115200); 16 | } 17 | 18 | void loop() { 19 | // usbMIDI.read() needs to be called rapidly from loop(). When 20 | // each MIDI messages arrives, it return true. The message must 21 | // be fully processed before usbMIDI.read() is called again. 22 | if (usbMIDI.read()) { 23 | processMIDI(); 24 | } 25 | } 26 | 27 | void processMIDI(void) { 28 | byte type, channel, data1, data2, cable; 29 | 30 | // fetch the MIDI message, defined by these 5 numbers (except SysEX) 31 | // 32 | type = usbMIDI.getType(); // which MIDI message, 128-255 33 | channel = usbMIDI.getChannel(); // which MIDI channel, 1-16 34 | data1 = usbMIDI.getData1(); // first data byte of message, 0-127 35 | data2 = usbMIDI.getData2(); // second data byte of message, 0-127 36 | cable = usbMIDI.getCable(); // which virtual cable with MIDIx8, 0-7 37 | 38 | // uncomment if using multiple virtual cables 39 | //Serial.print("cable "); 40 | //Serial.print(cable, DEC); 41 | //Serial.print(": "); 42 | 43 | // print info about the message 44 | // 45 | switch (type) { 46 | case usbMIDI.NoteOff: // 0x80 47 | Serial.print("Note Off, ch="); 48 | Serial.print(channel, DEC); 49 | Serial.print(", note="); 50 | Serial.print(data1, DEC); 51 | Serial.print(", velocity="); 52 | Serial.println(data2, DEC); 53 | break; 54 | 55 | case usbMIDI.NoteOn: // 0x90 56 | Serial.print("Note On, ch="); 57 | Serial.print(channel, DEC); 58 | Serial.print(", note="); 59 | Serial.print(data1, DEC); 60 | Serial.print(", velocity="); 61 | Serial.println(data2, DEC); 62 | break; 63 | 64 | case usbMIDI.AfterTouchPoly: // 0xA0 65 | Serial.print("AfterTouch Change, ch="); 66 | Serial.print(channel, DEC); 67 | Serial.print(", note="); 68 | Serial.print(data1, DEC); 69 | Serial.print(", velocity="); 70 | Serial.println(data2, DEC); 71 | break; 72 | 73 | case usbMIDI.ControlChange: // 0xB0 74 | Serial.print("Control Change, ch="); 75 | Serial.print(channel, DEC); 76 | Serial.print(", control="); 77 | Serial.print(data1, DEC); 78 | Serial.print(", value="); 79 | Serial.println(data2, DEC); 80 | break; 81 | 82 | case usbMIDI.ProgramChange: // 0xC0 83 | Serial.print("Program Change, ch="); 84 | Serial.print(channel, DEC); 85 | Serial.print(", program="); 86 | Serial.println(data1, DEC); 87 | break; 88 | 89 | case usbMIDI.AfterTouchChannel: // 0xD0 90 | Serial.print("After Touch, ch="); 91 | Serial.print(channel, DEC); 92 | Serial.print(", pressure="); 93 | Serial.println(data1, DEC); 94 | break; 95 | 96 | case usbMIDI.PitchBend: // 0xE0 97 | Serial.print("Pitch Change, ch="); 98 | Serial.print(channel, DEC); 99 | Serial.print(", pitch="); 100 | Serial.println(data1 + data2 * 128, DEC); 101 | break; 102 | 103 | case usbMIDI.SystemExclusive: // 0xF0 104 | // Messages larger than usbMIDI's internal buffer are truncated. 105 | // To receive large messages, you *must* use the 3-input function 106 | // handler. See InputFunctionsComplete for details. 107 | Serial.print("SysEx Message: "); 108 | printBytes(usbMIDI.getSysExArray(), data1 + data2 * 256); 109 | Serial.println(); 110 | break; 111 | 112 | case usbMIDI.TimeCodeQuarterFrame: // 0xF1 113 | Serial.print("TimeCode, index="); 114 | Serial.print(data1 >> 4, DEC); 115 | Serial.print(", digit="); 116 | Serial.println(data1 & 15, DEC); 117 | break; 118 | 119 | case usbMIDI.SongPosition: // 0xF2 120 | Serial.print("Song Position, beat="); 121 | Serial.println(data1 + data2 * 128); 122 | break; 123 | 124 | case usbMIDI.SongSelect: // 0xF3 125 | Serial.print("Sond Select, song="); 126 | Serial.println(data1, DEC); 127 | break; 128 | 129 | case usbMIDI.TuneRequest: // 0xF6 130 | Serial.println("Tune Request"); 131 | break; 132 | 133 | case usbMIDI.Clock: // 0xF8 134 | Serial.println("Clock"); 135 | break; 136 | 137 | case usbMIDI.Start: // 0xFA 138 | Serial.println("Start"); 139 | break; 140 | 141 | case usbMIDI.Continue: // 0xFB 142 | Serial.println("Continue"); 143 | break; 144 | 145 | case usbMIDI.Stop: // 0xFC 146 | Serial.println("Stop"); 147 | break; 148 | 149 | case usbMIDI.ActiveSensing: // 0xFE 150 | Serial.println("Actvice Sensing"); 151 | break; 152 | 153 | case usbMIDI.SystemReset: // 0xFF 154 | Serial.println("System Reset"); 155 | break; 156 | 157 | default: 158 | Serial.println("Opps, an unknown MIDI message type!"); 159 | } 160 | } 161 | 162 | 163 | void printBytes(const byte *data, unsigned int size) { 164 | while (size > 0) { 165 | byte b = *data++; 166 | if (b < 16) Serial.print('0'); 167 | Serial.print(b, HEX); 168 | if (size > 1) Serial.print(' '); 169 | size = size - 1; 170 | } 171 | } 172 | 173 | -------------------------------------------------------------------------------- /USB_MIDI/Interface_3x3/Interface_3x3.ino: -------------------------------------------------------------------------------- 1 | /* Create a "class compliant " USB to 3 MIDI IN and 3 MIDI OUT interface. 2 | 3 | MIDI receive (6N138 optocoupler) input circuit and series resistor 4 | outputs need to be connected to Serial1, Serial2 and Serial3. 5 | 6 | You must select MIDIx4 from the "Tools > USB Type" menu 7 | 8 | This example code is in the public domain. 9 | */ 10 | 11 | #include 12 | 13 | // Create the Serial MIDI ports 14 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI1); 15 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI2); 16 | MIDI_CREATE_INSTANCE(HardwareSerial, Serial3, MIDI3); 17 | 18 | // A variable to know how long the LED has been turned on 19 | elapsedMillis ledOnMillis; 20 | 21 | 22 | void setup() { 23 | Serial.begin(115200); 24 | pinMode(13, OUTPUT); // LED pin 25 | digitalWrite(13, LOW); 26 | MIDI1.begin(MIDI_CHANNEL_OMNI); 27 | MIDI2.begin(MIDI_CHANNEL_OMNI); 28 | MIDI3.begin(MIDI_CHANNEL_OMNI); 29 | } 30 | 31 | 32 | void loop() { 33 | bool activity = false; 34 | 35 | if (MIDI1.read()) { 36 | // get a MIDI IN1 (Serial) message 37 | byte type = MIDI1.getType(); 38 | byte channel = MIDI1.getChannel(); 39 | byte data1 = MIDI1.getData1(); 40 | byte data2 = MIDI1.getData2(); 41 | 42 | // forward the message to USB MIDI virtual cable #0 43 | if (type != midi::SystemExclusive) { 44 | // Normal messages, simply give the data to the usbMIDI.send() 45 | usbMIDI.send(type, data1, data2, channel, 0); 46 | } else { 47 | // SysEx messages are special. The message length is given in data1 & data2 48 | unsigned int SysExLength = data1 + data2 * 256; 49 | usbMIDI.sendSysEx(SysExLength, MIDI1.getSysExArray(), true, 0); 50 | } 51 | activity = true; 52 | } 53 | 54 | if (MIDI2.read()) { 55 | // get a MIDI IN2 (Serial) message 56 | byte type = MIDI2.getType(); 57 | byte channel = MIDI2.getChannel(); 58 | byte data1 = MIDI2.getData1(); 59 | byte data2 = MIDI2.getData2(); 60 | 61 | // forward the message to USB MIDI virtual cable #1 62 | if (type != midi::SystemExclusive) { 63 | // Normal messages, simply give the data to the usbMIDI.send() 64 | usbMIDI.send(type, data1, data2, channel, 1); 65 | } else { 66 | // SysEx messages are special. The message length is given in data1 & data2 67 | unsigned int SysExLength = data1 + data2 * 256; 68 | usbMIDI.sendSysEx(SysExLength, MIDI1.getSysExArray(), true, 1); 69 | } 70 | activity = true; 71 | } 72 | 73 | if (MIDI3.read()) { 74 | // get a MIDI IN1 (Serial) message 75 | byte type = MIDI3.getType(); 76 | byte channel = MIDI3.getChannel(); 77 | byte data1 = MIDI3.getData1(); 78 | byte data2 = MIDI3.getData2(); 79 | 80 | // forward the message to USB MIDI virtual cable #0 81 | if (type != midi::SystemExclusive) { 82 | // Normal messages, simply give the data to the usbMIDI.send() 83 | usbMIDI.send(type, data1, data2, channel, 2); 84 | } else { 85 | // SysEx messages are special. The message length is given in data1 & data2 86 | unsigned int SysExLength = data1 + data2 * 256; 87 | usbMIDI.sendSysEx(SysExLength, MIDI1.getSysExArray(), true, 2); 88 | } 89 | activity = true; 90 | } 91 | 92 | if (usbMIDI.read()) { 93 | // get the USB MIDI message, defined by these 5 numbers (except SysEX) 94 | byte type = usbMIDI.getType(); 95 | byte channel = usbMIDI.getChannel(); 96 | byte data1 = usbMIDI.getData1(); 97 | byte data2 = usbMIDI.getData2(); 98 | byte cable = usbMIDI.getCable(); 99 | 100 | // forward this message to 1 of the 3 Serial MIDI OUT ports 101 | if (type != usbMIDI.SystemExclusive) { 102 | // Normal messages, first we must convert usbMIDI's type (an ordinary 103 | // byte) to the MIDI library's special MidiType. 104 | midi::MidiType mtype = (midi::MidiType)type; 105 | 106 | // Then simply give the data to the MIDI library send() 107 | switch (cable) { 108 | case 0: 109 | MIDI1.send(mtype, data1, data2, channel); 110 | break; 111 | case 1: 112 | MIDI2.send(mtype, data1, data2, channel); 113 | break; 114 | case 2: 115 | MIDI3.send(mtype, data1, data2, channel); 116 | break; 117 | } 118 | 119 | } else { 120 | // SysEx messages are special. The message length is given in data1 & data2 121 | unsigned int SysExLength = data1 + data2 * 256; 122 | switch (cable) { 123 | case 0: 124 | MIDI1.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); 125 | break; 126 | case 1: 127 | MIDI2.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); 128 | break; 129 | case 2: 130 | MIDI3.sendSysEx(SysExLength, usbMIDI.getSysExArray(), true); 131 | break; 132 | } 133 | } 134 | activity = true; 135 | } 136 | 137 | // blink the LED when any activity has happened 138 | if (activity) { 139 | digitalWriteFast(13, HIGH); // LED on 140 | ledOnMillis = 0; 141 | } 142 | if (ledOnMillis > 15) { 143 | digitalWriteFast(13, LOW); // LED off 144 | } 145 | 146 | } 147 | 148 | -------------------------------------------------------------------------------- /USB_MIDI/MIDI_name/MIDI_name.ino: -------------------------------------------------------------------------------- 1 | /* USB MIDI Custom Name Example 2 | * 3 | * This example demonstrates how to change the USB MIDI 4 | * device name on Teensy LC and 3.x. When creating more 5 | * that one MIDI device, custom names are much easier to 6 | * use when selecting each device in MIDI software on 7 | * your PC or Mac. The custom name is in the "name.c" tab. 8 | * 9 | * Windows and Macintosh systems often cache USB info. 10 | * After changing the name, you may need to test on a 11 | * different computer to observe the new name, or take 12 | * steps to get your operating system to "forget" the 13 | * cached info. (TODO: wanted... can anyone contribute 14 | * instructions for these systems) 15 | * 16 | * You must select MIDI from the "Tools > USB Type" menu 17 | * 18 | * This example code is in the public domain. 19 | */ 20 | 21 | void setup() { 22 | } 23 | 24 | void loop() { 25 | // Add your MIDI application here... 26 | 27 | // MIDI Controllers should discard incoming MIDI messages. 28 | // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash 29 | while (usbMIDI.read()) { 30 | // ignore incoming messages 31 | } 32 | } 33 | 34 | // To name the individual ports for MIDIx4 and MIDIx16 35 | // https://forum.pjrc.com/threads/50043?p=260134#post260134 36 | -------------------------------------------------------------------------------- /USB_MIDI/MIDI_name/name.c: -------------------------------------------------------------------------------- 1 | // To give your project a unique name, this code must be 2 | // placed into a .c file (its own tab). It can not be in 3 | // a .cpp file or your main sketch (the .ino file). 4 | 5 | #include "usb_names.h" 6 | 7 | // Edit these lines to create your own name. The length must 8 | // match the number of characters in your custom name. 9 | 10 | #define MIDI_NAME {'M','y',' ','M','I','D','I'} 11 | #define MIDI_NAME_LEN 7 12 | 13 | // Do not change this part. This exact format is required by USB. 14 | 15 | struct usb_string_descriptor_struct usb_string_product_name = { 16 | 2 + MIDI_NAME_LEN * 2, 17 | 3, 18 | MIDI_NAME 19 | }; 20 | -------------------------------------------------------------------------------- /USB_MIDI/Many_Button_Knobs/Many_Button_Knobs.ino: -------------------------------------------------------------------------------- 1 | /* Use arrays to manage lists of knobs/pots and pushbuttons. 2 | 3 | By Leif Oddson 4 | https://forum.pjrc.com/threads/45376 5 | 6 | This more complex example demonstrates how to use arrays to 7 | manage a larger number of inputs, without duplicating your 8 | code for every signal. 9 | 10 | You must select MIDI from the "Tools > USB Type" menu 11 | 12 | This example code is in the public domain. 13 | */ 14 | 15 | 16 | //************LIBRARIES USED************** 17 | // include the ResponsiveAnalogRead library for analog smoothing 18 | #include 19 | // include the Bounce library for 'de-bouncing' switches -- removing electrical chatter as contacts settle 20 | #include 21 | //usbMIDI.h library is added automatically when code is compiled as a MIDI device 22 | 23 | // ******CONSTANT VALUES******** 24 | // customize code behaviour here! 25 | const int channel = 1; // MIDI channel 26 | const int A_PINS = 6; // number of Analog PINS 27 | const int D_PINS = 3; // number of Digital PINS 28 | const int ON_VELOCITY = 99; // note-one velocity sent from buttons (should be 65 to 127) 29 | 30 | // define the pins you want to use and the CC ID numbers on which to send them.. 31 | const int ANALOG_PINS[A_PINS] = {A0,A1,A2,A3,A4,A5}; 32 | const int CCID[A_PINS] = {21,22,23,24,25,26}; 33 | 34 | // define the pins and notes for digital events 35 | const int DIGITAL_PINS[D_PINS] = {0,1,2}; 36 | const int note[D_PINS] = {60,61,62}; 37 | const int BOUNCE_TIME = 7; // 5 ms is usually sufficient 38 | const boolean toggled = true; 39 | 40 | 41 | //******VARIABLES*********** 42 | // a data array and a lagged copy to tell when MIDI changes are required 43 | byte data[A_PINS]; 44 | byte dataLag[A_PINS]; // when lag and new are not the same then update MIDI CC value 45 | 46 | 47 | //************INITIALIZE LIBRARY OBJECTS************** 48 | // not sure if there is a better way... some way run a setup loop on global array?? 49 | // use comment tags to comment out unused portions of array definitions 50 | 51 | // initialize the ReponsiveAnalogRead objects 52 | ResponsiveAnalogRead analog[]{ 53 | {ANALOG_PINS[0],true}, 54 | {ANALOG_PINS[1],true}, 55 | {ANALOG_PINS[2],true}, 56 | {ANALOG_PINS[3],true}, 57 | {ANALOG_PINS[4],true}, 58 | {ANALOG_PINS[5],true}/*, 59 | {ANALOG_PINS[6],true}, 60 | {ANALOG_PINS[7],true},*/ 61 | }; 62 | 63 | // initialize the bounce objects 64 | Bounce digital[] = { 65 | Bounce(DIGITAL_PINS[0],BOUNCE_TIME), 66 | Bounce(DIGITAL_PINS[1], BOUNCE_TIME), 67 | Bounce(DIGITAL_PINS[2], BOUNCE_TIME)/*, 68 | Bounce(DIGITAL_PINS[3], BOUNCE_TIME), 69 | Bounce(DIGITAL_PINS[4], BOUNCE_TIME), 70 | Bounce(DIGITAL_PINS[5], BOUNCE_TIME), 71 | Bounce(DIGITAL_PINS[6], BOUNCE_TIME), 72 | Bounce(DIGITAL_PINS[7], BOUNCE_TIME),*/ 73 | }; 74 | 75 | //************SETUP************** 76 | void setup() { 77 | // loop to configure input pins and internal pullup resisters for digital section 78 | for (int i=0;i>3; 101 | if (data[i] != dataLag[i]){ 102 | dataLag[i] = data[i]; 103 | usbMIDI.sendControlChange(CCID[i], data[i], channel); 104 | } 105 | } 106 | } 107 | } 108 | 109 | 110 | 111 | //************DIGITAL SECTION************** 112 | void getDigitalData(){ 113 | for (int i=0;i USB Type" menu 24 | 25 | This example code is in the public domain. 26 | */ 27 | 28 | const int channel = 10; // General MIDI: channel 10 = percussion sounds 29 | const int note = 38; // General MIDI: note 38 = acoustic snare 30 | 31 | const int analogPin = A0; 32 | const int thresholdMin = 60; // minimum reading, avoid noise and false starts 33 | const int peakTrackMillis = 12; 34 | const int aftershockMillis = 25; // aftershocks & vibration reject 35 | 36 | 37 | void setup() { 38 | Serial.begin(115200); 39 | while (!Serial && millis() < 2500) /* wait for serial monitor */ ; 40 | Serial.println("Piezo Peak Capture"); 41 | } 42 | 43 | 44 | void loop() { 45 | int piezo = analogRead(analogPin); 46 | peakDetect(piezo); 47 | // Add other tasks to loop, but avoid using delay() or waiting. 48 | // You need loop() to keep running rapidly to detect Piezo peaks! 49 | 50 | // MIDI Controllers should discard incoming MIDI messages. 51 | // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash 52 | while (usbMIDI.read()) { 53 | // ignore incoming messages 54 | } 55 | } 56 | 57 | 58 | void peakDetect(int voltage) { 59 | // "static" variables keep their numbers between each run of this function 60 | static int state; // 0=idle, 1=looking for peak, 2=ignore aftershocks 61 | static int peak; // remember the highest reading 62 | static elapsedMillis msec; // timer to end states 1 and 2 63 | 64 | switch (state) { 65 | // IDLE state: wait for any reading is above threshold. Do not set 66 | // the threshold too low. You don't want to be too sensitive to slight 67 | // vibration. 68 | case 0: 69 | if (voltage > thresholdMin) { 70 | //Serial.print("begin peak track "); 71 | //Serial.println(voltage); 72 | peak = voltage; 73 | msec = 0; 74 | state = 1; 75 | } 76 | return; 77 | 78 | // Peak Tracking state: capture largest reading 79 | case 1: 80 | if (voltage > peak) { 81 | peak = voltage; 82 | } 83 | if (msec >= peakTrackMillis) { 84 | //Serial.print("peak = "); 85 | //Serial.println(peak); 86 | int velocity = map(peak, thresholdMin, 1023, 1, 127); 87 | usbMIDI.sendNoteOn(note, velocity, channel); 88 | msec = 0; 89 | state = 2; 90 | } 91 | return; 92 | 93 | // Ignore Aftershock state: wait for things to be quiet again. 94 | default: 95 | if (voltage > thresholdMin) { 96 | msec = 0; // keep resetting timer if above threshold 97 | } else if (msec > aftershockMillis) { 98 | usbMIDI.sendNoteOff(note, 0, channel); 99 | state = 0; // go back to idle when 100 | } 101 | } 102 | } 103 | 104 | -------------------------------------------------------------------------------- /USB_MIDI/TransmitEverything/TransmitEverything.ino: -------------------------------------------------------------------------------- 1 | /* Transmit every possible USB MIDI message. Each time a 2 | button is pressed, send another message. 3 | 4 | This very long example demonstrates all possible usbMIDI 5 | message send functions. It's mostly meant for testing 6 | and as a reference to easily copy-and-paste the code for 7 | every message send function. 8 | 9 | A pushbutton (ordinary momentary type) needs to be 10 | connected between pin 0 and GND. 11 | 12 | You must select MIDI from the "Tools > USB Type" menu 13 | 14 | This example code is in the public domain. 15 | */ 16 | 17 | #include 18 | 19 | // the MIDI channel number to send messages 20 | const int channel = 1; 21 | 22 | // the MIDI virtual cable to use 23 | int cable = 0; 24 | 25 | // Create a Bounce object for the button. 26 | const int pin = 0; 27 | Bounce button1 = Bounce(pin, 10); 28 | 29 | // remember when a note-on message has been sent 30 | int note=0; 31 | 32 | // which message will we do next 33 | int state=0; 34 | 35 | // sysex message to send 36 | uint8_t buf[] = {0xF0, 6, 24, 64, 5, 1, 0xF7}; 37 | 38 | void setup() { 39 | pinMode(pin, INPUT_PULLUP); 40 | } 41 | 42 | void loop() { 43 | button1.update(); 44 | if (button1.fallingEdge()) { 45 | // when the button is pressed, send another message 46 | sendNextMessage(); 47 | } 48 | if (button1.risingEdge()) { 49 | // when the button is release, send note off if we left a note on 50 | if (note > 0) { 51 | usbMIDI.sendNoteOff(note, 0, channel, cable); 52 | note = 0; 53 | } 54 | } 55 | 56 | // MIDI Controllers should discard incoming MIDI messages. 57 | // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash 58 | while (usbMIDI.read()) { 59 | // ignore incoming messages 60 | } 61 | } 62 | 63 | void sendNextMessage() { 64 | switch (state) { 65 | case 0: 66 | note = analogRead(A0) / 8; 67 | if (note == 0) note = 1; 68 | usbMIDI.sendNoteOn(note, 99, channel, cable); 69 | break; 70 | case 1: 71 | usbMIDI.sendAfterTouchPoly(65, 110, channel, cable); 72 | break; 73 | case 2: 74 | usbMIDI.sendControlChange(7, 100, channel, cable); 75 | break; 76 | case 3: 77 | usbMIDI.sendProgramChange(2, channel, cable); 78 | break; 79 | case 4: 80 | usbMIDI.sendAfterTouch(108, channel, cable); 81 | break; 82 | case 5: 83 | usbMIDI.sendPitchBend(911, channel, cable); 84 | break; 85 | case 6: 86 | usbMIDI.sendSysEx(sizeof(buf), buf, true, cable); 87 | break; 88 | case 7: 89 | usbMIDI.sendSysEx(sizeof(buf)-2, buf+1, false, cable); 90 | break; 91 | case 8: 92 | usbMIDI.sendRealTime(usbMIDI.Clock, cable); 93 | break; 94 | case 9: 95 | usbMIDI.sendRealTime(usbMIDI.Start, cable); 96 | break; 97 | case 10: 98 | usbMIDI.sendRealTime(usbMIDI.Continue, cable); 99 | break; 100 | case 11: 101 | usbMIDI.sendRealTime(usbMIDI.Stop, cable); 102 | break; 103 | case 12: 104 | usbMIDI.sendRealTime(usbMIDI.ActiveSensing, cable); 105 | break; 106 | case 13: 107 | usbMIDI.sendRealTime(usbMIDI.SystemReset, cable); 108 | break; 109 | case 14: 110 | usbMIDI.sendTimeCodeQuarterFrame(0, 3, cable); 111 | usbMIDI.sendTimeCodeQuarterFrame(1, 1, cable); 112 | usbMIDI.sendTimeCodeQuarterFrame(2, 7, cable); 113 | usbMIDI.sendTimeCodeQuarterFrame(3, 4, cable); 114 | usbMIDI.sendTimeCodeQuarterFrame(4, 1, cable); 115 | usbMIDI.sendTimeCodeQuarterFrame(5, 1, cable); 116 | usbMIDI.sendTimeCodeQuarterFrame(6, 0, cable); 117 | usbMIDI.sendTimeCodeQuarterFrame(7, 0, cable); 118 | break; 119 | case 15: 120 | usbMIDI.sendTuneRequest(cable); 121 | break; 122 | case 16: 123 | usbMIDI.sendSongPosition(2531, cable); 124 | break; 125 | case 17: 126 | usbMIDI.sendSongSelect(107, cable); 127 | break; 128 | case 18: 129 | usbMIDI.beginRpn(1, channel, cable); 130 | usbMIDI.sendRpnValue(6489, channel, cable); 131 | usbMIDI.endRpn(channel, cable); 132 | break; 133 | case 19: 134 | usbMIDI.beginRpn(1, channel, cable); 135 | usbMIDI.sendRpnIncrement(14, channel, cable); 136 | usbMIDI.endRpn(channel, cable); 137 | break; 138 | case 20: 139 | usbMIDI.beginRpn(1, channel, cable); 140 | usbMIDI.sendRpnDecrement(9, channel, cable); 141 | usbMIDI.endRpn(channel, cable); 142 | break; 143 | case 21: 144 | usbMIDI.beginNrpn(417, channel, cable); 145 | usbMIDI.sendRpnValue(6489, channel, cable); 146 | usbMIDI.endRpn(channel, cable); 147 | break; 148 | case 22: 149 | usbMIDI.beginNrpn(417, channel, cable); 150 | usbMIDI.sendRpnIncrement(3, channel, cable); 151 | usbMIDI.endRpn(channel, cable); 152 | break; 153 | case 23: 154 | usbMIDI.beginNrpn(417, channel, cable); 155 | usbMIDI.sendRpnDecrement(2, channel, cable); 156 | usbMIDI.endRpn(channel, cable); 157 | break; 158 | } 159 | state = state + 1; 160 | if (state > 23) state = 0; 161 | } 162 | 163 | -------------------------------------------------------------------------------- /USB_Mouse/Buttons/Buttons.ino: -------------------------------------------------------------------------------- 1 | /* Buttons to USB Mouse Example 2 | 3 | You must select Mouse from the "Tools > USB Type" menu 4 | 5 | This example code is in the public domain. 6 | */ 7 | 8 | #include 9 | 10 | const int moveDistance = 5; // how much to move the mouse on each button press 11 | 12 | // Create Bounce objects for each button. The Bounce object 13 | // automatically deals with contact chatter or "bounce", and 14 | // it makes detecting changes very simple. 15 | 16 | // Five buttons to control the 5 mouse clicks 17 | Bounce button2 = Bounce(2, 10); 18 | Bounce button3 = Bounce(3, 10); // if a button is too "sensitive" 19 | Bounce button4 = Bounce(4, 10); // to rapid touch, you can 20 | Bounce button5 = Bounce(5, 10); // increase this time. 21 | Bounce button6 = Bounce(6, 10); 22 | 23 | // Four more buttons to move the mouse 24 | Bounce button7 = Bounce(7, 10); 25 | Bounce button8 = Bounce(8, 10); 26 | Bounce button9 = Bounce(9, 10); 27 | Bounce button10 = Bounce(10, 10); 28 | 29 | // And still more buttons for the scroll wheel 30 | Bounce button14 = Bounce(14, 10); 31 | Bounce button15 = Bounce(15, 10); 32 | Bounce button16 = Bounce(16, 10); 33 | Bounce button17 = Bounce(17, 10); 34 | 35 | // Even more buttons to jump the mouse to a fixed coordinate 36 | Bounce button21 = Bounce(21, 10); 37 | Bounce button22 = Bounce(22, 10); 38 | Bounce button23 = Bounce(23, 10); 39 | 40 | void setup() { 41 | // Configure the pins for input mode with pullup resistors. 42 | // The pushbuttons connect from each pin to ground. When 43 | // the button is pressed, the pin reads LOW because the button 44 | // shorts it to ground. When released, the pin reads HIGH 45 | // because the pullup resistor connects to +5 volts inside 46 | // the chip. LOW for "on", and HIGH for "off" may seem 47 | // backwards, but using the on-chip pullup resistors is very 48 | // convenient. The scheme is called "active low", and it's 49 | // very commonly used in electronics... so much that the chip 50 | // has built-in pullup resistors! 51 | pinMode(2, INPUT_PULLUP); 52 | pinMode(3, INPUT_PULLUP); 53 | pinMode(4, INPUT_PULLUP); 54 | pinMode(5, INPUT_PULLUP); 55 | pinMode(6, INPUT_PULLUP); 56 | pinMode(7, INPUT_PULLUP); 57 | pinMode(8, INPUT_PULLUP); 58 | pinMode(9, INPUT_PULLUP); 59 | pinMode(10, INPUT_PULLUP); 60 | pinMode(14, INPUT_PULLUP); 61 | pinMode(15, INPUT_PULLUP); 62 | pinMode(16, INPUT_PULLUP); 63 | pinMode(17, INPUT_PULLUP); 64 | pinMode(21, INPUT_PULLUP); 65 | pinMode(22, INPUT_PULLUP); 66 | pinMode(23, INPUT_PULLUP); 67 | 68 | // If your screen is a different size, edit this to set the size. 69 | // Even if the size does not match, mouse.moveTo(x, y) will still 70 | // work, but the results will be scaled as if the x,y coordinate 71 | // was on this screen. Setting the correct screen size allows you 72 | // to use the actual pixel coordinates of your screen. 73 | Mouse.screenSize(1920, 1080); 74 | // screenSize() is not supported on Teensy 2.0 & Teensy++ 2.0. 75 | // Delete this line to run on 8 bit Teensy boards. 76 | } 77 | 78 | 79 | void loop() { 80 | // Update all the buttons. There should not be any long 81 | // delays in loop(), so this runs repetitively at a rate 82 | // faster than the buttons could be pressed and released. 83 | button2.update(); 84 | button3.update(); 85 | button4.update(); 86 | button5.update(); 87 | button6.update(); 88 | button7.update(); 89 | button8.update(); 90 | button9.update(); 91 | button10.update(); 92 | button14.update(); 93 | button15.update(); 94 | button16.update(); 95 | button17.update(); 96 | button21.update(); 97 | button22.update(); 98 | button23.update(); 99 | 100 | // Check each input for "falling" edge. Normally the pin is high 101 | // due to the INPUT_PULLUP. It goes "falling" to low when your 102 | // connect connects the pin to GND. 103 | 104 | if (button7.fallingEdge()) { 105 | Mouse.move(-moveDistance, 0); // move Left 106 | } 107 | if (button8.fallingEdge()) { 108 | Mouse.move(moveDistance, 0); // move Right 109 | } 110 | if (button9.fallingEdge()) { 111 | Mouse.move(0, -moveDistance); // move Up 112 | } 113 | if (button10.fallingEdge()) { 114 | Mouse.move(0, moveDistance); // move Down 115 | } 116 | 117 | if (button14.fallingEdge()) { 118 | Mouse.scroll(1); // scroll wheel Up 119 | } 120 | if (button15.fallingEdge()) { 121 | Mouse.scroll(-1); // scroll wheel Down 122 | } 123 | if (button16.fallingEdge()) { 124 | Mouse.scroll(0, 1); // scroll horizontal right 125 | } 126 | if (button17.fallingEdge()) { 127 | Mouse.scroll(0, -1); // scroll horizontal left 128 | } 129 | 130 | if (button21.fallingEdge()) { 131 | Mouse.moveTo(10, 10); // move near upper left corner (Apple menu) 132 | // moveTo() is not supported on Teensy 2.0 & Teensy++ 2.0. 133 | // Delete this line to run on 8 bit Teensy boards. 134 | } 135 | if (button22.fallingEdge()) { 136 | Mouse.moveTo(960, 540); // move to the screen center 137 | } 138 | if (button23.fallingEdge()) { 139 | Mouse.moveTo(24, 1066); // move near lower left corner (Windows Start menu) 140 | } 141 | 142 | // For the mouse buttons, we must detect both the falling and rising edges, 143 | // to press the mouse button when the button on our digital pin is pressed, 144 | // and to later release it when the physical button releases (the pin rises 145 | // from low back to high, thanks to INPUT_PULLUP). 146 | 147 | if (button2.fallingEdge()) { 148 | Mouse.press(MOUSE_LEFT); 149 | } 150 | if (button2.risingEdge()) { 151 | Mouse.release(MOUSE_LEFT); 152 | } 153 | if (button3.fallingEdge()) { 154 | Mouse.press(MOUSE_MIDDLE); 155 | } 156 | if (button3.risingEdge()) { 157 | Mouse.release(MOUSE_MIDDLE); 158 | } 159 | if (button4.fallingEdge()) { 160 | Mouse.press(MOUSE_RIGHT); 161 | } 162 | if (button4.risingEdge()) { 163 | Mouse.release(MOUSE_RIGHT); 164 | } 165 | if (button5.fallingEdge()) { 166 | Mouse.press(MOUSE_BACK); 167 | } 168 | if (button5.risingEdge()) { 169 | Mouse.release(MOUSE_BACK); 170 | } 171 | if (button6.fallingEdge()) { 172 | Mouse.press(MOUSE_FORWARD); 173 | } 174 | if (button6.risingEdge()) { 175 | Mouse.release(MOUSE_FORWARD); 176 | } 177 | 178 | 179 | 180 | 181 | 182 | } 183 | 184 | -------------------------------------------------------------------------------- /USB_Mouse/TriangleMove/TriangleMove.ino: -------------------------------------------------------------------------------- 1 | /* Simple USB Mouse Example 2 | Teensy becomes a USB mouse and moves the cursor in a triangle 3 | 4 | You must select Mouse from the "Tools > USB Type" menu 5 | 6 | This example code is in the public domain. 7 | */ 8 | 9 | void setup() { } // no setup needed 10 | void loop() { 11 | int i; 12 | for (i=0; i<40; i++) { 13 | Mouse.move(2, -1); 14 | delay(25); 15 | } 16 | for (i=0; i<40; i++) { 17 | Mouse.move(2, 2); 18 | delay(25); 19 | } 20 | for (i=0; i<40; i++) { 21 | Mouse.move(-4, -1); 22 | delay(25); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /USB_RawHID/Basic/Basic.ino: -------------------------------------------------------------------------------- 1 | /* Basic Raw HID Example 2 | Teensy can send/receive 64 byte packets with a 3 | dedicated program running on a PC or Mac. 4 | 5 | You must select Raw HID from the "Tools > USB Type" menu 6 | 7 | Optional: LEDs should be connected to pins 0-7, 8 | and analog signals to the analog inputs. 9 | 10 | This example code is in the public domain. 11 | */ 12 | 13 | 14 | void setup() { 15 | Serial.begin(9600); 16 | Serial.println(F("RawHID Example")); 17 | for (int i=0; i<7; i++) { 18 | pinMode(i, OUTPUT); 19 | } 20 | } 21 | 22 | // RawHID packets are always 64 bytes 23 | byte buffer[64]; 24 | elapsedMillis msUntilNextSend; 25 | unsigned int packetCount = 0; 26 | 27 | void loop() { 28 | int n; 29 | n = RawHID.recv(buffer, 0); // 0 timeout = do not wait 30 | if (n > 0) { 31 | // the computer sent a message. Display the bits 32 | // of the first byte on pin 0 to 7. Ignore the 33 | // other 63 bytes! 34 | Serial.print(F("Received packet, first byte: ")); 35 | Serial.println((int)buffer[0]); 36 | for (int i=0; i<8; i++) { 37 | int b = buffer[0] & (1 << i); 38 | digitalWrite(i, b); 39 | } 40 | } 41 | // every 2 seconds, send a packet to the computer 42 | if (msUntilNextSend > 2000) { 43 | msUntilNextSend = msUntilNextSend - 2000; 44 | // first 2 bytes are a signature 45 | buffer[0] = 0xAB; 46 | buffer[1] = 0xCD; 47 | // next 24 bytes are analog measurements 48 | for (int i=0; i<12; i++) { 49 | int val = analogRead(i); 50 | buffer[i * 2 + 2] = highByte(val); 51 | buffer[i * 2 + 3] = lowByte(val); 52 | } 53 | // fill the rest with zeros 54 | for (int i=26; i<62; i++) { 55 | buffer[i] = 0; 56 | } 57 | // and put a count of packets sent at the end 58 | buffer[62] = highByte(packetCount); 59 | buffer[63] = lowByte(packetCount); 60 | // actually send the packet 61 | n = RawHID.send(buffer, 100); 62 | if (n > 0) { 63 | Serial.print(F("Transmit packet ")); 64 | Serial.println(packetCount); 65 | packetCount = packetCount + 1; 66 | } else { 67 | Serial.println(F("Unable to transmit packet")); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /USB_Serial/HelloWorld/HelloWorld.ino: -------------------------------------------------------------------------------- 1 | /* Simple "Hello World" example. 2 | 3 | After uploading this to your board, use Serial Monitor 4 | to view the message. When Serial is selected from the 5 | Tools > USB Type menu, the correct serial port must be 6 | selected from the Tools > Serial Port AFTER Teensy is 7 | running this code. Teensy only becomes a serial device 8 | while this code is running! For non-Serial types, 9 | the Serial port is emulated, so no port needs to be 10 | selected. 11 | 12 | This example code is in the public domain. 13 | */ 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); // USB is always 12 Mbit/sec 18 | } 19 | 20 | void loop() 21 | { 22 | Serial.println("Hello World..."); 23 | delay(1000); // do not print too fast! 24 | } 25 | 26 | -------------------------------------------------------------------------------- /USB_Serial/Triple_Serial_Test/Triple_Serial_Test.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | void setup() { 4 | Serial1.begin(115200); 5 | delay(800); 6 | send_to_all("Triple Serial Test", 0); 7 | } 8 | 9 | void loop() { 10 | if (Serial.available() > 0) { 11 | send_to_all("Main Serial", Serial.read()); 12 | } 13 | if (Serial1.available() > 0) { 14 | send_to_all("Hardware Serial", Serial1.read()); 15 | } 16 | #if defined(USB_DUAL_SERIAL) || defined(USB_TRIPLE_SERIAL) 17 | if (SerialUSB1.available() > 0) { 18 | send_to_all("Serial USB1", SerialUSB1.read()); 19 | } 20 | #endif 21 | #if defined(USB_TRIPLE_SERIAL) 22 | if (SerialUSB2.available() > 0) { 23 | send_to_all("Serial USB2", SerialUSB2.read()); 24 | } 25 | #endif 26 | } 27 | 28 | void send_to_all(const char *name, int c) 29 | { 30 | char buf[128]; 31 | 32 | if (c) { 33 | snprintf(buf, sizeof(buf), "%s received: %u\r\n", name, c); 34 | } else { 35 | snprintf(buf, sizeof(buf), "%s\r\n", name); 36 | } 37 | Serial.print(buf); 38 | Serial1.print(buf); 39 | #ifdef USB_DUAL_SERIAL 40 | SerialUSB1.print(buf); 41 | #endif 42 | #ifdef USB_TRIPLE_SERIAL 43 | SerialUSB1.print(buf); 44 | SerialUSB2.print(buf); 45 | #endif 46 | 47 | } 48 | -------------------------------------------------------------------------------- /USB_Serial/USBtoSerial/USBtoSerial.ino: -------------------------------------------------------------------------------- 1 | /* USB to Serial - Teensy becomes a USB to Serial converter 2 | http://dorkbotpdx.org/blog/paul/teensy_as_benito_at_57600_baud 3 | 4 | You must select Serial from the "Tools > USB Type" menu 5 | 6 | This example code is in the public domain. 7 | */ 8 | 9 | // set this to the hardware serial port you wish to use 10 | #define HWSERIAL Serial1 11 | 12 | unsigned long baud = 19200; 13 | const int reset_pin = 4; 14 | const int led_pin = 13; // 13 = Teensy 3.X & LC 15 | // 11 = Teensy 2.0 16 | // 6 = Teensy++ 2.0 17 | void setup() 18 | { 19 | pinMode(led_pin, OUTPUT); 20 | digitalWrite(led_pin, LOW); 21 | digitalWrite(reset_pin, HIGH); 22 | pinMode(reset_pin, OUTPUT); 23 | Serial.begin(baud); // USB, communication to PC or Mac 24 | HWSERIAL.begin(baud); // communication to hardware serial 25 | } 26 | 27 | long led_on_time=0; 28 | byte buffer[80]; 29 | unsigned char prev_dtr = 0; 30 | 31 | void loop() 32 | { 33 | unsigned char dtr; 34 | int rd, wr, n; 35 | 36 | // check if any data has arrived on the USB virtual serial port 37 | rd = Serial.available(); 38 | if (rd > 0) { 39 | // check if the hardware serial port is ready to transmit 40 | wr = HWSERIAL.availableForWrite(); 41 | if (wr > 0) { 42 | // compute how much data to move, the smallest 43 | // of rd, wr and the buffer size 44 | if (rd > wr) rd = wr; 45 | if (rd > 80) rd = 80; 46 | // read data from the USB port 47 | n = Serial.readBytes((char *)buffer, rd); 48 | // write it to the hardware serial port 49 | HWSERIAL.write(buffer, n); 50 | // turn on the LED to indicate activity 51 | digitalWrite(led_pin, HIGH); 52 | led_on_time = millis(); 53 | } 54 | } 55 | 56 | // check if any data has arrived on the hardware serial port 57 | rd = HWSERIAL.available(); 58 | if (rd > 0) { 59 | // check if the USB virtual serial port is ready to transmit 60 | wr = Serial.availableForWrite(); 61 | if (wr > 0) { 62 | // compute how much data to move, the smallest 63 | // of rd, wr and the buffer size 64 | if (rd > wr) rd = wr; 65 | if (rd > 80) rd = 80; 66 | // read data from the hardware serial port 67 | n = HWSERIAL.readBytes((char *)buffer, rd); 68 | // write it to the USB port 69 | Serial.write(buffer, n); 70 | // turn on the LED to indicate activity 71 | digitalWrite(led_pin, HIGH); 72 | led_on_time = millis(); 73 | } 74 | } 75 | 76 | // check if the USB virtual serial port has raised DTR 77 | dtr = Serial.dtr(); 78 | if (dtr && !prev_dtr) { 79 | digitalWrite(reset_pin, LOW); 80 | delayMicroseconds(250); 81 | digitalWrite(reset_pin, HIGH); 82 | } 83 | prev_dtr = dtr; 84 | 85 | // if the LED has been left on without more activity, turn it off 86 | if (millis() - led_on_time > 3) { 87 | digitalWrite(led_pin, LOW); 88 | } 89 | 90 | // check if the USB virtual serial wants a new baud rate 91 | if (Serial.baud() != baud) { 92 | baud = Serial.baud(); 93 | if (baud == 57600) { 94 | // This ugly hack is necessary for talking 95 | // to the arduino bootloader, which actually 96 | // communicates at 58824 baud (+2.1% error). 97 | // Teensyduino will configure the UART for 98 | // the closest baud rate, which is 57143 99 | // baud (-0.8% error). Serial communication 100 | // can tolerate about 2.5% error, so the 101 | // combined error is too large. Simply 102 | // setting the baud rate to the same as 103 | // arduino's actual baud rate works. 104 | HWSERIAL.begin(58824); 105 | } else { 106 | HWSERIAL.begin(baud); 107 | } 108 | } 109 | } 110 | 111 | -------------------------------------------------------------------------------- /USB_Touchscreen/SingleFingerLine/SingleFingerLine.ino: -------------------------------------------------------------------------------- 1 | /* USB Touchscreen, Single Finger Line Example 2 | 3 | When a button is pressed, Teensy sends a sequence of 4 | touchscreen presses which draw diagonal line with a 5 | slight downward slope. 6 | 7 | A pushbutton needs to be connected between pin 14 and GND. 8 | Or a wire or paperclip may be touched between GND and pin 9 | 14 to start the drawing. 10 | 11 | To see the result, open this page with Google Chrome: 12 | http://www.repaa.net/draw.html 13 | 14 | Alternate test site if repaa.net is down: 15 | https://naqtn.github.io/WBBMTT/ 16 | (Thanks to PaulS on the forum for this tip) 17 | https://forum.pjrc.com/threads/71464?p=315652&viewfull=1#post315652 18 | 19 | After pressing the button 14 times, you should see this: 20 | https://forum.pjrc.com/attachment.php?attachmentid=12748&d=1516976598 21 | 22 | You must select Touch Screen in the "Tools > USB Type" menu 23 | 24 | This example code is in the public domain. 25 | */ 26 | 27 | #include 28 | 29 | Bounce mybutton = Bounce(14, 10); 30 | int yoffset = 4000; 31 | 32 | void setup() { 33 | pinMode(14, INPUT_PULLUP); 34 | TouchscreenUSB.begin(); 35 | } 36 | 37 | void drawline(int x, int y) { 38 | for (int i=0; i < 6000; i += 100) { 39 | TouchscreenUSB.press(0, x + i, y + i/13); 40 | delay(10); 41 | } 42 | TouchscreenUSB.release(0); 43 | } 44 | 45 | void loop() { 46 | mybutton.update(); 47 | if (mybutton.fallingEdge()) { 48 | Serial.println("press"); 49 | drawline(16000, yoffset); 50 | yoffset += 1200; 51 | if (yoffset > 24000) yoffset = 4000; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /USB_Touchscreen/TenFingerCircle/TenFingerCircle.ino: -------------------------------------------------------------------------------- 1 | /* USB Touchscreen, Ten Finger Circle Example 2 | 3 | When a button is pressed, Teensy sends a sequence of 4 | touchscreen presses for 10 fingers simultaneously 5 | drawing ten 36 degree arcs, which form a circle. 6 | 7 | This example is meant to test whether your computer 8 | can properly handle multi-touch with 10 simultaneous 9 | points. If you encounter problems, please run the 10 | simpler SingleFingerLine example. 11 | 12 | A pushbutton needs to be connected between pin 14 and GND. 13 | Or a wire or paperclip may be touched between GND and pin 14 | 14 to start the drawing. 15 | 16 | To see the result, open this page with Google Chrome: 17 | http://www.repaa.net/draw.html 18 | 19 | Alternate test site if repaa.net is down: 20 | https://naqtn.github.io/WBBMTT/ 21 | (Thanks to PaulS on the forum for this tip) 22 | https://forum.pjrc.com/threads/71464?p=315652&viewfull=1#post315652 23 | 24 | After pressing the button 5 times, you should see this: 25 | https://forum.pjrc.com/attachment.php?attachmentid=12750&d=1516978091 26 | 27 | This example has been tested on Windows 10 and Ubuntu 14.04. 28 | Macintosh High Sierra 10.13.2 does not work, and also does not 29 | respond properly to a Dell P2314T touch-enabled monitor. 30 | 31 | You must select Touch Screen in the "Tools > USB Type" menu 32 | 33 | This example code is in the public domain. 34 | */ 35 | 36 | #include 37 | 38 | Bounce mybutton = Bounce(14, 10); 39 | int yoffset = 8000; 40 | 41 | void setup() { 42 | pinMode(14, INPUT_PULLUP); 43 | TouchscreenUSB.begin(); 44 | } 45 | 46 | void drawcircle(int x, int y) { 47 | float arc = 2.0 * PI / 10.0; 48 | float r = 3000.0; 49 | for (float angle=0; angle < arc; angle += 0.01) { 50 | for (int i=0; i < 10; i++) { 51 | float ph = arc * (float)i; 52 | TouchscreenUSB.press(i, r * cosf(angle+ph) + x, r * sinf(angle+ph) + y); 53 | } 54 | delay(10); 55 | } 56 | for (int i=0; i < 10; i++) { 57 | TouchscreenUSB.release(i); 58 | } 59 | } 60 | 61 | void loop() { 62 | mybutton.update(); 63 | if (mybutton.fallingEdge()) { 64 | Serial.println("press"); 65 | drawcircle(16000, yoffset); 66 | yoffset += 4200; 67 | if (yoffset > 24000) yoffset = 8000; 68 | } 69 | } 70 | 71 | 72 | --------------------------------------------------------------------------------