Motor set speed wont work when adding sounds
Moderators: adafruit_support_bill, adafruit

Motor set speed wont work when adding sounds

by amador137 on Fri Jan 25, 2013 8:22 pm

Here is the code:
Code: Select all | TOGGLE FULL SIZE
/*
  Melody
 
 Plays a melody
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h"
 #include <AFMotor.h>
 
AF_DCMotor motor(1);
AF_DCMotor motor2(2);

const int switchoff = 0;

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void setup() {
 

  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(9, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(9);
    if(thisNote == 8){
    break;
    }
   
  }
 
     motor.setSpeed(250);
     motor2.setSpeed(250);
   
   
}

void loop() {
 
  motor.run(FORWARD);
  motor2.run(FORWARD);
}



sounds work but not the motor. I did the motor test example and motor works just fine.So i know the problem is not the motors. The problem is when I add sound the motor wont run at all. It just plays the sound and thats it. What I need is to be able to play the sound/music then move the dc motor. Please help.
amador137
 
Posts: 3
Joined: Tue Apr 03, 2012 8:07 am

Re: Motor set speed wont work when adding sounds

by franklin97355 on Sat Jan 26, 2013 1:21 am

Does it play the sound more than once?
User avatar
franklin97355
 
Posts: 5895
Joined: Mon Apr 21, 2008 2:33 pm

Re: Motor set speed wont work when adding sounds

by adafruit_support_bill on Sat Jan 26, 2013 8:09 am

Try using motors 3 & 4. I'm not sure which timer Melody uses, but I suspect it is the same one used for the motor 1/2 speed control.
User avatar
adafruit_support_bill
 
Posts: 31711
Joined: Sat Feb 07, 2009 10:11 am

Re: Motor set speed wont work when adding sounds

by amador137 on Sat Jan 26, 2013 7:45 pm

okay will do . will post what happens.
amador137
 
Posts: 3
Joined: Tue Apr 03, 2012 8:07 am