Pages: [1]   Go Down
Author Topic: need help with servos  (Read 326 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could someone help me combine these two sketches
I am new to this and would greatly appreciate some help
what I would like to do is trigger the random servo movement with the pir and only move servos when pir triggers my audio


Code:
/*
 * //////////////////////////////////////////////////
 * //making sense of the Parallax PIR sensor's output
 * //////////////////////////////////////////////////
 *
 * Switches a LED according to the state of the sensors output pin.
 * Determines the beginning and end of continuous motion sequences.
 *
 * @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
 * @date:   3. September 2006
 *
 * kr1 (cleft) 2006
 * released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
 * http://creativecommons.org/licenses/by-nc-sa/2.0/de/
 *
 *
 * The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
 * (http://www.parallax.com/detail.asp?product_id=555-28027)
 *
 * The sensor's output pin goes to HIGH if motion is present.
 * However, even if motion is present it goes to LOW from time to time,
 * which might give the impression no motion is present.
 * This program deals with this issue by ignoring LOW-phases shorter than a given time,
 * assuming continuous motion is present during these phases.
 *  
 */
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 1;        
//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 = 5;  
boolean lockLow = true;
boolean takeLowTime;  
int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;
int mp3Pin = 2;

/////////////////////////////
//SETUP
void setup(){
  Serial.begin(38400);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(mp3Pin, OUTPUT);  //set mp3Pin as an output
  digitalWrite(pirPin, LOW);
  digitalWrite(mp3Pin, LOW); //set mp3Pin to low ("off")
  //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
       digitalWrite(mp3Pin, HIGH);  //send a signal on the mp3Pin to have the relay trigger the sound
       delay(2); //wait
       digitalWrite(mp3Pin, LOW);  //bring the mp3Pin back low so the relay closes and doesn't loop the sound
       delay(15000);
       digitalWrite(mp3Pin, HIGH);  //send a signal on the mp3Pin to have the relay trigger the sound
       delay(2);
       digitalWrite(mp3Pin, LOW);
       delay(1000);
       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
       digitalWrite(mp3Pin, LOW);
       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(100);
           }
       }
  }
---------------------

Code:
/**
 * ScaryShiftServoEyeballs.pde --
 *  Get two eyeballs, hook a servo to each one,
 *  then mount that on a 'head' and connect a servo to that.
 *  This sketch will make the eyes and head "look around" in a shifty fashion.
 *  Add sensors to make it look at things!
 *
 * 2009, Tod E. Kurt, http://todbot.com/blog/
 *
 */

#include <Servo.h>
 
Servo servoHead;
Servo servoEyeL;
Servo servoEyeR;

int posHead;
int posEye;
int posHdest, posEdest;  
int wait;
int rate = 2;

void setup()
{
  servoHead.attach(5);
  servoEyeL.attach(6);
  servoEyeR.attach(7);

  posHdest = posHead = 90;
  posEdest = posEye = 90;
}
 
void loop()
{
    if( wait == 0 ) {
        posHdest = random(70,110);  // move had between 70-110 degrees
        posEdest = random(40,140);  // move eyes between 40-140 degrees
        wait = random(150,450);     // between 1.5 secs & 4.5 secs
    }
    else {
        posHead = slew_servo( posHead, posHdest, rate);
        posEye  = slew_servo( posEye,  posEdest, rate);
        wait--;
    }
 
    servoHead.write( posHead );
    servoEyeL.write( posEye  );
    servoEyeR.write( posEye  );    

    delay(10);
}

// return new pos from current pos, dest and rate
int slew_servo( int pos, int dest, int rate)
{
    int diff = pos - dest;
    if( diff == 0 ) return pos;
    else if( diff > -rate ) pos++;
    else if( diff <  rate ) pos--;
    if(      diff > 0 ) pos -= rate;
    else if( diff < 0 ) pos += rate;
    return pos;
}

Moderator edit: [code] [/code] tags added.
« Last Edit: July 25, 2012, 04:36:34 am by AWOL » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
could someone help me combine these two sketches
What help do you need? If you understand what each does, then combining them will take about 3 minutes.

Each one defines some global variables. Add the ones from one sketch to the other.

Each does some stuff, once, in setup(). Move the stuff from one setup() to the other.

Each does some stuff on every pass through loop. One works the way you want. The other doesn't. Figure out where in the first sketch the stuff from the second sketch should happen, and move that stuff there.
Logged

Pages: [1]   Go Up
Jump to: