We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi. I don't have a lot of experience with Processing. I need a graphical display of the changing values coming from a sensor which I have so far. I then need to check if the sensor value has dropped below a certain value and has been below that value for 20 seconds, if it has I use a wave file for an alarm. The problem I am having with this code is;
The wave alarm is sounding after 20 seconds of running the program, not after 20 seconds of the sensor value being below a certain value "threshold" (fyi - this same code worked properly for me using Arduino)
Once the wave file plays the graph stops, I would like for the graph to continue displaying the sensor values
Once the sensor value goes above the "threshold" I would like for the wave file to close and the program to continue monitoring when the sensor value drops below "threshold" for 20 seconds or more.
Any help would be greatly appreciated. Thanks!
import processing.serial.*;
import ddf.minim.*;``
Minim minim;
AudioPlayer player;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
int threshhold = 200;
float fValue;
boolean newVal = false;
void setup ()
{
// set the window size:
size(800, 600);
myPort = new Serial(this, "COM9", 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
stroke(127, 34, 255);
}
void draw ()
{
if (newVal)
{
line(xPos, height, xPos, height - fValue);
if (++xPos >= width)
{
xPos = 0;
background(0);
}
newVal = false;
}
}
void serialEvent (Serial myPort)
{
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
inString = trim(inString);
fValue = float(inString);
fValue = map(fValue, 0, 1023, 0, height);
newVal = true;
}
int lastBreath = 0;
if (fValue > threshhold)
{
lastBreath = millis();
}
if (fValue < threshhold)
{
if (millis() - lastBreath >= 20000)
{
while (fValue < threshhold)
{
minim = new Minim(this);
// loadFile will look in all the same places as loadImage does.
// this means you can find files that are in the data folder and the
// sketch folder. you can also pass an absolute path, or a URL.
// Change the name of the audio file here and add it by clicking on "Sketch —> Import File"
player = minim.loadFile("chimes.wav");
player.play();
delay(500);
}
}
}
}
Answers
Edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code
Kf
@cormee38 -- Were you able to resolve this issue?
No, I have searching online for some guidance. Any help you could offer would be greatly appreciated.
You can check the post on Feb 27: https://forum.processing.org/two/discussion/comment/89928/#Comment_89928
This code works by storing data after a trigger is issue by the software. The trigger is issued when the signal goes above a threshold expressed as a percentage of the full range.
Kf