Speaker then Motor

I'm trying to run several operations in series. After being triggered, I want to run a speaker, then a servo, then turn on an LED.
My sketch works fine EXCEPT it will not play the WAV file and run the servo in sequence. If everything is connected, it will play the WAV, and turn on the LED after the correct delays (so it is going through the servo loop) but will not run the servo in between.

If I comment out the WAV line, the servo works fine, even without disconnecting the speaker. I tried connecting the servo to a separate power supply and the result is the same.

Any ideas why this would be?

#include <Servo.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10  //using digital pin 10 on arduino nano 328
#include <ezOutput.h>
#include <SPI.h>
#include <SD.h>
File myFile;

int led_pin = 3;
int red_pin = 6;
int eye_pin = 5;            //Eye lights
int Button = 2;
int buttonState = 0;         // variable for reading the pushbutton status
unsigned long pushtime = 5000;
unsigned long initial = 0;
unsigned long current = 0;
unsigned long duration = 0;
int A = 2;

TMRpcm tmrpcm;   // create a speaker object for use in this sketch

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0;  // variable to store the servo position

void setup() {
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
  pinMode(led_pin, OUTPUT); // Declare the LED as an output
  pinMode(red_pin, OUTPUT); // Declare the LED as an output
  pinMode(eye_pin, OUTPUT); // Declare the LED as an output
  pinMode(Button, INPUT);
  Serial.begin(9600);       //print rate

  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc

  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized:
    return;   // don't do anything more if not
  }
  tmrpcm.volume(10);
  A = 1;
}

void loop() {
  buttonState = digitalRead(Button); // read the state of the pushbutton value:
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (A != 2) {
    digitalWrite(eye_pin, LOW);
  }

  if (buttonState == HIGH) {
    initial = millis();
    duration = 0;
    tmrpcm.play("service.wav");
    delay(1000);                    //Pause for 1 second to make sure the button is not still pushed

    while (duration < pushtime) {       //During the Pushtime duration after the trigger:
      buttonState = digitalRead(Button);     // read the state of the pushbutton value:
      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:

      duration = (millis() - initial); //time since button was first pushed
      if (buttonState == HIGH) {      //If the button is pushed a 2nd time
        delay(100);

        if (A == 2) {                    //If status was DOWN (A=2)
          digitalWrite(eye_pin, LOW);
          A = 1;
          for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(15);
          }
          //Serial.println("RAISED");        //status
        }

        else {                           //If status was UP (A=1)
          A = 2;
          for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
            // in steps of 1 degree
            myservo.write(pos);              // tell servo to go to position in variable 'pos'
            delay(15);                       // waits 15ms for the servo to reach the position
          }
          //Serial.println("LOWERED");     //status
          //      delay(500);
          digitalWrite(eye_pin, HIGH);
        }
      }

      Serial.println(duration);       //time record
      delay(100);
    }
  }
}

You forgot to state which Arduino you are using.

For the Uno and similar, there are TIMER1 conflicts between the servo library and the TMRPCM library.

Either use a Mega, or use the Servotimer2 library for the servo.

This code blocks all things outside the while lop including the sound call a few lines above.

I'm using an Uno, hoping to use a Nano. Servotimer2 works, just have to figure out the pulse width for the correct positions.

Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.