Hi everybody, I have a idea to create a box like ''useless box'' but including my sound.So the box will use two servo and one wave shield, one servo is control the door open or close, another servo is a hand to close the switch.
I went online to find the relevant information, I find that using Arduino and Wave Shield can provide a sound. In adafruit website '' Examples | Wave Shield | Adafruit Learning System '' 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 ![]()
The code:
#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();*
}