Introduction: How to Build Another Useless Machine: Easy to Make, and Hard to Use!
So many useless machines are built, why building another one? Simply, it is really fun :), fun to build and fun to play with.
I tried to give this version a character, as I always felt there is somebody "thinking inside the box", so here I tried, I hope I succeeded to give "her" a life!
I am really happy to share with you all the steps, with all hopes that you will be easily building yours and giving it a bitter or better character! My version is based on Arduino, you may think it is an overkill, but given the flexibility it gave me, I think it is worth it.
Here is a video of everything put all together. Later, we will move into the parts, steps, schematics and code
Let me know your comments and/or questions if any.
Hope you enjoy!
f.
I tried to give this version a character, as I always felt there is somebody "thinking inside the box", so here I tried, I hope I succeeded to give "her" a life!
I am really happy to share with you all the steps, with all hopes that you will be easily building yours and giving it a bitter or better character! My version is based on Arduino, you may think it is an overkill, but given the flexibility it gave me, I think it is worth it.
Here is a video of everything put all together. Later, we will move into the parts, steps, schematics and code
Let me know your comments and/or questions if any.
Hope you enjoy!
f.
Step 1: How It Works?
Everything is connected to an Arduino board, the board waits till the switch is set to logic HIGH , that is, when you flick the switch, the board signals two servo motors to move in a specific way (more on that later), after which, the servo connected to the "hand" will flick the switch back to OFF-position, and return back into the box, followed by the "door" servo. then everything repeats.
If you saw the above video, each time the switch is toggled, the machine responds in a different way, and this is done by simply writing different functions with different timings in the Arduino code, (I am calling them "Moves"). Each time the switch is toggled, I am calling a new move until all are done, I start over. note that in step 9, I provided another version of the code that picks random moves instead of sequential. You can add as many moves as you want, as long you have enough memory on the Arduino.
The Arduino is always ON, this is why I added a switch for the battery to save some power when the machine is not in use. I am already planning to use an interrupt on the switch, instead of the current simple HIGH/LOW detection, and coupling it with sleep mode. This shall save power when the machine is not in use ,and also allowing it to be always ready (but this will also complicate the code) So, for now, it is just a simple ON/OFF switch, and the board is always powered.
If you saw the above video, each time the switch is toggled, the machine responds in a different way, and this is done by simply writing different functions with different timings in the Arduino code, (I am calling them "Moves"). Each time the switch is toggled, I am calling a new move until all are done, I start over. note that in step 9, I provided another version of the code that picks random moves instead of sequential. You can add as many moves as you want, as long you have enough memory on the Arduino.
The Arduino is always ON, this is why I added a switch for the battery to save some power when the machine is not in use. I am already planning to use an interrupt on the switch, instead of the current simple HIGH/LOW detection, and coupling it with sleep mode. This shall save power when the machine is not in use ,and also allowing it to be always ready (but this will also complicate the code) So, for now, it is just a simple ON/OFF switch, and the board is always powered.
Step 2: The Parts
You will need:
Mandatory:
1. A box
2. Any Arduino board with PWM outputs (I happen to have an old one laying around, the Diecimila)
3. Two standard RC servo motors (I used the Futaba - S3003, they have 3,2 Kg/cm torque, more than enough to move the door or hand)
4. A 6-12 V battery (I used my AR drone battery, it is a 11.1 v / 1000 mA)
3. An SPTT or SPST switch for the trigger (basically, any switch that suits you!)
5. Another switch for the battery
6. A push button to reset the Arduino in case you want to restart the moves/code from top.
7. Some jumper wires
Optional:
In case you want to design a move that includes moving the box around (like last move in the video), then you can get:
1. An Arduino motor shield, this will also be handy for other projects (or you can design an H-bridge yourself, search it out)
2. A cheap RC car so you can take its DC motor and gear box
Furthermore, still in the "optional" section, I've built a hardware de-bounce circuit to have a clean trigger when the switch is toggled on/off. This can be skipped and easily be implemented in arduino software using delays or timers. I did the hardware debounce because I will be soon changing the code to use interrupts, and since the latter do not allow the reliable use of delay/timer functions in the routine, so I had to build a simple de-bounce circuit for future use. If you want to skip this part, then just connect a simple switch/resistor to pin 2 of the arduino and implement a software debounce routine.
For the hardware debounce:
1. 10mF electrolytic capacitor
2. 10K resistor
3. A logic NOT gate i.e Inverter (chip 74HC04) This has six inverters, but you will only need one.
Let's build it...
Mandatory:
1. A box
2. Any Arduino board with PWM outputs (I happen to have an old one laying around, the Diecimila)
3. Two standard RC servo motors (I used the Futaba - S3003, they have 3,2 Kg/cm torque, more than enough to move the door or hand)
4. A 6-12 V battery (I used my AR drone battery, it is a 11.1 v / 1000 mA)
3. An SPTT or SPST switch for the trigger (basically, any switch that suits you!)
5. Another switch for the battery
6. A push button to reset the Arduino in case you want to restart the moves/code from top.
7. Some jumper wires
Optional:
In case you want to design a move that includes moving the box around (like last move in the video), then you can get:
1. An Arduino motor shield, this will also be handy for other projects (or you can design an H-bridge yourself, search it out)
2. A cheap RC car so you can take its DC motor and gear box
Furthermore, still in the "optional" section, I've built a hardware de-bounce circuit to have a clean trigger when the switch is toggled on/off. This can be skipped and easily be implemented in arduino software using delays or timers. I did the hardware debounce because I will be soon changing the code to use interrupts, and since the latter do not allow the reliable use of delay/timer functions in the routine, so I had to build a simple de-bounce circuit for future use. If you want to skip this part, then just connect a simple switch/resistor to pin 2 of the arduino and implement a software debounce routine.
For the hardware debounce:
1. 10mF electrolytic capacitor
2. 10K resistor
3. A logic NOT gate i.e Inverter (chip 74HC04) This has six inverters, but you will only need one.
Let's build it...
Step 3: Building the Box
You can use any box you find suitable. I built mine, maybe not perfect, but still much personal :)
1. I used 2 sheets of 5mm BALSA wood, very easy to work with using a cutter, and relatively strong given its incredible light weight.
2. Consumed 5 tubes of Cyanoacrylat glue (aka strong fast-acting adhesives)
Here it is:
A 22 x 14 x 14 cm box
1. I used 2 sheets of 5mm BALSA wood, very easy to work with using a cutter, and relatively strong given its incredible light weight.
2. Consumed 5 tubes of Cyanoacrylat glue (aka strong fast-acting adhesives)
Here it is:
A 22 x 14 x 14 cm box
Step 4: Schematic
Above is the schematic (drawn using Fritzing), showing:
Top part: The arduino main board ,2 servos and switch connectivity. Note that as mentioned previously you can replace the whole debounce (top-right) circuit with a simple resistor/switch.
Both "door" and "hand" servos are powered directly from the 5v pin on the Arduino. The signal wires are connected to pin 9 and 10 respectively. These pins supports PWM, thus I am using them to control the angle/position of the servo (from 0 to 180 degree max).
Note: the red push button is just a reset (similar to the one found on the face of the arduino board). I added it to have quick access to reset the board/software at any time without opening the box. Simply connects the RESET pin to Ground to make the reset.
Bottom part: needed if you want to use a DC motor and move the machine around. This is the arduino motor shield, it is mounted on top of the Arduino, so all of the pins of both boards are connected to each other. I am showing them below as separated just for clarity, hopefully! This shield can control up to 2 DC motor (2 channels A/B) with incredible ease. I am using only one now. It allows to set in the code, the direction, speed and breaks of any of the available 2 channels/motors. I am using channel B as shown bellow. where pin
13 for direction, pin 11 for speed and pin 8 for brakes control.
The battery is a 11.1 / 1000 mA, connected to the shield, thus feeding the DC motor , the main arduino board and the servos. if you are not using the shield, then you can directly connect the battery to the power barrel on the main arduino board.
Top part: The arduino main board ,2 servos and switch connectivity. Note that as mentioned previously you can replace the whole debounce (top-right) circuit with a simple resistor/switch.
Both "door" and "hand" servos are powered directly from the 5v pin on the Arduino. The signal wires are connected to pin 9 and 10 respectively. These pins supports PWM, thus I am using them to control the angle/position of the servo (from 0 to 180 degree max).
Note: the red push button is just a reset (similar to the one found on the face of the arduino board). I added it to have quick access to reset the board/software at any time without opening the box. Simply connects the RESET pin to Ground to make the reset.
Bottom part: needed if you want to use a DC motor and move the machine around. This is the arduino motor shield, it is mounted on top of the Arduino, so all of the pins of both boards are connected to each other. I am showing them below as separated just for clarity, hopefully! This shield can control up to 2 DC motor (2 channels A/B) with incredible ease. I am using only one now. It allows to set in the code, the direction, speed and breaks of any of the available 2 channels/motors. I am using channel B as shown bellow. where pin
13 for direction, pin 11 for speed and pin 8 for brakes control.
The battery is a 11.1 / 1000 mA, connected to the shield, thus feeding the DC motor , the main arduino board and the servos. if you are not using the shield, then you can directly connect the battery to the power barrel on the main arduino board.
Step 5: Fixing the Servos
Here is the hand and door servos installed:
You need first to install the servos in their correct places, this needs a bit of trial and error until you get the needed movement. You can use the Servo example provided in the arduino IDE in order to test the servos and the amount of deflection needed for each, and thus decide on the final positioning and arms length for the door and hand servos.
The arms are simply made from balsa wood, I made them thicker to withstand some tearing.
Below is how I did it. Just one note about the hand servo. You will need the servo shaft to be directly under the edge of the cover (as shown below), this to avoid having hard time in making a hand that can successfully hit the switch, and also be able to go back into the box.
You need first to install the servos in their correct places, this needs a bit of trial and error until you get the needed movement. You can use the Servo example provided in the arduino IDE in order to test the servos and the amount of deflection needed for each, and thus decide on the final positioning and arms length for the door and hand servos.
The arms are simply made from balsa wood, I made them thicker to withstand some tearing.
Below is how I did it. Just one note about the hand servo. You will need the servo shaft to be directly under the edge of the cover (as shown below), this to avoid having hard time in making a hand that can successfully hit the switch, and also be able to go back into the box.
Step 6: Connecting the Switch, Servos and Arduino
Make all the connections seen in the schematic step, and try to put all inside the box similar to what's shown above:
Step 7: Optional: the DC Motor and Battery
Above is how I put the DC motor gear box (taken from a cheap RC car), also showing the batter and switch placement.
Make sure to place the gear as centered as possible to balance the box, you may need to put some weight inside the box to balance it more after final assembly, as the battery I used is a bit heavy (100 gr) and caused some wiggling when switching or moving the box.
Note that the dc motor speed is controlled by pin 11, Direction by pin 13, and brakes on pin 8, these are considered channel B on the arduino motor shield.
Through out the assembly I tried to hide all wires switches and other components in order to have a simple authentic box. This applies to the wheels, so I made a rim around them that hosts both the wheels and battery, and also provides access to the red reset button.
Make sure to place the gear as centered as possible to balance the box, you may need to put some weight inside the box to balance it more after final assembly, as the battery I used is a bit heavy (100 gr) and caused some wiggling when switching or moving the box.
Note that the dc motor speed is controlled by pin 11, Direction by pin 13, and brakes on pin 8, these are considered channel B on the arduino motor shield.
Through out the assembly I tried to hide all wires switches and other components in order to have a simple authentic box. This applies to the wheels, so I made a rim around them that hosts both the wheels and battery, and also provides access to the red reset button.
Step 8: The Code
In order to write code for arduino, you need the arduino IDE (download it here)
I am using the version 1.0.5, just make sure to select the correct COM port (windows) and board type from within the IDE, then use the below code, and hit upload.
NOTE: The values I used in the code to set the max deflection of the servos are made exactly for this box taking in account the servos positioning, axis and angles. I got this through trial. So you need to devise your own values according to your box and servo placement.
For reference, below are the min/max values I used for each servo as you will see them occurring in the code:
The door:
80 hiding, 155 fully open
The hand:
0 hiding, 129 pushing on switch
Here is the code (Better to download the file attached to this step instead of copy/paste from below):
#include <Servo.h>
Servo doorServo;
Servo handServo;
int switch_pin = 2; //set switch on pin 2
//motor variables
int motorThrottle=11;
int motorDirection = 13;
int motorBrake=8;
int pos = 0;
int selectedMove = 0; //move selector
int Testmove = 0; //test mode: set to move number to test only one selected move
//(set to Zero to run normally i.e: roundrobbin on amm moves)
void setup()
{
Serial.begin(9600);
pinMode(switch_pin, INPUT);
doorServo.attach(9); //set door servo on Pin 9 pwm
handServo.attach(10); //set hand servo on Pin 10 pwm
doorServo.write(80); //set door to hiding position
handServo.write(0); //set hand to hiding position
//Setup Channel B, (since Channel A is reserved by door and hand servos and can't ber used at same time)
pinMode(motorDirection, OUTPUT); //Initiates Motor Channel B pin
pinMode(motorBrake, OUTPUT); //Initiates Brake Channel B pin
}
void loop()
{
if (Testmove != 0) {
selectedMove = Testmove;
}
//if the switch is on, then move door and hand to switch it off...
if(digitalRead(switch_pin) == HIGH)
{
if (selectedMove > 10) { selectedMove = 0; } //when all moves are played, repeat the moves from beginning
// below are all the "Moves" i designed so far, each is a function that is written below in the code, execuse the names, abit strange? yes as some are written in arabic
if (selectedMove == 0) { switchoff(); }
else if (selectedMove == 1) { switchoff(); }
else if (selectedMove == 2) { switchoffbitaraddod(); }
else if (selectedMove == 3) { crazydoor(); }
else if (selectedMove == 4) { crazyslow(); }
else if (selectedMove == 5) { m7anika7anika(); }
else if (selectedMove == 6) { m3alla2(); }
else if (selectedMove == 7) { switchoff(); }
else if (selectedMove == 8) { matrix(); }
else if (selectedMove == 9) { sneak(); }
else if (selectedMove == 10) { zee7(); }
if (Testmove == 0) {
selectedMove++; //swith to next move if not in test mode
}
}
}
// Library of moves
// basic move
void switchoff()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
// move 3: open and wait, then move hand and wait, then switch of and hide
void switchoffbitaraddod()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(800);
//Moving hand
for(pos = 0; pos < 100; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(1000);
for(pos = 100; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=5)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
//move 4: open door then close it many times, wait, then quickly reoprn a nd switch off and hide.
void crazydoor()
{
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//Moving door
for(pos = 80; pos < 110; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 110; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//----of switch of----//
//Moving door
for(pos = 80; pos < 155; pos = 8)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 3)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=3)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
}
// move 5: open door, then move hand very slowly forward and back to hiding very slowly, then quickly close door
void crazyslow()
{
//Moving door
for(pos = 80; pos < 155; pos = 1)
{
doorServo.write(pos);
delay(30);
}
//Moving hand
for(pos = 0; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
//hiding hand
for(pos = 129; pos>=0; pos-=1)
{
handServo.write(pos);
delay(30);
}
//hiding door
for(pos = 155; pos>=125; pos-=1)
{
doorServo.write(pos);
delay(30);
}
delay(100);
for(pos = 125; pos>=80; pos-=4)
{
doorServo.write(pos);
delay(15);
}
}
//move 6:
void m7anika7anika() {
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 70; pos = 1)
{
handServo.write(pos);
delay(15);
}
delay(800);
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
handServo.write(40);
delay(1000);
//Moving hand
for(pos = 40; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
for(pos = 120; pos>=80; pos -= 1)
{
doorServo.write(pos);
delay(15);
}
}
void m3alla2(){
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 127; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
delay(2000);
for(pos = 130; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 155; pos>=140; pos-=3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 140; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 127; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void matrix()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 80; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 80; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(300);
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(10);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void sneak()
{
//Moving door
for(pos = 80; pos < 130; pos = 1)
{
doorServo.write(pos);
delay(30);
}
delay(2000);
//Moving hand
for(pos = 0; pos < 40; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(500);
for(pos = 130; pos < 155; pos = 4)
{
doorServo.write(pos);
delay(15);
}
delay(100);
for(pos = 40; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void zee7()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(2000);
//forward @ half speed to the left
digitalWrite(motorDirection, LOW); //Establishes RIGHT direction of Channel B
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at full speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
delay(2000);
//backward @ half speed to the right
digitalWrite(motorDirection, HIGH); //Establishes LEFT direction of Channel B
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at half speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
delay(1000);
//Moving hand
for(pos = 0; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
I am using the version 1.0.5, just make sure to select the correct COM port (windows) and board type from within the IDE, then use the below code, and hit upload.
NOTE: The values I used in the code to set the max deflection of the servos are made exactly for this box taking in account the servos positioning, axis and angles. I got this through trial. So you need to devise your own values according to your box and servo placement.
For reference, below are the min/max values I used for each servo as you will see them occurring in the code:
The door:
80 hiding, 155 fully open
The hand:
0 hiding, 129 pushing on switch
Here is the code (Better to download the file attached to this step instead of copy/paste from below):
#include <Servo.h>
Servo doorServo;
Servo handServo;
int switch_pin = 2; //set switch on pin 2
//motor variables
int motorThrottle=11;
int motorDirection = 13;
int motorBrake=8;
int pos = 0;
int selectedMove = 0; //move selector
int Testmove = 0; //test mode: set to move number to test only one selected move
//(set to Zero to run normally i.e: roundrobbin on amm moves)
void setup()
{
Serial.begin(9600);
pinMode(switch_pin, INPUT);
doorServo.attach(9); //set door servo on Pin 9 pwm
handServo.attach(10); //set hand servo on Pin 10 pwm
doorServo.write(80); //set door to hiding position
handServo.write(0); //set hand to hiding position
//Setup Channel B, (since Channel A is reserved by door and hand servos and can't ber used at same time)
pinMode(motorDirection, OUTPUT); //Initiates Motor Channel B pin
pinMode(motorBrake, OUTPUT); //Initiates Brake Channel B pin
}
void loop()
{
if (Testmove != 0) {
selectedMove = Testmove;
}
//if the switch is on, then move door and hand to switch it off...
if(digitalRead(switch_pin) == HIGH)
{
if (selectedMove > 10) { selectedMove = 0; } //when all moves are played, repeat the moves from beginning
// below are all the "Moves" i designed so far, each is a function that is written below in the code, execuse the names, abit strange? yes as some are written in arabic
if (selectedMove == 0) { switchoff(); }
else if (selectedMove == 1) { switchoff(); }
else if (selectedMove == 2) { switchoffbitaraddod(); }
else if (selectedMove == 3) { crazydoor(); }
else if (selectedMove == 4) { crazyslow(); }
else if (selectedMove == 5) { m7anika7anika(); }
else if (selectedMove == 6) { m3alla2(); }
else if (selectedMove == 7) { switchoff(); }
else if (selectedMove == 8) { matrix(); }
else if (selectedMove == 9) { sneak(); }
else if (selectedMove == 10) { zee7(); }
if (Testmove == 0) {
selectedMove++; //swith to next move if not in test mode
}
}
}
// Library of moves
// basic move
void switchoff()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
// move 3: open and wait, then move hand and wait, then switch of and hide
void switchoffbitaraddod()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(800);
//Moving hand
for(pos = 0; pos < 100; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(1000);
for(pos = 100; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=5)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
//move 4: open door then close it many times, wait, then quickly reoprn a nd switch off and hide.
void crazydoor()
{
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//Moving door
for(pos = 80; pos < 110; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 110; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//----of switch of----//
//Moving door
for(pos = 80; pos < 155; pos = 8)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 3)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=3)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
}
// move 5: open door, then move hand very slowly forward and back to hiding very slowly, then quickly close door
void crazyslow()
{
//Moving door
for(pos = 80; pos < 155; pos = 1)
{
doorServo.write(pos);
delay(30);
}
//Moving hand
for(pos = 0; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
//hiding hand
for(pos = 129; pos>=0; pos-=1)
{
handServo.write(pos);
delay(30);
}
//hiding door
for(pos = 155; pos>=125; pos-=1)
{
doorServo.write(pos);
delay(30);
}
delay(100);
for(pos = 125; pos>=80; pos-=4)
{
doorServo.write(pos);
delay(15);
}
}
//move 6:
void m7anika7anika() {
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 70; pos = 1)
{
handServo.write(pos);
delay(15);
}
delay(800);
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
handServo.write(40);
delay(1000);
//Moving hand
for(pos = 40; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
for(pos = 120; pos>=80; pos -= 1)
{
doorServo.write(pos);
delay(15);
}
}
void m3alla2(){
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 127; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
delay(2000);
for(pos = 130; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 155; pos>=140; pos-=3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 140; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 127; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void matrix()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 80; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 80; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(300);
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(10);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void sneak()
{
//Moving door
for(pos = 80; pos < 130; pos = 1)
{
doorServo.write(pos);
delay(30);
}
delay(2000);
//Moving hand
for(pos = 0; pos < 40; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(500);
for(pos = 130; pos < 155; pos = 4)
{
doorServo.write(pos);
delay(15);
}
delay(100);
for(pos = 40; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void zee7()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(2000);
//forward @ half speed to the left
digitalWrite(motorDirection, LOW); //Establishes RIGHT direction of Channel B
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at full speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
delay(2000);
//backward @ half speed to the right
digitalWrite(motorDirection, HIGH); //Establishes LEFT direction of Channel B
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at half speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
delay(1000);
//Moving hand
for(pos = 0; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
Attachments
Step 9: Optional Update: Movement Detection
I've got a proximity sensor today (GP2Y0A21), so now I can use it to move the machine if the user approaches the toggle switch :)
For this additional step, I am assuming you've already built the wheels to allow movement (see optional step 8). All what is left now is to add the sensor and make some modifications in the code for movement detection.
The sensor has 3 wires, yellow signal wire connected to pin 3 on arduino, while the red and black are connected to v5 and ground respectively on the board.
I've placed the sensor hidden under the door as shown in the picture, thus it can 'see' the user's movement only when the door opens. exactly what I need!
I had also to change the code a bit (here) to implement a movement detection routine (see the code, in function zee7()), such that when movement is detected, the box will move, first to the right, then to the left, then the hand flips the switch and hide. The code is heavily commented, I hope I didn't overdo it!
You can find the updated code below, also note that I've made the machine picks the moves randomly, this can be changed back to sequential by simply setting boolean randomize = false;
Thanks ALL for your great suggestions!
The code (Better to download the file attached to this step instead of copy/paste from below):
#include
Servo doorServo;
Servo handServo;
unsigned long TimerA,TimerB; //timer for the movement detection routine
int switch_pin = 2; //set switch on pin 2
//motor variables
int motorThrottle=11;
int motorDirection = 13;
int motorBrake=8;
//Distance Variables
int motionPin = 3; //motion sensor pin on analog 0
int lastDist = 0; //to remember last distance
int currentDist = 0;
int thresh = 200; //Threshold for Movement (set it such that you get the desired sensetivity of motion detection)
int pos = 0;
int selectedMove = 0; //move selector
int Testmove = 0; //test mode: set to move number to test only one selected move
//(set to Zero to run normally i.e: roundrobbin on all moves)
boolean randomize = true; // if true, the box will do movers randomly, if set to false then the moves will be done sequencially from 1 to 10
void setup()
{
Serial.begin(9600);
pinMode(switch_pin, INPUT);
doorServo.attach(9); //set door servo on Pin 9 pwm
handServo.attach(10); //set hand servo on Pin 10 pwm
doorServo.write(80); //set door to hiding position
handServo.write(0); //set hand to hiding position
//Setup Channel B, since Channel A is reserved by door and hand servos and can't ber used at same time
pinMode(motorDirection, OUTPUT); //Initiates Motor Channel B pin
pinMode(motorBrake, OUTPUT); //Initiates Brake Channel B pin
}
void loop()
{
//if the switch is on, then move door and hand to switch it off...
if(digitalRead(switch_pin) == HIGH)
{
if (Testmove == 0)
{
if(randomize == false)
{
if (selectedMove > 10)
{
selectedMove = 0; //when all moves are played, repeat the moves from beginning
}
}
else
{
selectedMove = random(11);
}
}
else
{
selectedMove = Testmove;
}
Serial.println(selectedMove);
if (selectedMove == 0) { switchoff(); }
else if (selectedMove == 1) { switchoff(); }
else if (selectedMove == 2) { switchoffbitaraddod(); }
else if (selectedMove == 3) { crazydoor(); }
else if (selectedMove == 4) { crazyslow(); }
else if (selectedMove == 5) { m7anika7anika(); }
else if (selectedMove == 6) { m3alla2(); }
else if (selectedMove == 7) { switchoff(); }
else if (selectedMove == 8) { matrix(); }
else if (selectedMove == 9) { sneak(); }
else if (selectedMove == 10) { zee7(); }
if (Testmove == 0 && randomize == false) {
selectedMove++; //switch to next move if not in test mode
}
}
}
// Library of moves
// basic move
void switchoff()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
// move 3: open and wait, then move hand and wait, then switch of and hide
void switchoffbitaraddod()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(800);
//Moving hand
for(pos = 0; pos < 100; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(1000);
for(pos = 100; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=5)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
//move 4: open door then close it many times, wait, then quickly reoprn a nd switch off and hide.
void crazydoor()
{
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//Moving door
for(pos = 80; pos < 110; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 110; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//----of switch of----//
//Moving door
for(pos = 80; pos < 155; pos = 8)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 3)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=3)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
}
// move 5: open door, then move hand very slowly forward and back to hiding very slowly, then quickly close door
void crazyslow()
{
//Moving door
for(pos = 80; pos < 155; pos = 1)
{
doorServo.write(pos);
delay(30);
}
//Moving hand
for(pos = 0; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
//hiding hand
for(pos = 129; pos>=0; pos-=1)
{
handServo.write(pos);
delay(30);
}
//hiding door
for(pos = 155; pos>=125; pos-=1)
{
doorServo.write(pos);
delay(30);
}
delay(100);
for(pos = 125; pos>=80; pos-=4)
{
doorServo.write(pos);
delay(15);
}
}
//move 6:
void m7anika7anika() {
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 70; pos = 1)
{
handServo.write(pos);
delay(15);
}
delay(800);
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
handServo.write(40);
delay(1000);
//Moving hand
for(pos = 40; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
for(pos = 120; pos>=80; pos -= 1)
{
doorServo.write(pos);
delay(15);
}
}
void m3alla2(){
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 127; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
delay(2000);
for(pos = 130; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 155; pos>=140; pos-=3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 140; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 127; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void matrix()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 80; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 80; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(300);
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(10);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void sneak()
{
//Moving door
for(pos = 80; pos < 130; pos = 1)
{
doorServo.write(pos);
delay(30);
}
delay(2000);
//Moving hand
for(pos = 0; pos < 40; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(500);
for(pos = 130; pos < 155; pos = 4)
{
doorServo.write(pos);
delay(15);
}
delay(100);
for(pos = 40; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
//this move will open the door and wait for movement, if detected near the switch,
//the box will move first to the Right, and then to the Left, and finally the hand moves and turns off the switch
void zee7()
{
//open door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(1200); //wait to stabilize sensor readings after opening the door
// set the timer and read the sensor
// wait for movement that exceeds threshold or if timer expires (5 sec),
lastDist= analogRead(motionPin);
TimerA= millis();
while(millis()-TimerA <= 5000)
{
currentDist = analogRead(motionPin);
//Does the current distance deviate from the last distance by more than the threshold?
//if yes, then move box to the right
if ((currentDist > lastDist thresh || currentDist < lastDist - thresh) || currentDist > 300)
{
//forward @ half speed to the left (choose the speed and the delay
//(i.e delay(300) according to you box and battery voltage such that the movement throw is desirable)
digitalWrite(motorDirection, LOW); //Establishes RIGHT direction of Channel A
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at full speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
break;
}
lastDist = currentDist;
}
// set the timer and read the sensor
// wait for movement that exceeds threshold or if timer expires (5 sec),
delay(1200); //wait to stabilize sensor readings after opening the door
lastDist= analogRead(motionPin);
TimerB= millis();
while(millis()-TimerB <= 5000)
{
currentDist = analogRead(motionPin);
//did the box already move to the right and the current distance deviate from the last distance by more than the threshold?
//If yes, then move to the Left
if ((currentDist > lastDist thresh || currentDist < lastDist - thresh) || currentDist > 300)
{
//backward @ half speed to the right (choose the speed and the delay
//(i.e delay(300) according to you box and battery voltage such that the movement throw is desirable)
digitalWrite(motorDirection, HIGH); //Establishes LEFT direction of Channel B
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at half speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
break;
}
lastDist = currentDist;
}
delay(100);
//Moving hand
for(pos = 0; pos < 130; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 130; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
For this additional step, I am assuming you've already built the wheels to allow movement (see optional step 8). All what is left now is to add the sensor and make some modifications in the code for movement detection.
The sensor has 3 wires, yellow signal wire connected to pin 3 on arduino, while the red and black are connected to v5 and ground respectively on the board.
I've placed the sensor hidden under the door as shown in the picture, thus it can 'see' the user's movement only when the door opens. exactly what I need!
I had also to change the code a bit (here) to implement a movement detection routine (see the code, in function zee7()), such that when movement is detected, the box will move, first to the right, then to the left, then the hand flips the switch and hide. The code is heavily commented, I hope I didn't overdo it!
You can find the updated code below, also note that I've made the machine picks the moves randomly, this can be changed back to sequential by simply setting boolean randomize = false;
Thanks ALL for your great suggestions!
The code (Better to download the file attached to this step instead of copy/paste from below):
#include
Servo doorServo;
Servo handServo;
unsigned long TimerA,TimerB; //timer for the movement detection routine
int switch_pin = 2; //set switch on pin 2
//motor variables
int motorThrottle=11;
int motorDirection = 13;
int motorBrake=8;
//Distance Variables
int motionPin = 3; //motion sensor pin on analog 0
int lastDist = 0; //to remember last distance
int currentDist = 0;
int thresh = 200; //Threshold for Movement (set it such that you get the desired sensetivity of motion detection)
int pos = 0;
int selectedMove = 0; //move selector
int Testmove = 0; //test mode: set to move number to test only one selected move
//(set to Zero to run normally i.e: roundrobbin on all moves)
boolean randomize = true; // if true, the box will do movers randomly, if set to false then the moves will be done sequencially from 1 to 10
void setup()
{
Serial.begin(9600);
pinMode(switch_pin, INPUT);
doorServo.attach(9); //set door servo on Pin 9 pwm
handServo.attach(10); //set hand servo on Pin 10 pwm
doorServo.write(80); //set door to hiding position
handServo.write(0); //set hand to hiding position
//Setup Channel B, since Channel A is reserved by door and hand servos and can't ber used at same time
pinMode(motorDirection, OUTPUT); //Initiates Motor Channel B pin
pinMode(motorBrake, OUTPUT); //Initiates Brake Channel B pin
}
void loop()
{
//if the switch is on, then move door and hand to switch it off...
if(digitalRead(switch_pin) == HIGH)
{
if (Testmove == 0)
{
if(randomize == false)
{
if (selectedMove > 10)
{
selectedMove = 0; //when all moves are played, repeat the moves from beginning
}
}
else
{
selectedMove = random(11);
}
}
else
{
selectedMove = Testmove;
}
Serial.println(selectedMove);
if (selectedMove == 0) { switchoff(); }
else if (selectedMove == 1) { switchoff(); }
else if (selectedMove == 2) { switchoffbitaraddod(); }
else if (selectedMove == 3) { crazydoor(); }
else if (selectedMove == 4) { crazyslow(); }
else if (selectedMove == 5) { m7anika7anika(); }
else if (selectedMove == 6) { m3alla2(); }
else if (selectedMove == 7) { switchoff(); }
else if (selectedMove == 8) { matrix(); }
else if (selectedMove == 9) { sneak(); }
else if (selectedMove == 10) { zee7(); }
if (Testmove == 0 && randomize == false) {
selectedMove++; //switch to next move if not in test mode
}
}
}
// Library of moves
// basic move
void switchoff()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
// move 3: open and wait, then move hand and wait, then switch of and hide
void switchoffbitaraddod()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(800);
//Moving hand
for(pos = 0; pos < 100; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(1000);
for(pos = 100; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=5)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
//move 4: open door then close it many times, wait, then quickly reoprn a nd switch off and hide.
void crazydoor()
{
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//Moving door
for(pos = 80; pos < 110; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 110; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//Moving door
for(pos = 80; pos < 125; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(700);
//hiding door
for(pos = 125; pos>=80; pos-=5)
{
doorServo.write(pos);
delay(15);
}
//----of switch of----//
//Moving door
for(pos = 80; pos < 155; pos = 8)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 129; pos = 3)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=3)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=15)
{
doorServo.write(pos);
delay(15);
}
}
// move 5: open door, then move hand very slowly forward and back to hiding very slowly, then quickly close door
void crazyslow()
{
//Moving door
for(pos = 80; pos < 155; pos = 1)
{
doorServo.write(pos);
delay(30);
}
//Moving hand
for(pos = 0; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
//hiding hand
for(pos = 129; pos>=0; pos-=1)
{
handServo.write(pos);
delay(30);
}
//hiding door
for(pos = 155; pos>=125; pos-=1)
{
doorServo.write(pos);
delay(30);
}
delay(100);
for(pos = 125; pos>=80; pos-=4)
{
doorServo.write(pos);
delay(15);
}
}
//move 6:
void m7anika7anika() {
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 70; pos = 1)
{
handServo.write(pos);
delay(15);
}
delay(800);
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
//hiding door
for(pos = 130; pos < 155; pos =3)
{
doorServo.write(pos);
delay(15);
}
handServo.write(40);
delay(1000);
//Moving hand
for(pos = 40; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
for(pos = 120; pos>=80; pos -= 1)
{
doorServo.write(pos);
delay(15);
}
}
void m3alla2(){
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 127; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=130; pos-=3)
{
doorServo.write(pos);
delay(15);
}
delay(2000);
for(pos = 130; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 155; pos>=140; pos-=3)
{
doorServo.write(pos);
delay(15);
}
for(pos = 140; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 127; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void matrix()
{
//Moving door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
//Moving hand
for(pos = 0; pos < 80; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 80; pos < 129; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(300);
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(10);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
void sneak()
{
//Moving door
for(pos = 80; pos < 130; pos = 1)
{
doorServo.write(pos);
delay(30);
}
delay(2000);
//Moving hand
for(pos = 0; pos < 40; pos = 1)
{
handServo.write(pos);
delay(30);
}
delay(500);
for(pos = 130; pos < 155; pos = 4)
{
doorServo.write(pos);
delay(15);
}
delay(100);
for(pos = 40; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(500);
//hiding hand
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 90; pos = 4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 90; pos>=70; pos-=4)
{
handServo.write(pos);
delay(15);
}
delay(100);
for(pos = 70; pos < 129; pos = 4)
{
handServo.write(pos);
delay(15);
}
for(pos = 129; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}
//this move will open the door and wait for movement, if detected near the switch,
//the box will move first to the Right, and then to the Left, and finally the hand moves and turns off the switch
void zee7()
{
//open door
for(pos = 80; pos < 155; pos = 3)
{
doorServo.write(pos);
delay(15);
}
delay(1200); //wait to stabilize sensor readings after opening the door
// set the timer and read the sensor
// wait for movement that exceeds threshold or if timer expires (5 sec),
lastDist= analogRead(motionPin);
TimerA= millis();
while(millis()-TimerA <= 5000)
{
currentDist = analogRead(motionPin);
//Does the current distance deviate from the last distance by more than the threshold?
//if yes, then move box to the right
if ((currentDist > lastDist thresh || currentDist < lastDist - thresh) || currentDist > 300)
{
//forward @ half speed to the left (choose the speed and the delay
//(i.e delay(300) according to you box and battery voltage such that the movement throw is desirable)
digitalWrite(motorDirection, LOW); //Establishes RIGHT direction of Channel A
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at full speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
break;
}
lastDist = currentDist;
}
// set the timer and read the sensor
// wait for movement that exceeds threshold or if timer expires (5 sec),
delay(1200); //wait to stabilize sensor readings after opening the door
lastDist= analogRead(motionPin);
TimerB= millis();
while(millis()-TimerB <= 5000)
{
currentDist = analogRead(motionPin);
//did the box already move to the right and the current distance deviate from the last distance by more than the threshold?
//If yes, then move to the Left
if ((currentDist > lastDist thresh || currentDist < lastDist - thresh) || currentDist > 300)
{
//backward @ half speed to the right (choose the speed and the delay
//(i.e delay(300) according to you box and battery voltage such that the movement throw is desirable)
digitalWrite(motorDirection, HIGH); //Establishes LEFT direction of Channel B
digitalWrite(motorBrake, LOW); //Disengage the Brake for Channel B
analogWrite(motorThrottle, 100); //Spins the motor on Channel B at half speed
delay(300);
digitalWrite(motorBrake, HIGH); //Eengage the Brake for Channel B
break;
}
lastDist = currentDist;
}
delay(100);
//Moving hand
for(pos = 0; pos < 130; pos = 4)
{
handServo.write(pos);
delay(15);
}
//hiding hand
for(pos = 130; pos>=0; pos-=4)
{
handServo.write(pos);
delay(15);
}
//hiding door
for(pos = 155; pos>=80; pos-=3)
{
doorServo.write(pos);
delay(15);
}
}