RISOLTO: Tastiera per macro, richiesta aiuto codice.

Salve. Come primo progetto -siate pazienti- sto cercando di realizzare una tastiera per macro, che da una matrice di pulsanti mi digiti delle combinazioni di tasti e/o dei testi ricorrenti.
Mi sono basato su un Teensy 2.0 e il codice di Arduino Forum.

Ho inserito nel loop, per vedere se l'errore era al suo interno, un po' di codice per il LED. Bene, il LED lampeggia come dovrebbe, eppure non riesco a far funzionare la tastiera come dovrebbe.

Il Teensy è configurato nell'IDE come USB, keyboard+mouse+ joystick,
a questo punto suppongo di aver sbagliato qualcosa nell'accoppiamento dei casi, o nelle dichiarazioni, ma non riesco a capire cosa.

Grazie in anticipo.

#include <Keypad.h>

// Author: Nick Gammon - mod sardauker
// Date: 5th July 2012
// Modified: 22 October 2012

int led = 11;
const byte ROWS = 6;
const byte COLS = 8;

char keys[ROWS][COLS] = {
{'31', '61', '91', '#1', '*2', '72', '42', '12'},
{'21', '51', '81', '01', '02', '82', '52', '22'},
{'11', '41', '71', '*1', '#2', '92', '62', '32'},
{'33', '63', '93', '#3', '*4', '74', '44', '14'},
{'23', '53', '83', '03', '04', '84', '54', '24'},
{'13', '43', '73', '*3', '#4', '94', '64', '34'},
};

byte rowPins[ROWS] = {11, 12, 13, 14, 15, 16}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {1, 2, 3, 4, 5, 6, 7, 8}; //connect to the column pinouts of the keypad

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup ()
{
// initialize control over the keyboard:
Keyboard.begin();
pinMode (ledPin, OUTPUT);
} // end of setup

void loop ()
{

//heartbeat
digitalWrite(led, HIGH); // turn the LED on
delay(200); // lascia acceso
digitalWrite(led, LOW); // spegne
delay(200); // pausa
digitalWrite(led, HIGH); // turn the LED on
delay(200); //lascia acceso
digitalWrite(led, LOW); // fine hearthbeat
delay(1000); // wait for a second

byte key = kpd.getKey();
if (!key)
return;

switch (key)
{
case '11': Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT | MODIFIERKEY_SHIFT ); Keyboard.println (F("11")); Keyboard.set_modifier(0); break;
case '21': Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT | MODIFIERKEY_SHIFT ); Keyboard.set_key1(KEY_DELETE); Keyboard.send_now(); Keyboard.set_modifier(0); Keyboard.set_key1(0); Keyboard.send_now() ; break;
case '31': Keyboard.println (F(" 31")); break;
case '41': Keyboard.println (F(" 41")); break;

// e così via...

} // end of switch

} // end of loop

Quel codice é scritto per un Arduino Leonardo. Non so se il Teensy 2.0 sia cosí compatibile che quel Sketch Arduino Leonardo funzioni sul Teensy 2.0.
Teensy 2.0 e Arduino Leonardo sono molto simili perché usano lo stesso microcontroller, ma sono veramente compatibili al 100% nelle funzioni che richiede quel sketch?

Ciao Uwe

Intanto grazie per la risposta.

Se per funzioni intendi keyboard.println et similia, dovrebbe essere così: è la prima cosa che ho cercato nella documentazione.
la libreria è la stessa. l'ide non segnala errori, e riconosce il teensy, tant'è che se in vece di keyboard faccio .begin alla seriale -ho provato un po' di tutto- mi dice che non è un Leonardo.

a 'sto punto non so se ho sbagliato qualcosa nella configurazione, nelle dichiarazioni, nello switch o, peggio, nell'assegnazione dei pin, ma non so più sotto quale tappeto andare a guardare.

Grazie ancora, mi sa che se non trovo la bega a breve devo dissaldare e rimettere sulla breadboard.

Ma non c'é un esempio simile per il Teensy 2.0 che puoi adattare per esempio dove Teensy 2.0. emula una tastiera?
Ciao Uwe

Sì.
Solo che io non trovo discrepanze.

Ho provato pure a mettere il ciclo "hello world", in modo da slegarlo dai pulsanti, ma niente.

Non devo aver capito come configurare il teensy come tastiera o la tastiera come tale.

Teensyduino: Using USB Keyboard with Teensy on the Arduino IDE per la documentazione

http://www.pjrc.com/teensy/projects/password_store.ino verso la metà usa Keyboard.print("name@email.com");

Am I doing this right? (Reddit Upvote Button) - The New Hobbyist The New Hobbyist usa un teensy per impiazzare due tasti, ma il principio è quello.

Insomma, temo non siano le istruzioni, ma d'aver perso qualcosa per strada.

Grazie comunque :slight_smile:

Io ho dei dubbi sul tuo array di char "keys". Vedo dei caratteri strani, che in altri esempi non ho visto.

Quelli sono perché ho messo in matrice quattro matrici per telefono.
In ogni caso, ho provato anche con soli numeri (da 11 a 81, praticamente contando in ottale righe-colonne) e lo stesso non torna.

c'è un altro modo per definire i casi dello switch?

Risolto:

#include <Keypad.h>

int led = 11;
const byte ROWS = 6;
const byte COLS = 8;
char keys[ROWS][COLS] = {
{38, 37, 36, 35, 14, 13, 12, 11},
{28, 27, 26, 25, 24, 23, 22, 21},
{18, 17, 16, 15, 34, 33, 32, 31},
{48, 47, 46, 45, 64, 63, 62, 61},
{58, 57, 56, 55, 54, 53, 52, 51},
{68, 67, 66, 65, 44, 43, 42, 41},

};

byte rowPins[ROWS] = {10, 12, 13, 14, 15, 16}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {1, 2, 3, 4, 5, 6, 7, 8}; //connect to the column pinouts of the keypad

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup ()
{
// initialize control over the keyboard:
Keyboard.begin();
} // end of setup

void loop ()
{

digitalWrite(led, HIGH);

byte key = kpd.getKey();
if (!key)
return;

switch (key)
{
case 11: Keyboard.set_key1(KEY_F5); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now(); break;//start -100
case 12: Keyboard.set_key1(KEY_F6); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now(); break;//start -10
case 13: Keyboard.set_key1(KEY_F7); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now(); break;//start +10
case 14: Keyboard.set_key1(KEY_F8); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now(); break;//start +100
case 15: Keyboard.set_modifier(MODIFIERKEY_CTRL); Keyboard.send_now(); //70%
Keyboard.set_key1(KEY_7); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now();
Keyboard.set_modifier(0); Keyboard.send_now(); break;
case 16: Keyboard.set_modifier(MODIFIERKEY_CTRL); Keyboard.send_now(); //80%
Keyboard.set_key1(KEY_8); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now();
Keyboard.set_modifier(0); Keyboard.send_now(); break;
case 17: Keyboard.set_modifier(MODIFIERKEY_CTRL); Keyboard.send_now(); //90%
Keyboard.set_key1(KEY_9); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now();
Keyboard.set_modifier(0); Keyboard.send_now(); break;
case 18: Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT); Keyboard.send_now(); //100%
Keyboard.set_key1(KEY_0); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now();
Keyboard.set_modifier(0); Keyboard.send_now(); break;
case 21: Keyboard.set_key1(KEY_F12); Keyboard.send_now(); Keyboard.set_key1(0); Keyboard.send_now(); break;//stop -100
//e via così

} // end of switch
} // end of loop