Waveshield + DC Motor

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
shahabned
 
Posts: 8
Joined: Sun Sep 20, 2015 5:54 am

Waveshield + DC Motor

Post by shahabned »

Hi!

Code: Select all

// Adding libraries
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

// Adding objects
SoftwareServo myservo;                        // servo
SdReader card;                                // sccard information
FatVolume vol;                                // sdcard partition
FatReader root;                               // sccard filesystem
FatReader f;                                  // fileinformation playing
WaveHC wave;                                  // audioinformation wave  

// Definitions
#define DEBOUNCE 100
#define ENA 9
#define IN1 8
#define IN2 7

// Servo starting position
int pos = 0;             

// RAm information debugging
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; 
    } 

// Error printing debugging
    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() 
    { 
      pinMode(ENA, OUTPUT);
      pinMode(IN1, OUTPUT);
      pinMode(IN2, OUTPUT); 

      //Wave setup
      Serial.begin(9600);                       // serial port
      putstring_nl("WaveHC with 6 buttons");
      Serial.println(freeRam());            
      pinMode(2, OUTPUT);
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT);
      pinMode(5, OUTPUT);

      //Wave setup Error debugging
      if (!card.init()) {                       //play with 8 MHz spi (default faster!)  
        putstring_nl("Card init. failed!");     // Something went wrong, lets print out why
        sdErrorCheck(); 
        while(1);                               // then 'halt' - do nothing!
      }
      
      // enable optimize read - some cards may timeout. Disable if you're having problems
      card.partialBlockRead(true);
     
    // Now we will look for a FAT partition!
      uint8_t part;
      for (part = 0; part < 5; part++) {        // we have up to 5 slots to look in
        if (vol.init(card, part)) 
          break;                                // we found one, lets bail
      }
      if (part == 5) {                          // if we ended up not finding one  :(
        putstring_nl("No valid FAT partition!");
        sdErrorCheck();      // Something went wrong, lets print out why
        while(1);                               // then 'halt' - do nothing!
      }
      
      // Lets tell the user about what we found
      putstring("Using partition ");
      Serial.print(part, DEC);
      putstring(", type is FAT");
      Serial.println(vol.fatType(),DEC);        // FAT16 or FAT32?
      
                                                // Try to open the root directory
      if (!root.openRoot(vol)) {
        putstring_nl("Can't open root dir!");   // Something went wrong,
        while(1);                               // then 'halt' - do nothing!
      }
      
                                                // Whew! We got past the tough parts.
      putstring_nl("Ready!");
    }
    
void loop() 
{ 
motorA(1, 50);
delay(1000);

motorA(3, 100);
delay(5000);

  playcomplete("1.WAV");

motorA(2, 50);
delay(1000);

}
//Wave Setup Playing Files

void playcomplete(char *name) {
      playfile(name);                           // call our helper to find and play this name
      while (wave.isplaying) {                  // do nothing while its playing
      }
                                                // now its done playing
    }
     
    void playfile(char *name) {
                                                // see if the wave object is currently doing something
      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();
    }

void motorA(int mode, int precent)
{ 
  int duty = map(precent, 0, 100, 0, 255);
  switch(mode)
  { 
    case 0:
    digitalWrite(ENA, LOW);
    break;

    case 1:
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, duty);
    break;

    case 2:
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    analogWrite(ENA, duty);
    break;

    case 3:
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, duty);
    break;
    
  }
}

i try to run this code, what happens is that the dc motor is spinning 1 time, after the playcomplete command in the void loop it stops spinning (the delay is still working) and playcomplete starts again.

i think that something in the playcomplete is disabling the spin from the dc motor but i cant figure out what.

thx. bw

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Waveshield + DC Motor

Post by adafruit_support_bill »

The wave shield uses Timer1 for clocking data to the DAC. Timer 1 is also the timer that controls PWM to pin 9 which you are using for ENA. You will need to choose a different PWM pin for ENA.

User avatar
shahabned
 
Posts: 8
Joined: Sun Sep 20, 2015 5:54 am

Re: Waveshield + DC Motor

Post by shahabned »

awesome, thx you guys are great! it works of course

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”