We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
sure
post it pls
ah I'm sorry it took me a while hope this is ok!
does it work?
maybe swap line 35/36
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!!
great!