Tone library + accel hang

Guys,

I have a lilypad with the standard accelerometer and a buzzer. I can read the accel just fine but if I use the tone library as well I can read the accel three times, make three tones, and then it all just hangs. Is this some wierd interrupt thing?

Code follows … thx in advance.

#include <Tone.h>
#include <bassdll.h>
#include <debug.h>

#include <stdlib.h> // for malloc and free
void* operator new(size_t size) { return malloc(size); }
void operator delete(void* ptr) { free(ptr); }

//
// Ardy Jacket
//
// leds, buzzers (left/right), vibrator, acelerometer
//
//

const int groundPin = 18; // analog input pin 4 – ground
const int powerPin = 19; // analog input pin 5 – voltage
const int xPin = 16; // x-axis of the accelerometer ANALOG
const int yPin = 17; // y-axis ANALOG
const int zPin = 18; // z-axis (only on 3-axis models) ANALOG

int ledPins[4];
int rightBuzzerPin = 6;
int leftBuzzerPin = 7;
int vibratorPin = 8;
//int x,y,z;

void setup() {

Serial.begin(9600);

// Setup led pins
for (int count = 0; count < 4; count++) {
ledPins[count] = 2 + count;
pinMode(ledPins[count], OUTPUT);
}

pinMode(leftBuzzerPin, OUTPUT);
pinMode(rightBuzzerPin, OUTPUT);
pinMode(vibratorPin, OUTPUT);

pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(zPin, INPUT);
}

// Read the accelerometer.
void readAccel(int &x, int &y, int &z) {

x = analogRead(xPin);
y = analogRead(yPin);
z = analogRead(zPin);

}

Tone tone1;
Tone tone2;

void loop() {

int x = 0, y = 0, z = 0;
// Read the accelerometer
readAccel(x,y,z);

char buffer[56];
sprintf(buffer, “(x,y,z) = (%d,%d,%d)”, x, y, z);

Serial.println(buffer);

sprintf(buffer, “(x,y,z) = (%d)”, NOTE_A4 + x / 100);

Serial.println(buffer);

tone1.begin(rightBuzzerPin);
tone1.play(NOTE_A4,100 );

// tone2.begin(leftBuzzerPin);
// tone2.play(NOTE_A4);

delay(800);

}

 pinMode(xPin, INPUT); 
 pinMode(yPin, INPUT); 
 pinMode(zPin, INPUT);

Not necessary - they're analogue pins, best change the numbering to match.

I'd probably dump the "sprintf"s too.

Please use the # key when posting code.

Ok - will do.

The sprintf’s are a good buffer over-run candidate but would they affect timing here or something?

How are you powering the setup ?

It sounds like maybee your powersupply can not deliver enough current ??

sprintf looks OK for buffer size, but I just don't like them! (Well, not on an Arduino, anyway) [edit]Just a moment - in the sketch above, the declarations of x,y and z are commented out - how does that work? Are you sure you've posted your latest code?[/edit]

The x,y,z reappear in the loop.

I tried powering off an external battery. Very good idea that - you never really know if you're sending enough current. However, vene then we get three beeps and it stops. Same as before.

This makes no real sense to me.

Ok - I simplified this and the issue is the tone library

I still get three beeps for the following

void loop() {

  // Read the accelerometer 
//  readAccel();
  Tone tone1;
  tone1.begin(rightBuzzerPin);
  tone1.play(NOTE_A4,550);
  
 // tone2.begin(leftBuzzerPin);
 // tone2.play(NOTE_A4);

  delay(600);

}

Oh, and I did think that the Tone object was getting destroyed so I put it as a global and it made no difference.

Got it. People who call Tone::begin repeatedly in the loop need their head examined.