I went online to find the relevant information, I find that using Arduino and Wave Shield can provide a sound. In adafruit website '' https://learn.adafruit.com/adafruit-wav ... o/examples '' It provide some example to teach us how to using the wave shield and I use it to test my wave shield. The wave shield is work.
And than, I also find the servo code about useless box on GitHub and two servo are working.
When I combine the code of servo and the code of wave shield, the problem is coming out. It will have some error...
Late, I know the problem is about the ''Timer 1''. Both program are using timer1, so it will have some confuse. Some people said that using ''ServoTimer2.h'' can solve the problem but I tried so many time the result is.... I cannot solve it.
Is it have some error I missed?
Please to point out or suggest me :smiley-confuse:
The code:
Code: Select all
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <ServoTimer2.h>
SdReader card;
FatVolume vol;
FatReader root;
FatReader f;
WaveHC wave;
#define DEBOUNCE 5
#define myPin 12
ServoTimer2 myservo;
byte buttons[] = {9, 8, 7, 17, 18, 19};
#define NUMBUTTONS sizeof(buttons)
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
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() {
myservo.attach(myPin);
byte i;
// set up serial port
Serial.begin(9600);
putstring_nl("WaveHC with ");
Serial.print(NUMBUTTONS, DEC);
putstring_nl("buttons");
putstring("Free RAM: ");
Serial.println(freeRam());
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons, INPUT);
digitalWrite(buttons, HIGH);
}
if (!card.init()) {
putstring_nl("Card init. failed!");
sdErrorCheck();
while(1);
}
card.partialBlockRead(true);
uint8_t part;
for (part = 0; part < 5; part++) {
if (vol.init(card, part))
break;
}
if (part == 5) {
putstring_nl("No valid FAT partition!");
sdErrorCheck();
while(1);
}
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC);
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!");
while(1);
}
putstring_nl("Ready!");
TCCR2A = 0;
TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;
TIMSK2 |= 1<<TOIE2;
}
SIGNAL(TIMER2_OVF_vect) {
check_switches();
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
byte index;
for (index = 0; index < NUMBUTTONS; index++) {
currentstate[index] = digitalRead(buttons[index]);
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
justreleased[index] = 1;
}
pressed[index] = !currentstate[index];
}
previousstate[index] = currentstate[index];
}
}
int incPulse(int val, int inc){
if( val + inc > 2000 )
return 1000 ;
else
return val + inc;
}
void loop() {
int val;
val = incPulse( myservo.read(), 1);
myservo.write(val);
delay(15);
byte i;
if (justpressed[0]) {
justpressed[0] = 0;
playcomplete("0.WAV");
}
if (justpressed[1]) {
justpressed[1] = 0;
playcomplete("1.WAV");
}
if (justpressed[2]) {
justpressed[2] = 0;
playcomplete("2.WAV");
}
if (justpressed[3]) {
justpressed[3] = 0;
playcomplete("0.WAV");
}
if (justpressed[4]) {
justpressed[4] = 0;
playcomplete("1.WAV");
}
if (justpressed[5]) {
justpressed[5] = 0;
playcomplete("2.WAV");
}
}
void playcomplete(char *name) {
playfile(name);
while (wave.isplaying) {
}
}
void playfile(char *name) {
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
wave.play();
}