How do I change the Halloween Pumpkin project from sensor to

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
garret
 
Posts: 5
Joined: Mon Oct 13, 2014 4:14 am

How do I change the Halloween Pumpkin project from sensor to

Post by garret »

Hi, I am following along in the Halloween Pumpkin project, and adapting it to my sons Halloween costume. What I would like to do is replace the sensors in the original project, with a button that my son can press when needed, to play the sounds and turn on LEDs.

Are there any examples of code from other projects? Or does anyone know how I can alter the code from the halloween pumpkin project to fit my button idea?

Thank you.
Garret

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

Re: How do I change the Halloween Pumpkin project from senso

Post by adafruit_support_bill »

The pumpkin project code has 5 different actions responding to 5 distance ranges from the sensor. Did you want to use multiple buttons, or simplify the code to have just one action?

User avatar
garret
 
Posts: 5
Joined: Mon Oct 13, 2014 4:14 am

Re: How do I change the Halloween Pumpkin project from senso

Post by garret »

Yes, Im looking to simplify the action, so that one button push, both plays a wav file and turns on the LEDs. Sorry if this is a simple question, Im new to arduino.

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

Re: How do I change the Halloween Pumpkin project from senso

Post by adafruit_support_bill »

The first step is to get the button wired up. Lesson 6 covers wiring and coding for buttons: https://learn.adafruit.com/adafruit-ard ... tal-inputs

You can eliminate the code in the loop() Just add a test for the button press and play a sound.

This code should turn on the leds and play one of 4 files at random. You can expand on that if you want.

Code: Select all

void loop()
{
    if (digitalRead(buttonPin) == LOW) // button press
    {
        digitalWrite(eyeleds, HIGH); 

        int i = random(4); // select a random track to play
        switch(i)
        {
            case 0;
                  playcomplete("HPYHALWN.WAV");
                  break;
            case 1;
                  playcomplete("SCREECH.WAV");
                  break;
            case 2;
                  playcomplete("GHOULLAF.WAV");
                  break;
            case 3;
                  playcomplete("BOOHAHA.WAV");
                  break;
        }
        digitalWrite(eyeleds, LOW); 
    }
}

User avatar
garret
 
Posts: 5
Joined: Mon Oct 13, 2014 4:14 am

Re: How do I change the Halloween Pumpkin project from senso

Post by garret »

Thank you very much for this. I will give it a shot and let you know how it goes.

User avatar
garret
 
Posts: 5
Joined: Mon Oct 13, 2014 4:14 am

Re: How do I change the Halloween Pumpkin project from senso

Post by garret »

So I have played around with the code and came up with this:

Code: Select all


#include <WaveHC.h>
#include <WaveUtil.h>

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 volumes root directory
FatReader file;   // This object represent the WAV file 
WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time


#define playcomplete(x) ROM_playcomplete(PSTR(x))         // save RAM by using program memory strings

int buttonApin = 9;
//#define servo 7
//#define redled 9
#define eyeleds 18
//#define mouthleds 17
//#define midmouthleds 16
//#define outermouthleds 19

void setup() 
{
  pinMode(buttonApin, INPUT_PULLUP);  
}
 
void loop() 
{
    if (digitalRead(buttonApin) == LOW) // button press
    {
        digitalWrite(eyeleds, HIGH); 

        int i = random(6); // select a random track to play
        switch(i)
        {
            case 0:
                  playcomplete("TREX1.WAV");
                  break;
            case 1:
                  playcomplete("TREX2.WAV");
                  break;
            case 2:
                  playcomplete("TREX3.WAV");
                  break;
            case 3:
                  playcomplete("TREX4.WAV");
                  break;
            case 4:
                  playcomplete("TREX5.WAV");
                  break;
            case 5:
                  playcomplete("TREX6.WAV");
                  break;
        }
        digitalWrite(eyeleds, LOW); 
    }

  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println(F("Wave test!"));

  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  //pinMode(redled, OUTPUT);
  //pinMode(servo, OUTPUT);
  pinMode(eyeleds, OUTPUT);
  //pinMode(outermouthleds, OUTPUT);
  //pinMode(midmouthleds, OUTPUT);
  //pinMode(mouthleds, OUTPUT);
  
  randomSeed(analogRead(0));


  if (!card.init()) {
    Serial.println(F("Card init. failed!")); return;
  }
  // 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  :(
    Serial.println(F("No valid FAT partition!"));  // Something went wrong, lets print out why
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  Serial.print(F(", type is FAT"));
  Serial.println(vol.fatType(), DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    Serial.println(F("Can't open root dir!"));      // Something went wrong,
  }
  
  // Whew! We got past the tough parts.
  Serial.println(F("Files found (* = fragmented):"));

  // Print out all of the files in all the directories.
  root.ls(LS_R | LS_FLAG_FRAGMENTED);
}


void pulseServo(uint8_t servopin, uint16_t p) {
  
 digitalWrite(servopin, HIGH);
 delayMicroseconds(600);
 while (p--) {
   delayMicroseconds(4);
 }
 digitalWrite(servopin, LOW);
  delay(18);
}

uint8_t pumpkinstate = 0;



void ROM_playcomplete(const char *romname) {
  char name[13], i;
  uint8_t volume;
  int v2;
  
  for (i=0; i<13; i++) {
    name[i] = pgm_read_byte(&romname[i]);
  }
  name[12] = 0;
  Serial.println(name);
  playfile(name);
  while (wave.isplaying) {
   volume = 0;
   for (i=0; i<8; i++) {
     v2 = analogRead(1) - 512;
     if (v2 < 0) 
        v2 *= -1;
     if (v2 > volume)
       volume = v2;
     delay(5);
   }
  file.close();
}
}

void playfile(char *name) {

   if (!file.open(root, name)) {
      Serial.println(F(" Couldn't open file")); return;
   }
   if (!wave.create(file)) {
     Serial.println(F(" Not a valid WAV")); return;
   }
   // ok time to play!
   wave.play();
}
As you can see I swapped out the old sensor code for what you suggested, and fumbled through getting it to upload. Im sure its not 100% correct, and that is probably why Im getting weird behavior.

So, after successfully uploading the code, and testing the button. It will only play a few times before failing to play. Also since im only testing it, rather than soldering the button connections I am simply touching the wires from the bread board with a button setup to the ground and #9 pin on the WaveShield, and it plays without pressing the button.

Im sure there are a million things I did wrong. Ive attached a photo so that you can see how ive set things up. Hopefully you can see where Ive gone wrong.

Thanks again for your help,

Garret
Attachments
IMG_20141023_233421.jpg
IMG_20141023_233421.jpg (181.55 KiB) Viewed 336 times

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

Re: How do I change the Halloween Pumpkin project from senso

Post by adafruit_support_bill »

You have all your card initialization at the end of your loop. It should be in the setup as in the tutorial code.

User avatar
garret
 
Posts: 5
Joined: Mon Oct 13, 2014 4:14 am

Re: How do I change the Halloween Pumpkin project from senso

Post by garret »

After switching the order of the code its all working now. Thank you very much!!!

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

Re: How do I change the Halloween Pumpkin project from senso

Post by adafruit_support_bill »

Good to hear. Hope your son has a good Halloween!

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

Return to “Arduino”