Having trouble getting processing to respond reliably to piezo

edited October 2017 in Arduino

So I am quite inexperienced in both processing and arduino but I think what I'm trying to accomplish is pretty simple so here it goes.

I'm trying to get a piezo disc input into arduino to trigger sounds in Processing using minim. For testing I have the piezo placed under a section of carpet and want it to continue triggering the sound as long as the carpet is being walked on. I have the arduino sketch written to print the string "play" any time the sensor reading is above zero. As you can see with the processing code I want Processing to play my sound sample any time it reads this string (and while the sample is not currently playing.)

The problem I'm having is the response from Processing is unreliable to say the least. I see it print the "play" many times but not always react accordingly (ie play the sound,). Only at certain times will it actually play the sound, often with delay. I imagine it might be because it's having trouble keeping up with the input from the piezo? I have tested the serial communication to load images as well with similar problems so I'm guessing it's that.

I hope this is something I can resolved with a better code, whether it's a simple oversight on my part or something more complicated I would appreciate any help.

PImage one;
import processing.serial.*;
Serial myPort;
String val;

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim minim;
AudioPlayer sample1;

Boolean reading=true;

void setup() {
  String portName = Serial.list()[0];
  myPort= new Serial(this, portName, 9600);
  one=loadImage("one.png");

  minim = new Minim(this);
  sample1 = minim.loadFile("sample1.wav");
}

void draw () {
  if (myPort.available()>0 ) {
    val=myPort.readString();
    println(val);
  }

  if (reading==true) {
    if (val.trim().equals("play")) {
      sample1.play();
      sample1.rewind();
    }
  }

  if (sample1.isPlaying()) {
    reading=false;  
  } else {
    reading=true;
  }
}
Tagged:
Sign In or Register to comment.