Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

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

$
0
0

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!!


Viewing all articles
Browse latest Browse all 747

Trending Articles