Ciao a tutti, sto realizzando delle periferiche per un simulatore di guida (3 potenziometri, una cella di carico tramite hx711, almeno 10 pulsanti, forse più) e volevo collegarla al pc tramite arduino. Ho comprato un arduino mega 2560 in quanto ha molti più pin di un leonardo o un pro micro ma purtroppo i tutorial scarseggiano. Sono riuscito tramite flip a renderlo visibile al pc come un joystick usb. Aspettando che arrivino i potenziometri e la cella di carico sto testando il codice per i pulsanti, ho preso ispirazione da questo video (Build Arduino Big Joystik in MEGA. - YouTube) e ho modificato il codice per avere più pulsanti, attivati collegando la massa i vari pin. Per fare una prova con windows e quindi con 32 pulsanti ho messo i pin da 2 a 34 (prima era da 2 a 12). L'unico risultato che ho ottenuto è che ora mettendo a massa i pin da 17 a 32 si attivano i pulsanti corrispondenti, di contro tutti i pin sotto il 17 non danno segni di vita. Posto il codice, magari qualcuno sa cosa correggere per farlo funzionare
/* Arduino USB Joystick HID demo */
/* Author: Darran Hunt
* Released into the public domain.
* Update by RICLAMER in 25/03/2014 to use Analog ports and digital ports
* This code is to be used with Arduino UNO (6 axis and 13 Button )
* This code is compatible with Arduino Mega.
*/
/* INSTALATION
Just install POT in each analog port. Using the _Grnd _Analog _5V Arduino.
Like this image: http://arduino.cc/en/uploads/Tutorial/joy_sch_480.jpg
To setup the buttons, just install you prefered button switch under GND and Port Digital 02~13.
Use Flip to erease and burn this firmware DFU: https://github.com/harlequin-tech/arduino-usb/blob/master/firmwares/Arduino-big-joystick.hex
I used Arduino R3 with Atmega 16U2.
*/
#undef DEBUG
#define NUM_BUTTONS 40 // you don't need to change this value
#define NUM_AXES 8 // 6 axes to UNO, and 8 to MEGA. If you are using UNO, don't need to change this value.
typedef struct joyReport_t {
int16_t axis[NUM_AXES];
uint8_t button[(NUM_BUTTONS+7)/8]; // 8 buttons per byte
} joyReport_t;
joyReport_t joyReport;
uint8_t btn[40];
int fulloff = 0;
void setup(void);
void loop(void);
void setButton(joyReport_t *joy, uint8_t button);
void clearButton(joyReport_t *joy, uint8_t button);
void sendJoyReport(joyReport_t *report);
void setup()
{
//set pin to input Button , Pin 2 to 34
for( int portId = 2; portId < 34; portId ++ )
{
pinMode( portId, INPUT_PULLUP);
}
Serial.begin(115200);
delay(200);
for (uint8_t ind=0; ind<8; ind++) {
joyReport.axis[ind] = ind*1000;
}
for (uint8_t ind=0; ind<sizeof(joyReport.button); ind++) {
joyReport.button[ind] = 0;
}
}
// Send an HID report to the USB interface
void sendJoyReport(struct joyReport_t *report)
{
#ifndef DEBUG
Serial.write((uint8_t *)report, sizeof(joyReport_t));
#else
// dump human readable output for debugging
for (uint8_t ind=0; ind<NUM_AXES; ind++) {
Serial.print("axis[");
Serial.print(ind);
Serial.print("]= ");
Serial.print(report->axis[ind]);
Serial.print(" ");
}
Serial.println();
for (uint8_t ind=0; ind<NUM_BUTTONS/8; ind++) {
Serial.print("button[");
Serial.print(ind);
Serial.print("]= ");
Serial.print(report->button[ind], HEX);
Serial.print(" ");
}
Serial.println();
#endif
}
// turn a button on
void setButton(joyReport_t *joy, uint8_t button)
{
uint8_t index = button/8;
uint8_t bit = button - 8*index;
joy->button[index] |= 1 << bit;
}
// turn a button off
void clearButton(joyReport_t *joy, uint8_t button)
{
uint8_t index = button/8;
uint8_t bit = button - 8*index;
joy->button[index] &= ~(1 << bit);
}
/*
* Read Digital port for Button
* Read Analog port for axis
*/
void loop()
{
// Create Matriz with value off switch button
for(int bt = 01; bt < 33; bt ++)
{
btn[bt]= digitalRead(bt+1);
}
// Power ON button if it is press.
for(int on=01; on < 33; on++)
{
if (btn[on] == LOW)
{
setButton(&joyReport, on);
// Serial.println("botao ON");
// Serial.println(on);
}else{
fulloff++;
} //Power off all Button, if all button are not press
if(fulloff == 32)
{
for(uint8_t cont=0; cont<40; cont++)
{
clearButton(&joyReport, cont);
}
// Serial.println("Every buttons is off");
}
}
/* Move all of the Analogic axis */
/* Arduino UNO have only 6 port. Will be necessary set to 0; the not used port */
/* In this example, i will use just 3 axis. X, Y, Z */
/* if you will use all 6 analog ports, set axis < 3, to axis < 5 */
for (uint8_t axis = 0; axis < 5; axis++) {
joyReport.axis[axis] = map(analogRead(axis), 0, 1023, -32768,32767 );
}
//Set to 0; the not used analog port.
//if you will use all 6 analog ports, exclude the lines, axis[3] at axis [5] */
//joyReport.axis[3] = 0;
joyReport.axis[4] = 0;
joyReport.axis[5] = 0;
joyReport.axis[6] = 0;
joyReport.axis[7] = 0;
joyReport.axis[8] = 0;
//Send Data to HID
sendJoyReport(&joyReport);
delay(100);
fulloff = 0;
}
Anche consigli su qualche guida per iniziare a programmare mi farebbe utile, soprattutto perchè questo codice mi sembra molto diverso da quelli usati in altri joystick con arduino.
Come ultima domanda, secondo voi conviene proseguire col Mega o conviene un Leonardo o un Pro Micro e nel caso mi servano più pulsanti usarne 2?