Arduino + Processing + Trigger events through webcam

Hi guys, I was wondering if anyone could give me some help…I’m doing a project where i have servos that i want to be triggered through movement, so far I have the below codes…the problem is that the servo just keeps runing on it’s own, doesn’t stop at states…

Arduino:

#include <Servo.h> 

Servo myservo1;
int pos = 0;
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  
  myservo1.attach(3);//servo  
  //myservo2.attach(9);//servo  
  //myservo1.write(pos);
}

void loop() {
  // see if there's incoming serial data:
   if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
          if (incomingByte == 'H'){
            digitalWrite(ledPin, HIGH);  // turn LED OFF 
            for(pos = 0; pos < 180; pos += 6)  // goes from 0 degrees to 180 degrees 
              {                                  // in steps of 1 degree 
                myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
                delay(15);                       // waits 15ms for the servo to reach the position 
              } 
             
             Serial.println("AAoff");
             Serial.println("Servooff");
           }
            if (incomingByte == 'L'){
              digitalWrite(ledPin, LOW);  // turn LED OFF 
              for(pos = 180; pos>=1; pos-=6)  // goes from 0 degrees to 180 degrees 
                {                                  // in steps of 1 degree 
                  myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
                  delay(15);                       // waits 15ms for the servo to reach the position 
                } 
               
               Serial.println("AAoff");
               Serial.println("Servooff");
            }
   }
}

WORKS WITH KEYBOARD…If i type L and H in the serial monitor it goes to the positions I want…

In the processing part I was trying this:

import processing.serial.*;

Serial port;

void setup(){
  size(200,200);
  noStroke();
  //framerate(10);
  
  port=new Serial (this, Serial.list()[1],9600);
}

boolean mouseOverRect()
{
  return((mouseX>=50)&&(mouseX<=150)&&(mouseY>=50)&(mouseY<=150));
}

void draw(){
  background(#222222);
  if(mouseOverRect())
  {
    fill(#BBBBB0);
    port.write("H");
  }
  else{
    fill(#666660);
    port.write("L");
  }
  rect(50,50,100,100);
}

Or something similar that can trigger when the camera detects movement…another example is the Frame Differencing but i can’t use regular video library with serial…

Thank in advance!

I've never used processing, but I believe draw is its equivalent of loop. So it appears that you're sending 'L' or 'H' continuously, which would account for the movement of the servo.

I have never used it either but I thought this was a neat project.

http://www.niklasroy.com/project/88/my-little-piece-of-privacy/

they used a camera and processing to move a curtain and their code examples may help?

Sending two letter ‘H’ will be the same from processing or the keyboard, will first move it slowly to 180 degrees and the on the second ‘H’ will move it back to 0 degrees quickly and again slowly to 180 degrees.

Grumpy Mike you're right but the thing is, i just want to use the camera do trigger events

detects movement: goes to 180 stops there

and after that, with a predetermined delay or something goes back to 0...is it possible?

Yes just build in the going back thing before you look at the input again.