Created
September 28, 2014 19:25
-
-
Save TICLasPalmas/da77279afc3a3269297f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *********************************************** */ | |
/* Nombre: ArduPiano */ | |
/* Descripción: Algoritmo que lee las pulsaciones */ | |
/* de un teclado matricial 4x4 y reproduce la nota */ | |
/* correspondiente en un zumbador. */ | |
/* */ | |
/* Creado por: TIC Las Palmas */ | |
/* Web: http://www.ticlaspalmas.com */ | |
/* Email: info[at]ticlaspalmas[dot]com */ | |
/* *********************************************** */ | |
// Incluimos la librería para manejar los teclados matriciales | |
#include <Keypad.h> | |
// Definimos las notas | |
const int DO = 262; | |
const int RE = 294; | |
const int MI = 330; | |
const int FA = 349; | |
const int SOL = 392; | |
const int LA = 440; | |
const int SI = 494; | |
const int DO2 = 523; | |
int notas_musicales[] = {DO, RE, MI, FA, SOL, LA, SI, DO2}; | |
// Configuramos el teclado | |
const byte filas = 4; | |
const byte columnas = 4; | |
char teclas[filas][columnas] = { | |
{'C', 'D', 'E', 'F'}, | |
{'G', 'A', 'B', 'H'}, | |
{'I', 'J', 'K', 'L'}, | |
{'M', 'N', 'O', 'P'} | |
}; | |
// Conexiones a los pines de las filas | |
byte pinesFila[filas] = {2, 3, 4, 5}; | |
// Conexiones a los pines de las columnas | |
byte pinesColumna[columnas] = {6, 7, 8, 9}; | |
Keypad teclado = Keypad(makeKeymap(teclas), pinesFila, pinesColumna, filas, columnas); | |
// Configuramos los pines de la placa | |
void setup() { | |
// Inicializamos el pin 13 como salida digital | |
pinMode(13, OUTPUT); | |
} | |
// La función principal que se ejecutará infinitamente | |
void loop() { | |
// Almacenamos la tecla pulsada | |
char tecla = teclado.getKey(); | |
// Si hemos pulsado una tecla | |
if (tecla != NO_KEY){ | |
// Si el teclado es pulsado | |
if (teclado.getState() == PRESSED) { | |
// Reproducimos el sonido según la tecla pulsada | |
switch (tecla) { | |
case 'C': | |
tone(13, notas_musicales[0]); | |
break; | |
case 'D': | |
tone(13, notas_musicales[1]); | |
break; | |
case 'E': | |
tone(13, notas_musicales[2]); | |
break; | |
case 'F': | |
tone(13, notas_musicales[3]); | |
break; | |
case 'G': | |
tone(13, notas_musicales[4]); | |
break; | |
case 'A': | |
tone(13, notas_musicales[5]); | |
break; | |
case 'B': | |
tone(13, notas_musicales[6]); | |
break; | |
case 'H': | |
tone(13, notas_musicales[7]); | |
break; | |
default: | |
tone(13, notas_musicales[0]); | |
} | |
} | |
} | |
// Comprobamos si se ha dejado de pulsar la tecla | |
if (teclado.getState() == RELEASED) { | |
// Paramos el sonido | |
noTone(13); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment