From 19dc97ac8998230f5f0bc645a0e877f438dea7fd Mon Sep 17 00:00:00 2001 From: Nico Date: Sat, 22 Nov 2014 14:02:34 +0100 Subject: [PATCH] Added Gamepad Project, PWM Fade example --- .../HoodLoader2_PWM_Fade.ino | 39 +++++++ .../Gamepad_Project/Gamepad_Project.ino | 110 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 examples/HoodLoader2/HoodLoader2_PWM_Fade/HoodLoader2_PWM_Fade.ino create mode 100644 examples/Projects/Gamepad_Project/Gamepad_Project.ino diff --git a/examples/HoodLoader2/HoodLoader2_PWM_Fade/HoodLoader2_PWM_Fade.ino b/examples/HoodLoader2/HoodLoader2_PWM_Fade/HoodLoader2_PWM_Fade.ino new file mode 100644 index 0000000..0ba22c8 --- /dev/null +++ b/examples/HoodLoader2/HoodLoader2_PWM_Fade/HoodLoader2_PWM_Fade.ino @@ -0,0 +1,39 @@ +/* + Copyright (c) 2014 NicoHood + See the readme for credit to other people. + + PWM Fade + + This example shows how to fade an LED on pin 7 + using the analogWrite() function. + + Basically it is a demonstartion that PWM on pin 7 works fine. + You can also deactivate the USB Core for this example, but then you'd + need the workaround. See the other example for this. +*/ + +int led = 7; // the pin that the LED is attached to +int brightness = 0; // how bright the LED is +int fadeAmount = 5; // how many points to fade the LED by + +// the setup routine runs once when you press reset: +void setup() { + // declare pin 9 to be an output: + pinMode(led, OUTPUT); +} + +// the loop routine runs over and over again forever: +void loop() { + // set the brightness of pin 9: + analogWrite(led, brightness); + + // change the brightness for next time through the loop: + brightness = brightness + fadeAmount; + + // reverse the direction of the fading at the ends of the fade: + if (brightness == 0 || brightness == 255) { + fadeAmount = -fadeAmount ; + } + // wait for 30 milliseconds to see the dimming effect + delay(30); +} diff --git a/examples/Projects/Gamepad_Project/Gamepad_Project.ino b/examples/Projects/Gamepad_Project/Gamepad_Project.ino new file mode 100644 index 0000000..f4d17a0 --- /dev/null +++ b/examples/Projects/Gamepad_Project/Gamepad_Project.ino @@ -0,0 +1,110 @@ +/* +Copyright (c) 2014 NicoHood +See the readme for credit to other people. + +Gamepad example project + +Press physical buttons to press USB Gamepad buttons. +This can be used for a simple SNES Controller. +*/ + +// pin mappings +const int pinButton1 = 2; +const int pinButton2 = 3; +const int pinButton3 = 4; +const int pinButton4 = 5; +const int pinButton5 = 6; +const int pinButton6 = 7; +const int pinButton7 = 8; +const int pinButton8 = 9; +const int pinButton9 = 10; +const int pinButton10 = 18; +const int pinButton11 = 19; +const int pinButton12 = 20; + +void setup() { + // pinsetup + pinMode(pinButton1, INPUT_PULLUP); + pinMode(pinButton2, INPUT_PULLUP); + pinMode(pinButton3, INPUT_PULLUP); + pinMode(pinButton4, INPUT_PULLUP); + pinMode(pinButton5, INPUT_PULLUP); + pinMode(pinButton6, INPUT_PULLUP); + pinMode(pinButton7, INPUT_PULLUP); + pinMode(pinButton8, INPUT_PULLUP); + pinMode(pinButton9, INPUT_PULLUP); + pinMode(pinButton10, INPUT_PULLUP); + pinMode(pinButton11, INPUT_PULLUP); + pinMode(pinButton12, INPUT_PULLUP); + + // Sends a clean report to the host. This is important on any Arduino type. + // Make sure all desired USB functions are activated in USBAPI.h! + Gamepad.begin(); +} + +void loop() { + // check each button and press Gamepad if needed + if (!digitalRead(pinButton1)) + Gamepad.press(1); + else + Gamepad.release(1); + + if (!digitalRead(pinButton2)) + Gamepad.press(2); + else + Gamepad.release(2); + + if (!digitalRead(pinButton3)) + Gamepad.press(3); + else + Gamepad.release(3); + + if (!digitalRead(pinButton4)) + Gamepad.press(4); + else + Gamepad.release(4); + + if (!digitalRead(pinButton5)) + Gamepad.press(5); + else + Gamepad.release(5); + + if (!digitalRead(pinButton6)) + Gamepad.press(6); + else + Gamepad.release(6); + + if (!digitalRead(pinButton7)) + Gamepad.press(7); + else + Gamepad.release(7); + + if (!digitalRead(pinButton8)) + Gamepad.press(8); + else + Gamepad.release(8); + + if (!digitalRead(pinButton9)) + Gamepad.press(9); + else + Gamepad.release(9); + + if (!digitalRead(pinButton10)) + Gamepad.press(10); + else + Gamepad.release(10); + + if (!digitalRead(pinButton11)) + Gamepad.press(11); + else + Gamepad.release(11); + + if (!digitalRead(pinButton12)) + Gamepad.press(12); + else + Gamepad.release(12); + + // write the information to the host now! + Gamepad.write(); +} +