Hi,
I'm building a 'robot bird' project. The robot moves, using an Sg-90 servo, using the Servo.h library, on D5, and makes an audible birdcall. To do the latter I am reading a wav file from an SD reader and then playing it via D9 which connects to a powered speaker using the TMRpcm library. Both the routines work fine alone, but when I try to use both the servo routine fails (it just does nothing). I'm probably doing something stupid and would be grateful for any help.
Best wishes
Chris.
This is the code: (if I comment out the tmrpcm audio calls then the servo works, but when I uncomment them the audio works and the servo does not).
#include <Servo.h>
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
#define SD_ChipSelectPin 10 //connect pin 10 of arduino to cs pin of sd card
int servoPin = 5; //PWM pin that is connected to the servo
int speaker = 9;
int crowData = 0;
int servoAngle = 0;
Servo Servo1;
TMRpcm tmrpcm;
void setup()
{
Servo1.attach(servoPin); //create a servo object
digitalWrite(2, HIGH); // sets LED Green eye ON
Servo1.write(0); //move servo to zero angle
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
{
Serial.println("SD fail");
return;
};
tmrpcm.setVolume(6); //
}
// End of setup
// Main program loop
void loop() {
if (random(1,80) == 1)
{
birdMove();
}
if (random(1,8) == 1)
{
birdblink();
}
if (random(1,50) == 1)
{
birdcall();
}
delay(1000);
}
// End of main program
void birdMove()
{
// Some code to ensure servo is moving - I'll code the real movement later.
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
}
void birdblink()
{
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
delay(500);
}
void birdcall()
{
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
tmrpcm.setVolume(6); //
tmrpcm.play("crow1.wav");
delay(1000);
digitalWrite(3, LOW);
digitalWrite(2, HIGH);
}