About Wave shield + 2 Servo Problems

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 :confused:

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();*
}

I cannot solve it.

Can not solve what? Aside from the fact that the code you posted is NOT the code you compiled (YOUR code does NOT have italics in it), what IS the problem?

PaulS:
Can not solve what? Aside from the fact that the code you posted is NOT the code you compiled (YOUR code does NOT have italics in it), what IS the problem?

Thanks for your reply :slight_smile:
My problem is that I cannot using the wave shield and servo at the same time.
I don’t know how to write that program :confused:

I use Arduino 1.5.8 because the new one cannot use ‘‘ServoTimer2’’ library.

I use Arduino 1.5.8 because the new one cannot use ''ServoTimer2'' library.

These unsupported statements do not work here. There is no real reason why ServoTimer2 can not be used with versions of the IDE after 1.5.8.

My problem is that I cannot using the wave shield and servo at the same time.

You said that. You provided NO proof of that statement. WHY can't you use them at the same time? If the problem is that they both use timer 1, then using ServoTimer2 is the solution.

PaulS:
These unsupported statements do not work here. There is no real reason why ServoTimer2 can not be used with versions of the IDE after 1.5.8.
You said that. You provided NO proof of that statement. WHY can’t you use them at the same time? If the problem is that they both use timer 1, then using ServoTimer2 is the solution.

So What should I do?

When I upload the program, it show that:

Arduino: 1.5.8 (Windows 7), Board: “Arduino Uno”

ServoTimer2\ServoTimer2.cpp.o: In function ServoTimer2::ServoTimer2()': C:\Users\MY1609\Documents\Arduino\libraries\ServoTimer2/ServoTimer2.cpp:25: multiple definition of __vector_9’
sketch_jul28a.cpp.o:/arduino/sketch_jul28a.ino:124: first defined here
collect2: error: ld returned 1 exit status
Error compiling.

This report would have more information with
“Show verbose output during compilation”
enabled in File > Preferences.

So, you can't use ServoTimer2 because that uses timer 2 and apparently the wave shield uses that timer.

So, why can't you use Servo?

So What should I do?

Get a Mega, with 5 timers.

PaulS:
So, you can't use ServoTimer2 because that uses timer 2 and apparently the wave shield uses that timer.

So, why can't you use Servo?
Get a Mega, with 5 timers.

You means that if I use Timer1 the problem will be solved?

You means that if I use Timer1 the problem will be solved?

I don't know, since I don't know what your problem is.

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...

Do you see anything in there that looks useful for sorting out your problem? I don't either.

PaulS:
I don't know, since I don't know what your problem is.
Do you see anything in there that looks useful for sorting out your problem? I don't either.

The wave shield and servo can work separately. If the code is only about servo or wave, it can run. But I do not know how to write a code to combine both of them.

But I do not know how to write a code to combine both of them.

You don't seem to be able to define any requirements, either.

PaulS:
You don't seem to be able to define any requirements, either.

Sorry... Maybe I should find more information about Arduino.