Added Gamepad Project, PWM Fade example

This commit is contained in:
Nico 2014-11-22 14:02:34 +01:00
parent 2a906abd79
commit 19dc97ac89
2 changed files with 149 additions and 0 deletions

View file

@ -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);
}

View file

@ -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();
}