Loading...
Logo
Processing Forum
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:

Copy code
  1. #include <Servo.h> 

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

  6. void setup() {
  7.   // initialize serial communication:
  8.   Serial.begin(9600);
  9.   // initialize the LED pin as an output:
  10.   pinMode(ledPin, OUTPUT);
  11.   
  12.   myservo1.attach(3);//servo  
  13.   //myservo2.attach(9);//servo  
  14.   //myservo1.write(pos);
  15. }

  16. void loop() {
  17.   // see if there's incoming serial data:
  18.    if (Serial.available() > 0) {
  19.     // read the oldest byte in the serial buffer:
  20.     incomingByte = Serial.read();
  21.     // if it's a capital H (ASCII 72), turn on the LED:
  22.           if (incomingByte == 'H'){
  23.             digitalWrite(ledPin, HIGH);  // turn LED OFF 
  24.             for(pos = 0; pos < 180; pos += 6)  // goes from 0 degrees to 180 degrees 
  25.               {                                  // in steps of 1 degree 
  26.                 myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
  27.                 delay(15);                       // waits 15ms for the servo to reach the position 
  28.               } 
  29.              
  30.              Serial.println("AAoff");
  31.              Serial.println("Servooff");
  32.            }
  33.             if (incomingByte == 'L'){
  34.               digitalWrite(ledPin, LOW);  // turn LED OFF 
  35.               for(pos = 180; pos>=1; pos-=6)  // goes from 0 degrees to 180 degrees 
  36.                 {                                  // in steps of 1 degree 
  37.                   myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
  38.                   delay(15);                       // waits 15ms for the servo to reach the position 
  39.                 } 
  40.                
  41.                Serial.println("AAoff");
  42.                Serial.println("Servooff");
  43.             }
  44.    }
  45. }

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:

Copy code
  1. import processing.serial.*;

  2. Serial port;

  3. void setup(){
  4.   size(200,200);
  5.   noStroke();
  6.   //framerate(10);
  7.   
  8.   port=new Serial (this, Serial.list()[1],9600);
  9. }

  10. boolean mouseOverRect()
  11. {
  12.   return((mouseX>=50)&&(mouseX<=150)&&(mouseY>=50)&(mouseY<=150));
  13. }

  14. void draw(){
  15.   background(#222222);
  16.   if(mouseOverRect())
  17.   {
  18.     fill(#BBBBB0);
  19.     port.write("H");
  20.   }
  21.   else{
  22.     fill(#666660);
  23.     port.write("L");
  24.   }
  25.   rect(50,50,100,100);
  26. }

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!