Indice
Descrizione
In questo episodio del tutorial di Arduino, impareremo a emulare la tastiera e il mouse. Realizzeremo alcuni semplici esempi di sketch e circuiti per trasformare la scheda di Arduino in un dispositivo di controllo del computer. Utilizzeremo dei pulsanti e il modulo joystick per inviare i caratteri al computer e muovere il cursore del mouse.
#include <Keyboard.h>
int buttons[5] = {7, 6, 5, 4, 3};
void setup(){
Keyboard.begin();
for(int i = 0; i < 5; i++){
pinMode(buttons[i], INPUT);
}
}
void loop(){
if(digitalRead(buttons[0]) == HIGH){
Keyboard.write('a');
delay(200);
}
if(digitalRead(buttons[1]) == HIGH){
Keyboard.println(" tutorial di arduino");
Keyboard.print("come emulare la tastiera con ...");
delay(200);
}
if(digitalRead(buttons[2]) == HIGH){
Keyboard.write(KEY_BACKSPACE);
delay(200);
}
if(digitalRead(buttons[3]) == HIGH){
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('c');
delay(100);
Keyboard.releaseAll();
delay(200);
}
if(digitalRead(buttons[4]) == HIGH){
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('v');
delay(100);
Keyboard.releaseAll();
delay(200);
}
delay(10);
}
#include <Mouse.h>
#define JOYSTICK_X_PIN A0
#define JOYSTICK_Y_PIN A1
#define JOYSTICK_BUTTON_PIN 2
int buttons[5] = {7, 6, 5, 4, 3};
bool enable = false;
int wheelDirection = 0;
void setup(){
Mouse.begin();
for(int i = 0; i < 5; i++){
pinMode(buttons[i], INPUT);
}
pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
}
void loop(){
if(digitalRead(JOYSTICK_BUTTON_PIN) == LOW){
enable = !enable;
delay(500);
}
if(enable){
if(digitalRead(buttons[0]) == HIGH){
Mouse.click(MOUSE_LEFT);
delay(200);
}
if(digitalRead(buttons[1]) == HIGH){
Mouse.click(MOUSE_MIDDLE);
delay(200);
}
if(digitalRead(buttons[2]) == HIGH){
Mouse.click(MOUSE_RIGHT);
delay(200);
}
if(digitalRead(buttons[3]) == HIGH){
wheelDirection++;
delay(200);
}
if(digitalRead(buttons[4]) == HIGH){
wheelDirection--;
delay(200);
}
int xDelta = axisDelta(JOYSTICK_X_PIN);
int yDelta = axisDelta(JOYSTICK_Y_PIN);
Mouse.move(xDelta, yDelta, wheelDirection);
wheelDirection = 0;
}
delay(10);
}
int axisDelta(int axisPin){
return (analogRead(axisPin)-512)/150;
}