3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Halloween Dropping Spider

Step 3Arduino Sketch

Arduino Sketch
I used the X10Firecracker and the Servo libraries as well as the PIR sensor example from the arduino playground.

I used the 1K resistors on the led1Pin and led2Pin.
I used the 10K resistor on speakerPin going to 2N2222 transistor base, ground to the emitter. Then the emiter went to one side of the toy switch and the collector went to the other. This worked the 2N2222 transistor as a switch.

Look at the comments for the arduino pin wiring.


#include <X10Firecracker.h>
#include <Servo.h>

Servo myservo;             // New instance of Servo.h
int rtsPin = 2;            // RTS line for C17A - DB9 pin 7
int dtrPin = 3;            // DTR line for C17A - DB9 pin 4
// Connect DB9 pin 5 to ground.
int servoPin = 5;          // Servo used to lift the reel
int pirPin = 8;
int led1Pin = 10;          // Left led
int led2Pin = 11;          // Right led
int speakerPin = 12;       // Piezo buzzer speaker
int bitDelay = 1;          // mS delay between bits (1 mS OK)              
int ledStatus = 0;
int calibrationTime = 30;       
long unsigned int lowIn;      
long unsigned int pause = 5000;   
boolean lockLow = true;
boolean takeLowTime;   
int booCounter = 1;

void setup(){
  Serial.begin(9600);        // Start serial communication at 9600 baud rate
  pinMode(led1Pin, OUTPUT);  // Set led1Pin digital pin to output
  pinMode(led2Pin, OUTPUT);  // Set led1Pin digital pin to output
  pinMode(speakerPin, OUTPUT);// Set speakerPin digital pin to output
  pinMode(servoPin, OUTPUT);  // Set led1Pin digital pin to output
  myservo.attach(7);          // Atach servo on pin 7 for continous rotation servo
  X10.init(rtsPin, dtrPin, bitDelay);  // Initialize X10 C17A
  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); 
  myservo.write(140);  
}
void loop(){
  if(digitalRead(pirPin) == HIGH){
    Serial.print("[[[get|http://www.mysite.com/iobridge.html]]]"); // send serial message to iobridge.
    digitalWrite(led1Pin, HIGH);   //the led visualizes the sensors output pin state
    digitalWrite(led2Pin, 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; 
      // Release the reel by lifting servo.
      myservo.write(140);
      // Turn on toy with sound
      digitalWrite(speakerPin, HIGH);
      delay(100);
      digitalWrite(speakerPin, LOW);
      delay(7000);
      myservo.write(65);
      // send x10 commands to trun off/on lights
      X10.sendCmd( hcC, 1, cmdOn );
      X10.sendCmd( hcC, 3, cmdOn );
      X10.sendCmd( hcC, 2, cmdOff );         
      int var = 0;
      // Activate the continous rotation servo.
      while(var < 800){
        digitalWrite(servoPin,HIGH);
        delayMicroseconds(1200); // 1.5ms
        digitalWrite(servoPin,LOW);
        delay(20); // 20ms
        var++;
      }
      delay(50);
    }        
    takeLowTime = true;
  }

  if(digitalRead(pirPin) == LOW){ 
    if (ledStatus == 0){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, LOW);
      ledStatus = 1;
      delay(100);
    }
    else{
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, HIGH);
      ledStatus = 0;
      delay(100);
    }
    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;                       
      // Send x10 commands
      X10.sendCmd( hcC, 1, cmdOff );
      X10.sendCmd( hcC, 3, cmdOff );
      X10.sendCmd( hcC, 2, cmdOn );
    }
  }
}
« Previous StepDownload PDFView All StepsNext Step »
1 comment
Nov 6, 2009. 4:05 PMrachel says:
As of Nov 6, there's a bug in displaying certain kinds of content, including code and some kinds of ASCII art.  We're working on fixing this.  You don't need to do anything; the text hasn't been deleted, it's just not displaying properly.

Sorry about that and we'll have it back as soon as we can!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
9
Followers
4
Author:noelportugal(My blog)