Go Down

Topic: OK i need super good code writers to help me [beginer project] (Read 609 times) previous topic - next topic

Crazycr1ss

ok i need to merge two codes together to make one. I need my servo to move 90 degrees when the PIR Sensor is activated. and yea thats about is. so i have these two codes.


This code is for the PIR Sensor. When the sensor is activated it makes a light and oiezo speaker light, and buzz.

/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){ 
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;           
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec");
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){ 
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                       
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }



This simply makes the servo move 90 degrees whan power goes throught the arduino

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2; // a maximum of eight servo objects can be created

int pos = 90;    // variable to store the servo position

// set a variable to store the byte sent from the serial port
int incomingByte;

void setup() {

  // start sending data at 9600 baud rate
  Serial.begin(9600);
  myservo.attach(9);    // attaches the servo on pin 9 to the servo object
  myservo2.attach(10);  // attaches the servo on pin 10 to the servo object

}

void loop() {
  // check that there's something in the serial buffer
  if (Serial.available() > 0) {
    // read the byte and store it in our variable
    // the byte sent is actually an ascii value
    incomingByte = Serial.read();
    // note the upper casing of each letter!
    // each letter turns a motor different way.
   
    // start of left laser sweep, moving left 90 degrees.
    // arrow laser spins and points to left.
    if (incomingByte == 'L') {
    Serial.println(incomingByte);
   
    myservo2.write(180);                         // send servo 2 to the 180 degree position and stay
    for(pos = 180; pos > 90; pos -= 1)  // goes from 90 degrees to 180 degrees
  {     // in steps of 1 degree
    Serial.println(pos);
    myservo.write(pos);    // tell servo to go to position in variable 'pos'
   
    delay(15);                       // waits 15ms for the servo to reach the position
  }
    for(pos = 90; pos<=180; pos+=1)     // goes from 180 degrees to 90 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    }
    }
   
   
    // start of right laser sweep, moving right 90 degrees.
    // arrow laser spins and points to right.
   
    if (incomingByte == 'R') {
     Serial.println(incomingByte);
     Serial.println(pos);
    myservo2.write(0);                          // send servo 2 to the 0 degree position
    for(pos = 0; pos < 90; pos += 1)  // goes from 90 degrees to 0 degrees
  { Serial.println(pos);                                 // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
    for(pos = 90; pos>=0; pos-=1)     // goes from 0 degrees to 90 degrees
  { Serial.println(pos);                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    }
    }
   
    // if a S is sent make sure the servos go to 90 degrees
    if (incomingByte == 'S') {
    Serial.println(incomingByte);
    Serial.println(pos);
    myservo2.write(90);
    myservo.write(90); 
     
     
     
     
    }
  }
}

Please help

zoomkat

Some reading that might help you.

http://www.google.com/search?q=servo+sensor+site%3Ahttp%3A%2F%2Farduino.cc%2Fforum%2Findex.php&hl=en&num=100&lr=&ft=i&cr=&safe=images&tbs=
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Crazycr1ss

Thamkyou so much KE7GKP but it is putting an error when i compile it...... it high lights
Code: [Select]
       myservo.write(180);         // send servo to the 180 degree position and stay and says that "myservo was not declared in this scope"

AWOL

So,you need to declare "myservo" in scope.
I'd help you more, but I can't see your code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up