Trouble with Servo Breakout & Wave Shield.
Moderators: adafruit_support_bill, adafruit

Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Sun Jan 13, 2013 6:40 pm

Hey,

Yes, I'm a noob. I'm trying to wing a project, and, well, it was going well until now. I'm attempting to control a few servos along with a wave shield. The wave shield - on it's own - was doing just fine, registering button pushes correctly, finding the .wav file perfectly, it was peachy. Now that I'm trying to patch my servos into the system, it's all gone horribly wrong. Can't find the file, and just plain won't function. Help!

Thanks very much in advance.

Code: Select all | TOGGLE FULL SIZE
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servomin/ Servomax
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

// Button Stuff
int buttonLevel = digitalRead(14); //current button signal.
unsigned long now = millis(); //Current relative time in msecs.
int _state = 0; // Starting with state 0: waiting for button.

// Waveshield stuff
SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

#define DEBOUNCE 100  // button debouncer

// this handy function will return the number of bytes currently free in RAM, great for 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;
}

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() {
  // set up serial port
  Serial.begin(9600);
  putstring_nl("Fortune Teller");
 
   putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
 
  // pin13 LED
  pinMode(13, OUTPUT);
 
  // More Servo Stuff
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
}
  void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
 
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period");
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit");
  pulse *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
 
   // enable pull-up resistors on switch pins (analog inputs)
  digitalWrite(14, HIGH);    // analog pin 0 (Normal)
  digitalWrite(15, HIGH);   // analog pin 1 (Wireless)
 
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  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() {
  //putstring(".");            // uncomment this to see if the loop isnt running
  switch (check_switches()) {
    case 1:
      playcomplete("LAUGHT~1.WAV");
    if (_state == 0) {
    _state = 1; // step to state 1
    putstring_nl("Whoa, we moved RIGHT!");
  } else if (_state ==1) {
    _state = 0; // Step to state 0
    putstring_nl("Whoa, we moved LEFT!");
      break;
case 2:
      playcomplete("LAUGHT~1.WAV");
    uint16_t pulselength = map(10, 0, 180, SERVOMIN, SERVOMAX);
  delay(500);
      break;
  }
  }}
byte check_switches()
{
  static byte previous[6];
  static long time[6];
  byte reading;
  byte pressed;
  byte index;
  pressed = 0;

  for (byte index = 0; index < 6; ++index) {
    reading = digitalRead(14 + index);
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch pressed
      time[index] = millis();
      pressed = index + 1;
      break;
    }
    previous[index] = reading;
  }
  // return switch number (1 - 6)
  return (pressed);
}


// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(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();
}
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_rick on Sun Jan 13, 2013 7:17 pm

