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!
1