Laser Harp - Crash and played sound not continuous

edited March 2017 in Arduino

Ive built a laser harp with 5 laser and 5 ldr's and im using the processing Sound library to play the sound. Everything works well on terms of detecting my hands and playing the sounds, the problem is that the sounds are in loop and are playing one on top of the other while my hand remains on the sensors leading to a crash.

What I'd like the harp to do is to simply play the file once and continuously until my hand leaves the sensor area, just like when a finger is touching a piano key. Ive tried this with the minim library but I always get the NullException error while trying to read the sound files.

If you could help me with the code Id be very thankful because this is for a school project and needs to be finished as soon as possible. Do realise Im fairly new to processing and programming.

By the way in the code I was just testing 2 sensors the 4th and the 3rd.

Thanks in advance, Volkun. `import processing.serial.*; import cc.arduino.*; import processing.sound.*;

SoundFile file; Arduino arduino;

int LDR1 = 8; int LDR2 = 9; int LDR3 = 10; int LDR4 = 11; int LDR5 = 12; int Sensor1 = 14; int Sensor2 = 15; int Sensor3 = 16; int Sensor4 = 17; int Sensor5 = 18;

int time; int timeDelay; int ReadLDR1; int ReadLDR2; int ReadLDR3; int ReadLDR4; int ReadLDR5;

void setup() { arduino = new Arduino(this, Arduino.list()[0], 57600); arduino.pinMode(LDR1, Arduino.INPUT); arduino.pinMode(LDR2, Arduino.INPUT); arduino.pinMode(LDR3, Arduino.INPUT); arduino.pinMode(LDR4, Arduino.INPUT); arduino.pinMode(LDR5, Arduino.INPUT); arduino.pinMode(Sensor1, Arduino.INPUT); arduino.pinMode(Sensor2, Arduino.INPUT); arduino.pinMode(Sensor3, Arduino.INPUT); arduino.pinMode(Sensor4, Arduino.INPUT); arduino.pinMode(Sensor5, Arduino.INPUT); time = millis(); timeDelay = 1000; // meio segundo }

void draw() { print(ReadLDR1); print(ReadLDR2); print(ReadLDR3); print(ReadLDR4); println(ReadLDR5);

ReadLDR1 = arduino.digitalRead(LDR1); if (ReadLDR1 == 0) { //arduino.analogRead(Sensor1); //file = new SoundFile(this, "C:\Users\António\Desktop\PAP_Claudio\do1.mp3"); //file.play(); } else if (ReadLDR1 == 1) { //Do nothing
}

ReadLDR2 = arduino.digitalRead(LDR2);
  if (ReadLDR2 == 0)
  {
  //arduino.analogRead(Sensor2);
  //file = new SoundFile(this, "C:\\Users\\António\\Desktop\\PAP_Claudio\\re1.mp3");
 // file.play();
  }
  else if (ReadLDR2 == 1)
  {
  //Do nothing  
  }

ReadLDR3= arduino.digitalRead(LDR3);
  if (ReadLDR3 == 0)
  {
  //arduino.analogRead(Sensor3);
  file = new SoundFile(this, "C:\\Users\\António\\Desktop\\PAP_Claudio\\do1.mp3");
  file.play();
  }
  else if (ReadLDR3 == 1)
  {
  //Do nothing  
  }

ReadLDR4 = arduino.digitalRead(LDR4);
  if (ReadLDR4 == 0)
  {
  //arduino.analogRead(Sensor4);
  file = new SoundFile(this, "C:\\Users\\António\\Desktop\\PAP_Claudio\\re1.mp3");
  file.play();
  }
  else if (ReadLDR4 == 1)
  {
  //Do nothing  
  }

ReadLDR5 = arduino.digitalRead(LDR5);
  if (ReadLDR5 == 0)
  {
  //arduino.analogRead(Sensor5);
  //file = new SoundFile(this, "C:\\Users\\António\\Desktop\\PAP_Claudio\\mi1.mp3");
  //file.play();
  }  
  else if (ReadLDR5 == 1)
  {
  //Do nothing  
  }

}`

Tagged:

Answers

  • edit post, highlight code, press ctrl-o to format.

    DON'T READ FILES IN DRAW(). just don't. draw runs 60 times a second. loading files that don't change 60 times a second is terrible.

    load them all in setup(). reference them in draw().

    and continually calling play() might not work - i don't think samples loop automatically.

  • edited March 2017

    @Volkun --

    the problem is that the sounds are in loop and are playing one on top of the other while my hand remains on the sensors leading to a crash.

    Here is a recent related example using minim and a "Sampler" class object. Not exactly what you are doing, but it demonstrates some of the aspects of Sampler like multiple voices -- this allows you to play a laser string either just once or many times rapidly without problems.

    https://forum.processing.org/two/discussion/comment/90737/#Comment_90737

Sign In or Register to comment.