Ok grazie!
in realtà non credo sia un errore ma solo che l'esempio sia fuorviante e che il codice riferisca al pin2 (non mostrandone il collegamento nello schema) e che lo schema mostri invece il collegamento del pin6 allo switch dello stick per usarlo per le funzioni ad esempio citate dalla riga 80 alla 94......
al di la di queste considerazioni, ho fatto questi collegamenti :
e messo assieme questo codice
#include "Mouse.h"
// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 4; // switch to turn on and off mouse control
const int xAxis = A0; // joystick X axis
const int yAxis = A1; // joystick Y axis
const int ledPin = 5; // Mouse control LED
// parameters for reading the joystick:
int range = 3; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range / 4; // resting threshold
int center = range / 2; // resting position value
bool mouseIsActive = false; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state
// parametri encoder rotativo
int valore = 0;
int letturaPrecedente = HIGH;
void setup() {
// setup Encoder rotativo
pinMode (6, INPUT_PULLUP); // mousewheel UP
pinMode (7, INPUT_PULLUP); // mousewheel DOWN
Serial.begin (9600);
// end setup Encoder rotativo
//setup mouse joystick
pinMode(switchPin, INPUT_PULLUP); // the switch pin
pinMode(ledPin, OUTPUT); // the LED pin
// take control of the mouse:
Mouse.begin();
// end setup mouse joystick
}
void loop() {
// loop encoder rotativo
int n = digitalRead(6);
if ((letturaPrecedente == HIGH) && (n == LOW)) {
if (digitalRead(7) == HIGH) {
valore--;
// scroll direzione 1
Mouse.move(0, 0, 1);
} else {
valore++;
// scroll direzione 2
Mouse.move(0, 0, -1);
}
Serial.println(valore);
}
letturaPrecedente = n;
// end loop encoder rotativo
// loop mouse joystick
// read the switch:
int switchState = digitalRead(switchPin);
// if it's changed and it's high, toggle the mouse state:
if (switchState != lastSwitchState) {
if (switchState == HIGH) {
mouseIsActive = !mouseIsActive;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
}
// save switch state for next comparison:
lastSwitchState = switchState;
// read and scale the two axes:
int xReading = readAxis(A0);
int yReading = readAxis(A1);
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
}
delay(responseDelay);
}
//reads an axis (0 or 1 for x or y) and scales the analog input range to a range from 0 to <range>
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);
// if the output reading is outside from the rest position threshold, use it:
int distance = reading - center;
if (abs(distance) < threshold) {
distance = 0;
}
// return the distance for this axis:
return distance;
// end loop mouse joystick
}
A questo punto ho due domande da supernoob :
1 - Come potere vedere porto al pulsante sulla breadboard solo il GND, che problema c'è a usare un collegamento di questo tipo senza portare 5V e senza resistenza ?
2- se la cosa di cui sopra è lecita, dovendo aggiungere dei pulsanti per fornire al mio progetto la possibilità di includere Keyboard.h ed emulare anche alcuni tasti della tastiera, posso usare lo stesso sistema senza portare loro 5V e senza impiegare resistori ?
Grazie dell'attenzione e perdonate le mie domande da poppante.
Buona serata.
Lorenzo