ZOMG i got my cat cam code to work!!!

/**
* Getting Started with Capture.
*
* Reading and displaying an image from an attached Capture device.
*/
import processing.serial.*;
String inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed
Serial myPort;
int val;
import processing.video.*;
float pot;
float ping;
float threshold;

Capture cam;

void setup() {
size(640, 480);
frameRate(15);
// List all the available serial ports:
println(Serial.list());
myPort = new Serial(this, Serial.list()[13], 9600);
myPort.bufferUntil(lf);
String[] cameras = Capture.list();

if (cameras.length == 0) {
println(“There are no cameras available for capture.”);
exit();
}
else {
println(“Available cameras:”);
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}

// The camera can be initialized directly using an element
// from the array returned by list():
cam = new Capture(this, cameras[0]);
// Or, the settings can be defined based on the text in the list
//cam = new Capture(this, 640, 480, “Built-in iSight”, 30);

// Start capturing the images from the camera
cam.start();
}
}

void draw() {
if (cam.available() == true) {
cam.read();
}

image(cam, 0, 0);
//the potentiometer changes the threshold of the ping sensor, the ping sensor threshold is set via the potentiometer.
//potentiometer number = ping sensor threshold
//first number sets threshold
//inString
// Ping sensor threshold  = catinview

// if (keyPressed && key == ‘s’)
// if (inString<catinview){
//if (inValueInCM < catinview ) {

//delay 100 seconds
}
// The following does the same as the above image() line, but
// is faster when just drawing the image without any additional
// resizing, transformations, or tint.
//set(0, 0, cam);

void serialEvent(Serial p) {
inString = p.readString();
inString=trim(inString);
int sensors[] = int(split(inString, ‘,’));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
println(“Sensor ” + sensorNum + “: ” + sensors[sensorNum] + “\t”);

if (sensors.length > 1) {
pot = sensors[0];
ping = sensors[1];

if (ping<pot) {
println(“I GOT YOU CAT!!”);
saveFrame(“test-######.png”);
}
}
}

//println(“stringvalue: “+inString);

//toms stuff don’t lose
// toms int i=0;
/*try {
i =Integer.parseInt(trim(inString));
} catch (Exception e) {
println(“Not parsed”);
}*/
//toms i = int(trim(inString));
}

 

 

and arduino code (i also added sound to the other one)

 

/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.
The circuit:
* VCC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 2
* ECHO connection of the sensor attached to digital pin 4

Original code for Ping))) example was created by David A. Mellis
Adapted for HC-SR04 by Tautvidas Sipavicius

This example code is in the public domain.
tweek by Amelia Winger-Bearskin ITP 2013
*/
int potPin = A0;    // select the input pin for the potentiometer
int redled = 9;
int greenled=10;// select the pin for the LED
int potValue = 0;  // variable to store the value coming from the sensor

const int trigPin = 12;
const int echoPin = 13;

void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
}
void loop()
{
//potentiomenter info
potValue = analogRead(A0)/4;
Serial.print(potValue);
Serial.print(“,”);
// turn the ledPin on

// digitalWrite(redledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
// digitalWrite(greenledPin, HIGH);
// stop the program for for <sensorValue> milliseconds:
// read the switch input:

// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.println(cm);
if (cm<potValue) {
// if the switch is closed:
digitalWrite(redled, HIGH);    // turn on the yellow LED
digitalWrite(greenled, LOW);     // turn off the red LED
}
else {
//if the switch is open:
digitalWrite(redled, LOW);     // turn off the yellow LED
digitalWrite(greenled, HIGH); }   // turn on the red LED

delay(100);

}

long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Comments Are Closed