Topic: About Wave shield + 2 Servo Problems

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
Tasma
 
Posts: 5
Joined: Sun Jul 10, 2016 10:49 am

Topic: About Wave shield + 2 Servo Problems

Post by Tasma »

Hi everybody, I have a idea to create a box like ''useless box'' but including my sound.So the box will use two servo and one wave shield, one servo is control the door open or close, another servo is a hand to close the switch.

I went online to find the relevant information, I find that using Arduino and Wave Shield can provide a sound. In adafruit website '' https://learn.adafruit.com/adafruit-wav ... o/examples '' It provide some example to teach us how to using the wave shield and I use it to test my wave shield. The wave shield is work.

And than, I also find the servo code about useless box on GitHub and two servo are working.
When I combine the code of servo and the code of wave shield, the problem is coming out. It will have some error...

Late, I know the problem is about the ''Timer 1''. Both program are using timer1, so it will have some confuse. Some people said that using ''ServoTimer2.h'' can solve the problem but I tried so many time the result is.... I cannot solve it.

Is it have some error I missed?

Please to point out or suggest me :smiley-confuse:

The code:

Code: Select all

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

SdReader card;    
FatVolume vol;   
FatReader root;   
FatReader f;      
WaveHC wave;      
#define DEBOUNCE 5  
 #define myPin 12
 ServoTimer2 myservo;
 
byte buttons[] = {9, 8, 7, 17, 18, 19};
#define NUMBUTTONS sizeof(buttons)
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

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; 
} 

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() {
  myservo.attach(myPin);
  byte i;
  
  // set up serial port
  Serial.begin(9600);
  putstring_nl("WaveHC with ");
  Serial.print(NUMBUTTONS, DEC);
  putstring_nl("buttons");
  
  putstring("Free RAM: ");     
  Serial.println(freeRam());      
  

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
 

 

  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons, INPUT);
    digitalWrite(buttons, HIGH);
  }
  
 
  if (!card.init()) {        
    putstring_nl("Card init. failed!"); 
    sdErrorCheck();
    while(1);                          
  }
  
  
  card.partialBlockRead(true);
 

  uint8_t part;
  for (part = 0; part < 5; part++) {     
    if (vol.init(card, part)) 
      break;                            
  }
  if (part == 5) {                      
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();     
    while(1);                          
  }
  
 
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);    
  

  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); 
    while(1);                             
  }
  
  
  putstring_nl("Ready!");
  
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;


  TIMSK2 |= 1<<TOIE2;


}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(buttons[index]);  
   
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
       
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
       
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  
    }
    
    previousstate[index] = currentstate[index];   
  }
}
int incPulse(int val, int inc){
  if( val + inc  > 2000 )
     return 1000 ;
  else
      return val + inc; 
} 

void loop() {
  int val;
 
  val = incPulse( myservo.read(), 1);
  myservo.write(val);
              
    delay(15);
  byte i;
  
  if (justpressed[0]) {
      justpressed[0] = 0;
      playcomplete("0.WAV");
  }
  if (justpressed[1]) {
      justpressed[1] = 0;
      playcomplete("1.WAV");
  }
  if (justpressed[2]) {
      justpressed[2] = 0;
      playcomplete("2.WAV");
  }
  if (justpressed[3]) {
      justpressed[3] = 0;
      playcomplete("0.WAV");
  } 
  if (justpressed[4]) {
      justpressed[4] = 0;
      playcomplete("1.WAV");
  } 
  if (justpressed[5]) {
      justpressed[5] = 0;
      playcomplete("2.WAV");
  }
}




void playcomplete(char *name) {
  
  playfile(name);
  while (wave.isplaying) {
  
  }
  
}

void playfile(char *name) {
 
  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();
}
Last edited by adafruit_support_bill on Thu Jul 28, 2016 10:19 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

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

Re: Topic: About Wave shield + 2 Servo Problems

Post by adafruit_support_bill »

ServoTimer2 is a pretty old library and doesn't seem to be supported by anyone anymore.

The MusicMaker shield might be a better choice for the project, since it does not rely on Arduino timers.
Another option is to use a Servo Shield. That has its own PWM generator on board and does not need Arduino timers either.

User avatar
Tasma
 
Posts: 5
Joined: Sun Jul 10, 2016 10:49 am

Re: Topic: About Wave shield + 2 Servo Problems

Post by Tasma »

adafruit_support_bill wrote:ServoTimer2 is a pretty old library and doesn't seem to be supported by anyone anymore.

The MusicMaker shield might be a better choice for the project, since it does not rely on Arduino timers.
Another option is to use a Servo Shield. That has its own PWM generator on board and does not need Arduino timers either.
THANK YOU VERY MUCH!!! I will try to use MusicMaker shield or Servo Shield. But which one will you prefer? Can you quote the device's name or a photo?

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

Re: Topic: About Wave shield + 2 Servo Problems

Post by adafruit_support_bill »

The Music Maker shield is a good choice for audio projects. It handles a wide variety of file types and the output is in stereo, with much better sound quality than what you can get from the wave shield.
https://learn.adafruit.com/adafruit-mus ... s?view=all

The Servo Shield is ideal if you have lots of servos to control.
https://learn.adafruit.com/adafruit-16- ... s?view=all

For your project, the music-maker is probably the best choice. You can use that and still control your 2 servos using regular Arduino pins.

User avatar
Tasma
 
Posts: 5
Joined: Sun Jul 10, 2016 10:49 am

Re: Topic: About Wave shield + 2 Servo Problems

Post by Tasma »

adafruit_support_bill wrote:The Music Maker shield is a good choice for audio projects. It handles a wide variety of file types and the output is in stereo, with much better sound quality than what you can get from the wave shield.
https://learn.adafruit.com/adafruit-mus ... s?view=all

The Servo Shield is ideal if you have lots of servos to control.
https://learn.adafruit.com/adafruit-16- ... s?view=all

For your project, the music-maker is probably the best choice. You can use that and still control your 2 servos using regular Arduino pins.
You are so nice! I spent a lot of time searching for information about WAVE SHIELD and it cannot have a good way to use. Thanks for your suggestion. I will try it again using music maker shield.
From the website: https://learn.adafruit.com/adafruit-mus ... r?view=all
I cannot find any example to let me learn how to program it. How should I know how to program it?(using the same bottom to change to next music.)

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

Re: Topic: About Wave shield + 2 Servo Problems

Post by adafruit_support_bill »

The library functions are documented here: https://learn.adafruit.com/adafruit-mus ... -reference
How should I know how to program it?(using the same bottom to change to next music.)
Start with the simple audio player sketch: https://learn.adafruit.com/adafruit-mus ... yer-sketch
Once you have that working, make a list of the files you want to play, then post here again and we can help you with that code.

User avatar
Tasma
 
Posts: 5
Joined: Sun Jul 10, 2016 10:49 am

Re: Topic: About Wave shield + 2 Servo Problems

Post by Tasma »

adafruit_support_bill wrote:The library functions are documented here: https://learn.adafruit.com/adafruit-mus ... -reference
How should I know how to program it?(using the same bottom to change to next music.)
Start with the simple audio player sketch: https://learn.adafruit.com/adafruit-mus ... yer-sketch
Once you have that working, make a list of the files you want to play, then post here again and we can help you with that code.
Wow~ Thank you! I will try my best, and see you late XD

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

Return to “Arduino Shields from Adafruit”