Graph with loop checking for sensor input

edited March 2017 in Arduino

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;

  1. 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)

  2. Once the wave file plays the graph stops, I would like for the graph to continue displaying the sensor values

  3. 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

Sign In or Register to comment.