This was initially conceived as a mini weekend project with my 9 years old daughter I went for the simplest possible build using a bash shell script to detect the camera status and particle photon to move the Sith trooper. I repurposed an enclosure from a previous project with some modifications, and chose to make it battery powered to keep it as a self sufficient package.
It took me around 3 hours to build including many distractions and explaining the code / script to a 9 years old.
This build requires the following :
- Particle Photon Chip
- Micro Servo
- Li-Ion Battery (optional if you keep it connected)
Assembly:
Going for the simplest possible approach I went for direct wiring using female-female jumpers, thus bi-passing the need for a prototyping board or soldiering (safer given that a 9 years old was involved).
- Servo Control went into D2
- the rest is self explanatory (Ground to GND, power to power and so on.
CODE:
To detect the camera status I used a simple bash script running on my mac, which I set to start running when I logged in using this guide. The script maintains the previous status and only calls the particle backend if the status changes. When you use it please change <DEVID> and <TOKEN> to your own.
This is built for Mac however this can also be ported over easily to windows machines.
#!/bin/bash
########
#Detect if camers is on, and if its on call webservice#
########
#log show --last 1m|grep "Post event kCameraStream"|tail -1
isCamOn=$(log show --last 15m|grep "Post event kCameraStream"|tail -1)
wasCamOn="a"
while true
do
sleep 1;
isCamOn=$(log show --last 15m|grep "Post event kCameraStream"|tail -1)
if [[ $isCamOn == *"Stop"* ]]; then
if [[ $wasCamOn == "no" ]]; then
continue
fi
echo "It's stopped!"
curl https://api.particle.io/v1/devices/<DEVID>/faceback -d access_token=<TOKEN>
wasCamOn="no"
#Cam trooper to look back
elif [[ $isCamOn == *"Start"* ]]; then
if [[ $wasCamOn == "yes" ]]; then
continue
fi
echo "its ON"
curl https://api.particle.io/v1/devices/<DEVID>/facefront -d access_token=<TOKEN>
wasCamOn="yes"
#Cam trooper to look front
else
echo "unkown status"
#move left and right (trooper dance)
fi
sleep 1;
done
Moving the Sith-Trooper was as simple as moving the servo 180 degrees in each direction at a reasonable speed and then power off the servo (to reduce power consumption)
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
bool FacingFront=false;
void setup()
{
Particle.function("facefront", facefront);
Particle.function("faceback", faceback);
Particle.function("dance", dance);
myservo.attach(D2); // attach the servo on the D2 pin to the servo object
pinMode(D7, OUTPUT); // set D7 as an output so we can flash the onboard LED
Particle.publish("Setup Module", "Starting", PRIVATE);
// system.sleep
}
int moveServoTO(int newPos)
{
myservo.attach(D2);
myservo.write(newPos);
Particle.publish("**Servo Moving TO **"+newPos, "position:"+ newPos, PRIVATE);
delay(3000);
myservo.detach();
return 1;
// return 0;
}
int facefront(String astring)
{
if(FacingFront)
{
return 0;
}
moveServoTO(180);
FacingFront=true;
return 1;
}
int faceback(String astring)
{
if(!FacingFront)
{
return 0;
}
moveServoTO(0);
FacingFront=false;
return 1;
}
int dance(String astring)
{
return 1;
}
void loop()
{
facefront("a");
faceback("b");
}
Future Enhancements:
- Properly wiring it (a bunch of resistors are required to secure the leads to the particle chip)
- Padding the box to reduce the servo sound
- Adding more events and actions
- 3D printing a better enclosure (smaller one)