Paso 4: Arduino
> mirrormirror.ino
#include #include Servo myservo; // New instance of Servo.h int servoPin = 7; // Servo used to rotate the CD int pingPin = 4; // Ping pin int dtrPin = 3; // DTR line for C17A - DB9 pin 4 int rtsPin = 2; // RTS line for C17A - DB9 pin 7 int buttonState = 0; // Initialize button state to low int bitDelay = 1; // mS delay between bits (1 mS OK) int counter = 0; // counter used to change/toogle lcd message char code[32] = ""; boolean isEngaged = false; int isitreal = 0; void setup(){ myservo.attach(servoPin); Serial.begin(9600); X10.init(rtsPin, dtrPin, bitDelay); // Initialize X10 C17A myservo.write(0); dropCandy(); } void loop(){ long duration, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); cm = microsecondsToCentimeters(duration); if (cm < 120){ isitreal ++; if (isitreal = 2){ Serial.print("A"); delay(8 * 1000); dropCandy(); delay(15 * 1000); isitreal = 0; } } else if (cm > 120 && cm < 200){ X10.sendCmd( hcA, 2, cmdOff ); } //Listen if computer send signal to drop candy. if (Serial.available()) { char c = (char)Serial.read(); if (c == '1'){ dropCandy(); } } delay(100); } void dropCandy(){ //Turn on X10 light and fog X10.sendCmd( hcA, 1, cmdOn ); X10.sendCmd( hcA, 3, cmdOn ); myservo.write(180); delay(500); myservo.write(0); //Wait 30 seconds before next candy //Turn on X10 light and fog X10.sendCmd( hcA, 1, cmdOff ); //delay(10 * 1000); X10.sendCmd( hcA, 3, cmdOff ); } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }
El Applescript siguiente se encarga de la conexión serie de Arduino USB. Tenga en cuenta que solía Xports para conectar Applescript para puerto serie.
set zonea to {"Who are you", "I like your costume", "Spin around for me","Who is the fairest of them all, you of course.", "What is the magic word?", "Do a dance for me","Trick or treat, Smell my feet"} set zoneb to {"Happy Halloween!", "Hasta la vista baby", "So long, farewell, auf Wiedersehen, good night", "Thanks for stopping by, happy halloween" } set onMode to false set use_port to "/dev/cu.usbmodem1d11" repeat until (get serialport list) contains use_port delay 3 end repeat if (get serialport list) contains use_port then set onMode to true set myPort to serialport open use_port bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0 delay 1 if myPort is equal to -1 then display dialog "could not open port" else repeat while onMode is true set state to (do shell script "cat '/full/path/of/file/state.txt'") if state is equal to "1" then set x to serialport read myPort if x is not equal to "" then if x is equal to "A" then set randomPhraseA to some item of zonea say randomPhraseA do shell script ("sudo -u [username] arch -i386 osascript /full/path/of/file/animate.scpt &> /dev/null &") delay 10 set randomPhraseB to some item of zoneb say randomPhraseB end if end if else serialport write "1" to myPort delay 5 do shell script ("sudo -u [username] echo 1 > /full/path/of/file/state.txt") do shell script ("sudo -u [username] arch -i386 osascript /full/path/of/file/animate.scpt &> /dev/null &") end if end repeat end if serialport close myPort end if
Utilicé el siguiente script para prácticamente pulsa abajo/izquierda/derecha flechas. Esto hizo que la marioneta de espejo mover la cabeza.
> animate.scpt
tell application "System Events" key down (ASCII character 31) delay 1 key up (ASCII character 31) key down (ASCII character 28) delay 1 key up (ASCII character 28) key down (ASCII character 29) delay 1 key up (ASCII character 29) end tell