OK! Grazie mille per gli aiuti! Funziona tutto alla perfezione! Ho usato lo sketch di prova e tutti i pin mi danno la risposta corretta leggendo sul serial monitor!
Adesso mi rimane solo di integrare questo sketch all'interno del mio precedente software per triggerare gli mp3? qualche idea?
// plays the stdintro menu as long as the receiver is lifted (pin #14 open)
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card
FatReader f; // This holds the information for the file we're play
WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
#define DEBOUNCE 4000 // button debouncer
// this handy function will return the number of bytes currently free in RAM, great for debugging! **
int freeRam(void)
{
** extern int __bss_end;
extern int __brkval;
** int free_memory;*
** if((int)__brkval == 0) {**
** free_memory = ((int)&free_memory) - ((int)&__bss_end);**
** }**
** else {**
** free_memory = ((int)&free_memory) - ((int)__brkval);**
** }**
** return free_memory;**
}
void sdErrorCheck(void)
{
** if (!card.errorCode()) return;**
** putstring("\n\rSD I/O error: ");**
** Serial.print(card.errorCode(), HEX);**
** putstring(", ");**
** Serial.println(card.errorData(), HEX);**
** while(1);**
}
void setup() {
** // set up serial port**
** Serial.begin(9600);**
** putstring_nl("WaveHC with 6 buttons");**
** putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad**
** Serial.println(freeRam()); // if this is under 150 bytes it may spell trouble!**
** // Set the output pins for the DAC control. This pins are defined in the library**
** pinMode(2, OUTPUT);**
** pinMode(3, OUTPUT);**
** pinMode(4, OUTPUT);**
** pinMode(5, OUTPUT);**
** // pin13 LED**
** pinMode(13, OUTPUT);**
** // enable pull-up resistors on switch pins (analog inputs)**
** digitalWrite(14, HIGH);**
** digitalWrite(15, HIGH);**
** digitalWrite(16, HIGH);**
** digitalWrite(17, HIGH);**
** digitalWrite(18, HIGH);**
** digitalWrite(19, HIGH);**
** // if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you**
** if (!card.init()) { //play with 8 MHz spi (default faster!) **
** putstring_nl("Card init. failed!"); // Something went wrong, lets print out why**
** sdErrorCheck();**
** while(1); // then 'halt' - do nothing!**
** }**
** // enable optimize read - some cards may timeout. Disable if you're having problems**
** card.partialBlockRead(true);**
// Now we will look for a FAT partition!
** uint8_t part;**
** for (part = 0; part < 5; part++) { // we have up to 5 slots to look in**
** if (vol.init(card, part))**
** break; // we found one, lets bail**
** }**
** if (part == 5) { // if we ended up not finding one :(**
** putstring_nl("No valid FAT partition!");**
** sdErrorCheck(); // Something went wrong, lets print out why**
** while(1); // then 'halt' - do nothing!**
** }**
** // Lets tell the user about what we found**
** putstring("Using partition ");**
** Serial.print(part, DEC);**
** putstring(", type is FAT");**
** Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?**
** // Try to open the root directory**
** if (!root.openRoot(vol)) {**
** putstring_nl("Can't open root dir!"); // Something went wrong,**
** while(1); // then 'halt' - do nothing!**
** }**
** // Whew! We got past the tough parts.**
** putstring_nl("Ready!");**
}
int thetrack = 0;
int playpoem = 0;
int buttonpressed = 0;
// ----------------- LOOP ----------------- LOOP ----------------- LOOP ----------------- LOOP -----------------
void loop() {
putstring_nl("loop");
** while (digitalRead(14) == HIGH) { // check if the receiver id lifted**
** buttonpressed = (check_switches());**
** if (buttonpressed != 0 ) { // if buttons has been pressed**
** putstring("button pressed:"); Serial.println(buttonpressed);**
** if (thetrack == 0) { // check in witch menu we are**
** putstring_nl("button pressed on stdintro");**
** wave.stop();**
** thetrack = 1;**
** }**
** else if (thetrack == 1) { // check in witch menu we are**
** putstring_nl("button pressed on menu");**
** playpoem = 1;**
** switch (buttonpressed) {**
** case 1:**
** wave.stop();**
** thetrack = 2; // plays poem #1 (thetrack -1)**
** break;**
** case 2:**
** wave.stop();**
** thetrack = 3;**
** break;**
** case 3:**
** wave.stop();**
** thetrack = 4;**
** break;**
** case 4:**
** wave.stop();**
** thetrack = 5;**
** break;**
** }**
** }**
** else { // if we are playing a poem**
** putstring_nl("button pressed on poems");**
** wave.stop();**
** thetrack = 0; // torna al menu principale**
** } **
** }**
** if (!wave.isplaying) {**
** if (thetrack <= 1) {**
** playtrack (thetrack);**
** }**
** else if (thetrack > 1) {**
** if (playpoem == 0) {**
** thetrack = 0;**
** playtrack (thetrack);**
** }**
** else if (playpoem == 1) {**
** playtrack (thetrack);**
** playpoem = 0;**
** }**
** } **
** }**
** // playloop (thetrack);**
** }**
** putstring_nl("phoneoff_wave_stopped");**
** wave.stop();**
}
// ----------------- ENDLOOP -----------------
/*
// Plays a track in loop
void playloop (byte trackID) {
** if (!wave.isplaying) {**
** playtrack (trackID); **
** }**
}
*/
// Translates file index to files name
/*
WAVE FILES INDEX:
0 STDINTRO.WAV
1 MENU.WAV
... add below if needed
*/
void playtrack (byte track) {
** switch (track) {**
** case 0:**
** playfile("STDINTRO.WAV");**
** break;**
** case 1:**
** playfile("MENU.WAV");**
** break;**
** case 2:**
** playfile("POEM1.WAV");**
** break;**
** case 3:**
** playfile("POEM2.WAV");**
** break; **
** }**
}
// Plays files and print their name
void playfile(char *name) {
** // see if the wave object is currently doing something**
** if (wave.isplaying) {// already playing something, so stop it!**
** wave.stop(); // stop it**
** }**
** // look in the root directory and open the file**
** if (!f.open(root, name)) {**
** putstring("Couldn't open file "); Serial.print(name); return;**
** }**
** // OK read the file and turn it into a wave object**
** if (!wave.create(f)) {**
** putstring_nl("Not a valid WAV"); return;**
** }**
** // ok time to play! start playback**
** putstring("Playing file: "); Serial.println(name);**
** wave.play();**
}
int check_switches()
{
** static byte previous[5];**
** static long time[5];**
** byte reading;**
** byte pressed;**
** byte index;**
** pressed = 0;**
** for (byte index = 0; index < 5; ++index) {**
** reading = digitalRead(15 + index);**
** if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)**
** {**
** // switch pressed**
** time[index] = millis();**
** pressed = index + 1;**
** break;**
** }**
** previous[index] = reading;**
** }**
** // return switch number (0 - 5)**
** return (pressed);**
}
e questo è lo sketch di prova che sto usando per il keypad
/* Keypadtest.pde
*
* Demonstrate the simplest use of the keypad library.
*
* The first step is to connect your keypad to the
* Arduino using the pin numbers listed below in
* rowPins[] and colPins[]. If you want to use different
* pins then you can change the numbers below to
* match your setup.
*
* Note: Make sure to use pullup resistors on each of
* the rowPins.
*/
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
** {'1','2','3'},**
** {'4','5','6'},**
** {'7','8','9'},**
__ {'#','0',''}__
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 9, 8, 7, 6 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 12, 11, 10 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
void setup()
{
** digitalWrite(ledpin, HIGH);**
** Serial.begin(9600);**
}
void loop()
{
** char key = kpd.getKey();**
** if(key) // same as if(key != NO_KEY)**
** {**
** switch (key)**
** {**
__ case '':__
** digitalWrite(ledpin, LOW);**
** break;**
** case '#':**
** digitalWrite(ledpin, HIGH);**
** break;**
** default:**
** Serial.println(key);**
** }**
** }**
}