Need help to play a file whenever a button is pressed via the arduino

edited July 2014 in Arduino

Arduino Sketch : int switchPin = 8; int ledPin = 13;

                void setup()
                {
                  Serial.begin(9600);
                  pinMode(switchPin, INPUT);
                  pinMode(ledPin, OUTPUT);
                }

                void loop()
                {
                  if (digitalRead(switchPin) == HIGH)
                  {
                    digitalWrite(ledPin, HIGH);
                    Serial.println("1");
                    delay(300);
                  }
                  else
                  {
                    digitalWrite(ledPin, LOW);
                  }
                }

Processing Sketch:

    import ddf.minim.*;
    import processing.serial.*;

    Serial port;
    float value = 0;

    Minim minim;
    AudioPlayer player;

    void setup()
    {
      size(512, 200, P3D);

      // we pass this to Minim so that it can load files from the data directory
      minim = new Minim(this);

      // loadFile will look in all the same places as loadImage does.
      // this means you can find files that are in the data folder and the 
      // sketch folder. you can also pass an absolute path, or a URL.


      // play the file from start to finish.
      // if you want to play the file again, 
      // you need to call rewind() first.
      player = minim.loadFile("C:/Users/home/Desktop/go-travel.wav");

      port = new Serial(this, "COM3", 9600);
      port.bufferUntil('\n');
    }

    void draw()
    {
      background(10,20,10);
      if (value == 1)
      {
        player.play();
        delay(300);
      }

    }

    void serialEvent (Serial port)
    {
      value = float(port.readStringUntil('\n'));
    }

When I press the button for the first time, the file plays correctly, but after that no matter how many times I press the button, the file does not play, what should I do? Using the rewind function does not help either. What I want to do is when I press the push button connected to the arduino, it sends the number 1 via serial which is read by processing and then processing opens a file which plays a sound. When I press the button for the first time it works great but then when I press it again the sound does not play its as if the void draw function just acts as void setup and thats it. What should I do to edit the code so that it works?

Thanks

Answers

  • Is serialEvent called again when you release the button, setting the value back to zero? If not, reset it to zero in draw(), in the condition.

    Using println() at the critical locations can help understanding what are the values at these locations.

  • The reason can be too small delay value (it was in my case). Try a delay value bigger than 1 second which is delay(1000) or just use the frameRate() in void setup; instead of delay() function.

  • edited July 2014

    Hello!

    I got a new arduino UNO now.

    Can you describe the hardware site of this?

    I mean is the arduino connected to the PC via usb? If not: which pins do you use?

    where is the button to the arduino connected? Is it connected with 2 wires? Where do they got? To which pins?

    Thanks!

    I am total noob with arduino...

    Chrisir ;-)

  • Yes, the Arduino hooks to the PC over USB. A button would be connected with two wires, one to ground and one to one of the Dnn sockets. The simplest way to use Arduino is to install the Firmata program from the Arduino IDE's examples menu. (Use StandardFirmata). You then use the Processing Arduino library to do the basic communication. There's good information at the Libraries page for Arduino. The book http://shop.oreilly.com/product/0636920021414.do is well worth while at $10 for the ebook edition.

  • thanks a lot!

    ;-)

  • sorry I can't find Dnn sockets. -

    what do they look like? Other possible text written on it?

    can you post an image?

    Thanks...

  • The row of sockets along one edge labelled D0-D13. Ah, I see. On the Uno they're labelled as Digital Pins and just the numbers, just above the words Arduino and Uno. (I have older models.) There's a pretty good set of examples at http://arduino.cc/en/Tutorial/HomePage .

  • thanks!!

Sign In or Register to comment.