Well, you've got quite a few '}' in the wrong places. For instance, when you inserted the function
Code: Select all | TOGGLE FULL SIZE
  void setServoPulse(uint8_t n, double pulse) {

You cut the function setup() in half. it now ends at the '}' right before setServoPulse, and so the part of setup() that initializes the card reader is never called.

The matching '}' for this
Code: Select all | TOGGLE FULL SIZE
  } else if (_state ==1) {
is the brace that should be the end of the switch statement, so you've cut the switch statement in half as well. It looks like you've added a couple of '}}' after the switch to try to balance things out - that has made it compile, but has left the logic horribly mutilated.

Honestly, I'm rather surprised that it does compile successfully.

In the Arduino IDE, you can easily check the matching braces by placing the cursor immediately to the right of either and opening or closing brace. The IDE will then draw a little box around the matching brace. You need to review your code to make sure that the matching braces correctly surround the right chunks of code.

For simplicity, I think you want to start over with a fresh copy of the wave shield code you started with. Then, carefully merge in the servo code, checking as you go that you haven't inserted any unmatched braces.

adafruit_support_rick
 
Posts: 13051
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Mon Jan 14, 2013 12:41 pm

horribly mutilated.


Sounds like something I would do. :oops: Thanks for the help - I'll try and clean it up and see if it works. This is my first attempt at programming an Arduino, and I suppose it's showing. Thanks again!
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_rick on Mon Jan 14, 2013 1:45 pm

unorthodoxarts wrote:Sounds like something I would do.

Sorry - didn't mean it that way. Believe me, I've mutilated my fair share of logic... :roll:

Just think of curly braces as containers.

adafruit_support_rick
 
Posts: 13051
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Tue Jan 15, 2013 7:44 pm

Oh no worries! I'm a props person trying to be a programmer. I am very used to breaking things. :)

Unfortunately I need to call on your amazing help yet again. I can't seem to control the servo to save my life. It's compiling, but it won't actually move the blasted servo. I'm sure that I'm not addressing the servo correctly.

Are we allowed to mail you people fruit baskets? :P

Code: Select all | TOGGLE FULL SIZE
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servomin/ Servomax
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

// Button Stuff
int buttonLevel = digitalRead(14); //current button signal.
unsigned long now = millis(); //Current relative time in msecs.
int _state = 0; // Starting with state 0: waiting for button.

// Waveshield stuff
SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

#define DEBOUNCE 100  // button debouncer

// this handy function will return the number of bytes currently free in RAM, great for 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;
}

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() {
  // set up serial port
  Serial.begin(9600);
  putstring_nl("Fortune Teller");
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // pin13 LED
  pinMode(13, OUTPUT);
 
  // More Servo Stuff
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

   // enable pull-up resistors on switch pins (analog inputs)
  digitalWrite(14, HIGH);    // analog pin 0 (Normal)
  digitalWrite(15, HIGH);   // analog pin 1 (Wireless)

  //  if (!card.init(true)) //play with 4 MHz spi if 8MHz isn't working for you
  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 setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period");
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit");
  pulse *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
  }
void loop() {
  //putstring(".");            // uncomment this to see if the loop isnt running
  switch (check_switches()) {
    case 1:
      playcomplete("LAUGHT~1.WAV");
    if (_state == 0) {
    _state = 1; // step to state 1
    putstring_nl("Whoa, we moved RIGHT!"); }
  else if (_state ==1) {
    _state = 0; // Step to state 0
    putstring_nl("Whoa, we moved LEFT!");}
      break;
case 2:
      playcomplete("LAUGHT~1.WAV");
      putstring_nl("He did what?!");
  uint16_t pulseLen = map (5, 0, 180, SERVOMIN, SERVOMAX);
  pwm.setPWM (0, 0, pulseLen);
  delay(500);
      break;
  }
}

byte check_switches()
{
  static byte previous[6];
  static long time[6];
  byte reading;
  byte pressed;
  byte index;
  pressed = 0;

  for (byte index = 0; index < 6; ++index) {
    reading = digitalRead(14 + index);
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch pressed
      time[index] = millis();
      pressed = index + 1;
      break;
    }
    previous[index] = reading;
  }
  // return switch number (1 - 6)
  return (pressed);
}


// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(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();
}
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_rick on Wed Jan 16, 2013 11:37 am

Hmmm… You've got the servo attached to output 0, right? Did you do anything with the A0-A5 jumpers, or are they all still open? Have you attached a separate power supply to V+? (The servo won't run off Vcc from the microcontroller).
Have you tried values larger than 5 in this:?
uint16_t pulseLen = map (5, 0, 180, SERVOMIN, SERVOMAX);

Have you tried servo outputs other than 0?

Could be a soldering issue - post clear, detailed pictures (use the Upload Attachment tab - 600x800 jpg or png work well) and we'll take a look.

unorthodoxarts wrote:Are we allowed to mail you people fruit baskets?

Well, we prefer Macallan 18, but fruit is acceptable! :lol:

adafruit_support_rick
 
Posts: 13051
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Wed Jan 16, 2013 5:36 pm

Man, you guys drink classy. :P

