photosensor triggers sound, how do I stop the sound from looping?

edited August 2015 in Arduino

Hello! I'm writing a code for a Lilypad and one of the inputs I'm hoping to use is a photosensor, which should trigger a sound when the sensor is covered. That bit is all working fine, but the sound is looped and I can't figure out how to stop that? I tried noLoop() but that just plays the sound when the Lilypad is first connected, and nothing happens when the sensor is covered.. This is my code:

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

Minim minim;
Event event_example;
Arduino arduino;

int input_example, led; //the led just gives additional feedback if the the sensor was covered 

void setup() {
  size(470, 280);

  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);

  input_example = 2; //this is the pin the photo resistor is connected to
  led = 6; //this is the pin that the led is connected to

  arduino.pinMode(input_example, Arduino.INPUT);

  minim = new Minim(this);
  event_example= new Event("audio/example.wav");
}

void draw() {

  if (arduino.analogRead(input_example) < 500) {
    event_example.trigger();
    arduino.digitalWrite(led, Arduino.HIGH);
  } else {
    event_example.stop();
    arduino.digitalWrite(led, Arduino.LOW);
  }
}

//The following code is in another tab

public class Event {

  boolean flag;
  AudioSample sample;

  public Event (String file) {
    sample = minim.loadSample(file, 512);
    flag = false;
  }

  void trigger() {
    sample.trigger();
    flag = true;
  }

  void stop() {
    flag = false;
  }
}

Any help is much appreciated!!

Answers

  • do you mean sample or event_example?

  • I think it's because when the file is played already, the sensor starts it again

    look at isPlaying() and start only when isPlaying() is false

    http://code.compartmental.net/minim/audioplayer_method_isplaying.html

  • yep, that was the reason! I've now switched from audioPlayer to audioSample and included a boolean with an if-clause to check if the the sensor has been covered before and so far it's working.. would you be ok looking over my code?

  • post it pls

  • import processing.serial.*;
    import cc.arduino.*;
    import ddf.minim.*;
    
    Minim minim;
    Arduino lilyPad;
    
    AudioPlayer playerDu;
    
    boolean in_schatten;
    int  in_du, led;
    
    void setup () {
      size(600, 600);
      minim = new Minim(this);
      lilyPad = new Arduino (this, Arduino.list()[0], 57600);
    
      in_schatten = false;
    
      in_du = 2;
      led = 10;
    
      playerDu = minim.loadFile("audio/du.wav", 512);
    
      textFont(createFont("Arial", 100));
    }
    
    void draw () {
      background(0);
      stroke(255);
    
      if (lilyPad.analogRead(in_du) < 600 && lilyPad.analogRead(in_du) > 0 && in_schatten == false) {
        println("schatten" + " reading: " + lilyPad.analogRead(in_du));
        in_schatten = true;
        playerDu.play();
        playerDu.rewind();
        lilyPad.digitalWrite(led, Arduino.HIGH);
      } else if (in_schatten == true && lilyPad.analogRead(in_du) > 700) {
        println("licht" + " reading: " + lilyPad.analogRead(in_du));
        in_schatten = false;
        lilyPad.digitalWrite(led, Arduino.LOW);
      } 
      if (lilyPad.analogRead(in_du) < 600 && lilyPad.analogRead(in_du) > 0) {
        text("du", 100, 200);
      }
    }
    
  • ah I'm sorry it took me a while hope this is ok!

  • Answer ✓

    does it work?

    maybe swap line 35/36

  • Answer ✓

    this

    && in_schatten == false

    is

    && ! in_schatten

    this

    == true

    you can leave out, it's not necessary

  • it does! I'll edit the true/false thing! and why would you change those lines? do the two methods imapct each other? I tried it with just play() and just rewind() before and with play() it only played the sound the first time that the sensor was covered, and not again, and with rewind() it didn't play the sound at all..

    also thank you so much for all your help I really appreciate it and probably wouldn't have figured it out otherwise!!

  • Answer ✓

    great!

Sign In or Register to comment.