questo è il file che mi ha mandato Havebug. Va salvato come Gamepad_Havebug.ino
@EAF51_Havebug il forum ha la funzione specifica per postare codice. (è l’icona </> ) (invece di abilitare ulteriori tipi di files lo posto così! dimmi se ti va bene, altrimenti lo tolgo)
P.S. Fighissimo che si possano fare queste cose! E ancora più bello avere uno in gruppo che lo sa fare!
ecco il codice:
//Throttle add-on controller
//by Havebug
//
//disclaimer:
//Author is not responsible for any damage this schetch may cause to your system.
//Use at your own risk.
//
//This code is offered as Open-Source
//Feel free to share it, mod it, improve it... and share it
//No fee applies for it's distribution.
//
//All credits for the libraries used in this sketch go to the relevant creators.
// ----------------------------------
// connections:
//
// 6x Potentiometer: pin A(0-5)
// 10x Buttons: pin 4-13
// 1x I2C LCD 20x4: pin 2-3
// ----------------------------------
// Pin out:
// ___
// / \== 5V
// Potentiometer | O |== to pin
// \___/== GND
//
// ___
// Button / \== to pin
// \___/== GND
// ----------------------------------
#include <Joystick.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
10, 0, // Button Count, Hat Switch Count
true, true, true, // X, Y, Z
true, true, true, // Rx, Ry, Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
const int axis0 = A0;
const int axis1 = A1;
const int axis2 = A2;
const int axis3 = A3;
const int axis4 = A4;
const int axis5 = A5;
int sensorValue = 0;
int outputValue = 0;
int newInd0=1;
int oldInd0=1;
int newInd1=1;
int oldInd1=1;
int newInd2=1;
int oldInd2=1;
int flaps=0;
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void printintro();
void setup() {
// Initialize Button Pins
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
Joystick.setZAxisRange(-512, 512);
Joystick.setRxAxisRange(-512, 512);
Joystick.setRyAxisRange(-512, 512);
Joystick.setRzAxisRange(-512, 512);
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
printintro();
}
// Last state of the buttons
int lastButtonState[10] = {0,0,0,0,0,0,0,0,0,0};
void loop() {
// Read pin values
for (int index = 0; index < 10; index++)
{
int currentButtonState = !digitalRead(index + 4);
if (currentButtonState != lastButtonState[index])
{
switch (index) {
case 0: // button 1
Joystick.setButton(0, currentButtonState);
lcd.setCursor(5,0);
lcd.print(" UP ");
break;
case 1: // button 2
Joystick.setButton(1, currentButtonState);
lcd.setCursor(5,0);
lcd.print("DOWN");
break;
case 2: // button 3
Joystick.setButton(2, currentButtonState);
lcd.setCursor(16,0);
if (currentButtonState)
{
flaps=flaps -1;
if (flaps <0) flaps=0;
updateflap();
}
break;
case 3: // button 4
Joystick.setButton(3, currentButtonState);
lcd.setCursor(16,0);
if (currentButtonState)
{
flaps=flaps +1;
if (flaps >5) flaps=5;
updateflap();
}
break;
case 4: // button 5
Joystick.setButton(4, currentButtonState);
break;
case 5: // button 6
Joystick.setButton(5, currentButtonState);
break;
case 6: // button 7
Joystick.setButton(6, currentButtonState);
break;
case 7: // button 8
Joystick.setButton(7, currentButtonState);
break;
case 8: // button 9
Joystick.setButton(8, currentButtonState);
break;
case 9: // button 10
Joystick.setButton(9, currentButtonState);
break;
}
lastButtonState[index] = currentButtonState;
}
}
sensorValue = analogRead(axis0);
outputValue = map(sensorValue, 0, 1023, -512, 512);
Joystick.setXAxis(outputValue);
newInd0= map(sensorValue, 0, 1023, 1, 19);
if (newInd0!=oldInd0)
{
lcd.setCursor(oldInd0,3);
if (oldInd0==10) lcd.print("+");
else lcd.print("-");
lcd.setCursor(newInd0,3);
lcd.print((char)255);
oldInd0=newInd0;
}
// print the results to the Serial Monitor:
sensorValue = analogRead(axis1);
outputValue = map(sensorValue, 0, 1023, -512, 512);
Joystick.setYAxis(outputValue);
newInd1= map(sensorValue, 0, 1023, 1, 19);
if (newInd1!=oldInd1)
{
lcd.setCursor(oldInd1,2);
if (oldInd1==10) lcd.print("+");
else lcd.print("-");
lcd.setCursor(newInd1,2);
lcd.print((char)255);
oldInd1=newInd1;
}
// Serial.print("sensor = ");
// Serial.print(sensorValue);
// Serial.print("\t output = ");
// Serial.println(outputValue);
sensorValue = analogRead(axis2);
outputValue = map(sensorValue, 0, 1023, -512, 512);
Joystick.setZAxis(outputValue);
newInd2= map(sensorValue, 0, 1023, 1, 19);
if (newInd2!=oldInd2)
{
lcd.setCursor(oldInd2,1);
if (oldInd2==10) lcd.print("+");
else lcd.print("-");
lcd.setCursor(newInd2,1);
lcd.print((char)255);
oldInd2=newInd2;
}
sensorValue = analogRead(axis3);
outputValue =sensorValue - 512;
Joystick.setRxAxis(outputValue);
sensorValue = analogRead(axis4);
outputValue =sensorValue - 512;
Joystick.setRyAxis(outputValue);
sensorValue = analogRead(axis5);
outputValue =sensorValue - 512;
Joystick.setRzAxis(outputValue);
delay(30);
}
void printintro()
{
lcd.setCursor(0,0);
lcd.print("Gear:---- Flaps:----");
lcd.setCursor(0,1);
lcd.print("A---------+---------");
lcd.setCursor(0,2);
lcd.print("P---------+---------");
lcd.setCursor(0,3);
lcd.print("R---------+---------");
lcd.setCursor(2,3);
lcd.print("");
}
void updateflap()
{
if (flaps==0) lcd.print(" 0 ");
if (flaps==1) lcd.print(" 10 ");
if (flaps==2) lcd.print(" 20 ");
if (flaps==3) lcd.print(" 30 ");
if (flaps==4) lcd.print(" 40 ");
if (flaps==5) lcd.print(" 50 ");
}