What I failed to mention - and I apologize profusely for neglecting to mention this - is that when I run servo.pde (the test file that's included with the library,) the servo sweeps back and forth perfectly. I am still at the sad 'day job' so I don't have photos immediately - I'll take them when I get home. But I have a separate power supply and the servo does its little boot-up jolt when it's turned on, and I am OCD when it comes to following the assembly instructions.

In short, I'm being an idiot, and I don't understand what the code actually is with the adafruit library to simply say: "This servo go here now please." would: pwm.setPWM(0,0, pulselen=map(5,0,180,SERVOMIN,SERVOMAX); work? Everything I've tried to cut-and-paste from the servo.pde file just gives me awful errors.
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_bill on Wed Jan 16, 2013 5:51 pm

would: pwm.setPWM(0,0, pulselen=map(5,0,180,SERVOMIN,SERVOMAX); work?

Yes. It will move the servo to about the 5-degree position. If you want to move it somewhere else you need to use a number other than 5.

In the servo example, the pulselen value changes in the 'for' loop to make it sweep from SERVOMIN to SERVOMAX.

Code: Select all | TOGGLE FULL SIZE
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
    pwm.setPWM(servonum, 0, pulselen);
  }
User avatar
adafruit_support_bill
 
Posts: 31025
Joined: Sat Feb 07, 2009 10:11 am

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Wed Jan 16, 2013 7:18 pm

It's giving me an error saying that 'pulselen' was not declared in this scope:
Code: Select all | TOGGLE FULL SIZE
void loop() {
  //putstring(".");            // uncomment this to see if the loop isnt running
  switch (check_switches()) {
    case 1:
      playcomplete("LAUGHT~1.WAV");
    if (_state == 0) {
    _state = 1; // step to state 1
    putstring_nl("Whoa, we moved RIGHT!"); }
  else if (_state ==1) {
    _state = 0; // Step to state 0
    putstring_nl("Whoa, we moved LEFT!");}
      break;
case 2:
      playcomplete("LAUGHT~1.WAV");
      putstring_nl("He did what?!");
 pwm.setPWM(0,0, pulselen=map(5,0,180,SERVOMIN,SERVOMAX);
  delay(500);
      break;
  }
}
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_bill on Wed Jan 16, 2013 7:57 pm

We can't diagnose compile problems from the fragment you posted. You would need to post the entire sketch.

It would help even more if you told us exactly what you were trying to do.
User avatar
adafruit_support_bill
 
Posts: 31025
Joined: Sat Feb 07, 2009 10:11 am

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Wed Jan 16, 2013 8:00 pm

Sorry about that! I had posted the whole sketch earlier, but, here it is:

Code: Select all | TOGGLE FULL SIZE
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servomin/ Servomax
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

// Button Stuff
int buttonLevel = digitalRead(14); //current button signal.
unsigned long now = millis(); //Current relative time in msecs.
int _state = 0; // Starting with state 0: waiting for button.

// Waveshield stuff
SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

#define DEBOUNCE 100  // button debouncer

// this handy function will return the number of bytes currently free in RAM, great for 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;
}

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() {
  // set up serial port
  Serial.begin(9600);
  putstring_nl("Fortune Teller");
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // pin13 LED
  pinMode(13, OUTPUT);
 
  // More Servo Stuff
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

   // enable pull-up resistors on switch pins (analog inputs)
  digitalWrite(14, HIGH);    // analog pin 0 (Normal)
  digitalWrite(15, HIGH);   // analog pin 1 (Wireless)

  //  if (!card.init(true)) //play with 4 MHz spi if 8MHz isn't working for you
  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 setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period");
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit");
  pulse *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
  }
void loop() {
  //putstring(".");            // uncomment this to see if the loop isnt running
  switch (check_switches()) {
    case 1:
      playcomplete("LAUGHT~1.WAV");
    if (_state == 0) {
    _state = 1; // step to state 1
    putstring_nl("Whoa, we moved RIGHT!"); }
  else if (_state ==1) {
    _state = 0; // Step to state 0
    putstring_nl("Whoa, we moved LEFT!");}
      break;
case 2:
      playcomplete("LAUGHT~1.WAV");
      putstring_nl("He did what?!");
      pwm.setPWM(0,0, pulselen= map (5,0,180,SERVOMIN,SERVOMAX);
  delay(500);
      break;
  }
}

byte check_switches()
{
  static byte previous[6];
  static long time[6];
  byte reading;
  byte pressed;
  byte index;
  pressed = 0;

  for (byte index = 0; index < 6; ++index) {
    reading = digitalRead(14 + index);
    if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
    {
      // switch pressed
      time[index] = millis();
      pressed = index + 1;
      break;
    }
    previous[index] = reading;
  }
  // return switch number (1 - 6)
  return (pressed);
}


// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(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();
}


What I'm trying to do is build a fortune teller. The servo controls for the case 1 of the button push isn't programmed in yet. I'm just trying to get the code figured out before I put it in everywhere I need it. Case 2 has the servo movement, where when someone triggers case 2, (a remote control,) the sound effect plays, and then the servo moves. Case 1 will also have another servo that moves, hooked up to a different slot on the break out board.

Thank you!
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_bill on Wed Jan 16, 2013 8:07 pm

and then the servo moves.

Where is it moving from? And where does it move to? And does it need to go back to where it came from?
User avatar
adafruit_support_bill
 
Posts: 31025
Joined: Sat Feb 07, 2009 10:11 am

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Wed Jan 16, 2013 8:52 pm

The servo should be moving from point zero - then move ten degrees 'out of the way' and then the item it was holding in place (a card) drops into a tray.

There are two other servos that are yet to be included into the programming - so three servos total. Servo 2 moves back and forth in every instance (the arm of the fortune teller moves back and forth each time you push the button.) Servo 1 is only triggered in case 1, and in state 0 of case 1 it moves to 160 degrees. In state 1 of case 1 it moves back to 0. (that servo rotates a special 'card vending machine' that I built, and it needs to move back and forth in order to drop cards out. The state changes are so it knows which direction to go from where it was.) In theory, it would look like:

Code: Select all | TOGGLE FULL SIZE
void loop() {
  //putstring(".");            // uncomment this to see if the loop isnt running
  switch (check_switches()) {
    case 1:
      playcomplete("LAUGHT~1.WAV");
    if (_state == 0) {
    _state = 1; // step to state 1
    putstring_nl("Whoa, we moved RIGHT!");
    pwm.setPWM(2,0, pulselen= map (120,0,180,SERVOMIN,SERVOMAX);
    delay(100);
    pwm.setPWM(2,0, pulselen= map (0,0,180,SERVOMIN,SERVOMAX);
    delay(100);
    pwm.setPWM(1,0, pulselen= map (160,0,180,SERVOMIN,SERVOMAX);
    delay(400);
break;
}
   
  else if (_state ==1) {
    _state = 0; // Step to state 0
    putstring_nl("Whoa, we moved LEFT!");
 pwm.setPWM(2,0, pulselen= map (120,0,180,SERVOMIN,SERVOMAX);
    delay(100);
    pwm.setPWM(2,0, pulselen= map (0,0,180,SERVOMIN,SERVOMAX);
    delay(100);
    pwm.setPWM(1,0, pulselen= map (0,0,180,SERVOMIN,SERVOMAX);
    delay(400);
break;
}
   
case 2:
      playcomplete("LAUGHT~1.WAV");
      putstring_nl("He did what?!");
    pwm.setPWM(2,0, pulselen= map (120,0,180,SERVOMIN,SERVOMAX);
    delay(100);
    pwm.setPWM(2,0, pulselen= map (0,0,180,SERVOMIN,SERVOMAX);
    delay(100);
      pwm.setPWM(0,0, pulselen= map (5,0,180,SERVOMIN,SERVOMAX);
  delay(500);
      break;
  }
}


But I was trying to get one thing working and then untangle the code from there, as opposed to breaking it all at once, y'know?
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm

Re: Trouble with Servo Breakout & Wave Shield.

by adafruit_support_bill on Wed Jan 16, 2013 9:02 pm

You can get rid of the "pulselen= " part. It is not necessary.

Your previously posted sketch only had one setPWM() statement. So even if it did work, you wouldn't be able to tell.
User avatar
adafruit_support_bill
 
Posts: 31025
Joined: Sat Feb 07, 2009 10:11 am

Re: Trouble with Servo Breakout & Wave Shield.

by unorthodoxarts on Wed Jan 16, 2013 9:54 pm

Oh. Hahah. I see. The 'pulselen' in the pwm.setPWM() thing is a placeholder. Ahhah!

The only thing now, is that the 'delay' after the pwm.setPWM() doesn't control the speed at which the servo moves, but the gap in between them moving. Is there any way to slow down the rate it moves?

Thank you again.
unorthodoxarts
 
Posts: 11
Joined: Fri Dec 28, 2012 2:13 pm