/*
  Cat graphing program
  Language: Processing

  Reads in a string of characters until it gets a linefeed (ASCII 10). 
  Then converts the string into a number. Then graphs it. 
  If the number has changed significantly, and there hasn't been 
  a big change in more than a minute, the program prints a 
  text string in place of an email message.
  
*/

import processing.serial.*;

int linefeed = 10;      // linefeed in ASCII
Serial myPort;          // The serial port
int sensorValue = 0;    // the value from the sensor
int graphPosition = 0;  // the horizontal position of the latest
                        // line to be drawn on the graph

int prevSensorValue = 0;    // the previous sensor reading
boolean catOnMat = false;   // whether or not the cat's on the mat
int threshold = 320;        // above this number, the cat is on the mat.

int timeThreshold = 1;    // minimum number of minutes between emails
int timeLastSent[] = {
  hour(), minute() - 1 }; // time the last message was sent



void setup() {
  size(400,300);
  // List all the available serial ports
  println(Serial.list());

  // I know that the first port in the serial list on my Mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using (the output
  // of Serial.list() can help; the are listed in order
  // starting with the one that corresponds to [0]).
  myPort = new Serial(this, Serial.list()[0], 9600);

  // read bytes into a buffer until you get a linefeed (ASCII 10):
  myPort.bufferUntil(linefeed);
  println(hour() + ":" + minute());
}

void draw() {

  if (sensorValue > threshold ) {
    // if the last reading was less than the threshold,
    // then the cat just got on the mat.
    if (prevSensorValue <= threshold) {
      delay(100);
      if (sensorValue > threshold) {
        catOnMat = true;
        sendMail();
      }
    } 
  } else {
    // if the sensor value is less than the threshold,
    // and the previous value was greater, then the cat
    // just left the mat
    if (prevSensorValue >= threshold) {
      catOnMat = false;
    }    
  }
  // save the sensor value as the previous value
  // so you can take new readings:
  prevSensorValue = sensorValue;
}

// serialEvent  method is run automatically by the Processing applet
// whenever the buffer reaches the  byte value set in the bufferUntil() 
// method in the setup():

void serialEvent(Serial myPort) { 
  // read the serial buffer:
  String myString = myPort.readStringUntil(linefeed);
  // if you got any bytes other than the linefeed:
  if (myString != null) {
    //trim off the carriage return and convert the string to an integer:
    sensorValue = int(trim(myString));
    // print it:
    // println(sensorValue);
    drawGraph();
  }
} 

void drawGraph() {
  int lineHeight = sensorValue /2;
  // draw the line:
  if (catOnMat) {
    // draw green:
    stroke(0,255,0);
  } 
  else {
    // draw red:
    stroke(255,0,0); 
  }
  line(graphPosition, height, graphPosition, height - lineHeight);
  // at the edge of the screen, go back to the beginning:
  if (graphPosition >= width) {
    graphPosition = 0;
    background(0); 
  } 
  else {
    graphPosition++;
  }
}

void sendMail() {
  // calculate the current time in minutes:
  int[] presentTime = { 
    hour(), minute()          };

  // print the current time and the last time you sent a message:
  print(sensorValue + "\t");
  print( presentTime[0] + ":" + presentTime[1] +"\t");
  println(timeLastSent[0] + ":" + timeLastSent[1]);


  // if you're still in the same hour as the last message,
  // then make sure at least the minimum number of minutes has passed:
  if (presentTime[0] == timeLastSent[0]) {
    if (presentTime[1] - timeLastSent[1] >= timeThreshold) {
      println("This is where you'd send a mail."); 
      // take note of the time this message was sent:
      timeLastSent[0] = hour();
      timeLastSent[1] = minute();  
    }
  }
  
   // if the hour has changed since the last message,
  // then the difference in minutes is a bit more complex.
  // Use !+ rather than > to make sure the shift 
  // from 23:59 to 0:00 is covered as well:
  if (presentTime[0] != timeLastSent[0]) {
    // calculate the difference in minutes:
    int minuteDifference = (60 - timeLastSent[1]) + presentTime[1];

    if (minuteDifference >= timeThreshold) {
      println("This is where you'd send a mail."); 

      // take note of the time this message was sent:
      timeLastSent[0] = hour();
      timeLastSent[1] = minute();  
    }
  }
}

Sorry, the comment form is closed at this time.

   
© 2012 Making Things Talk Suffusion theme by Sayontan Sinha