//Useless Box "Hank" Style by Blayder
//the code is free to change, you can do whatever you want with it. Have fun :D
//You need the Servo.h library to use this code.
#include <ESP32Servo.h>
Servo doorServo;
Servo handServo;
int switch_pin = 15; //set switch on pin 15
int vibration_motor_pin = 14;
int door_pin = 13;
int hand_pin = 12;
int angry_light = 26;
int hand_min = 160; //Calibration Value Hand Min
int hand_max = 0; //Calibration Value Hand Max
int motion_sensor_pin = 27; //pin connected to this motion sensor
int Servo_power_pin = 5; //
int door_min = 180; //Calibration Value Door Min
int door_max = 30; //Calibration Value Door Max
unsigned long peek_period = 300000; //Variable timer - peek every 5 min (300 000)
int pos = 0;
int AktHandpos = 0; //current HandPos
int AktDoorpos = 0; //current Doorpos
int selected_move = 0; //Move selector
int random_mode = LOW; //After ran through all the modes, set to random
int already_reset = LOW;
unsigned long time1 = 0; //saves switch trigger time
unsigned long time2 = 0; //time counting up since last switch
unsigned long time3 = 60000; //Variable timer - used to reset random mode
unsigned long peek_period_millis; //Variable timer - peek every 10 min
int random_block = 0;
bool motion_detected_once = LOW;
bool random_chosen = LOW;
bool already_subtracted = LOW;
void setup(){
Serial.begin(9600);
pinMode(switch_pin, INPUT_PULLUP);
pinMode(motion_sensor_pin, INPUT_PULLDOWN);
pinMode(vibration_motor_pin, OUTPUT);
pinMode(angry_light, OUTPUT);
pinMode(Servo_power_pin, OUTPUT);
doorServo.attach(door_pin); //set door servo on Pin 9 pwm
handServo.attach(hand_pin); //set hand servo on Pin 10 pwm
doorServo.write(hand_min); //set door to hiding position
handServo.write(hand_min); //set hand to hiding position
}
void loop(){
Reset();
time2 = millis() - time1;
//SPECIAL TIMER MODE, PEEK EVERY "peek_period" MIN
if((millis() > peek_period_millis + peek_period) & digitalRead(motion_sensor_pin) == HIGH){
peek_period_millis = millis();
digitalWrite(Servo_power_pin, HIGH);
delay (100);
{ PEEK(); }
}
if(digitalRead(motion_sensor_pin) == HIGH && motion_detected_once == LOW){
Serial.println("motion detected");
motion_detected_once = HIGH;
}
else if(digitalRead(motion_sensor_pin) == LOW && motion_detected_once == HIGH){
motion_detected_once = LOW;
}
//if the switch is on, then Move door and hand to switch it off...
else if(digitalRead(switch_pin) == LOW){
time1 = millis();
digitalWrite(Servo_power_pin, HIGH);
delay (100);
already_reset = LOW;
already_subtracted = LOW;
if(random_mode == LOW){
selected_move++;
}
if (selected_move > 11) {
random_mode = HIGH;
Serial.println("random mode set");
}
if (random_mode == HIGH){
selected_move = random(1,11); //when all Moves are played, do random Moves
}
if (selected_move == 0) { MOVE1(); }
else if (selected_move == 1) { MOVE1(); }
else if (selected_move == 2) { MOVE2(); }
else if (selected_move == 3) { MOVE3(); }
else if (selected_move == 4) { MOVE4(); }
else if (selected_move == 5) { MOVE5(); }
else if (selected_move == 6) { MOVE6(); }
else if (selected_move == 7) { MOVE7(); }
else if (selected_move == 8) { MOVE8(); }
else if (selected_move == 9) { MOVE9(); }
else if (selected_move == 10) { MOVE10(); }
else if (selected_move == 11) { MOVE11(); }
//if toggle te switch and random mode is on, set random block on
if(random_mode == HIGH){
random_block = random(0,3); // 25% chance of getting blocked 0,1,2,3 (1/4)
Serial.print("random block set to: ");
Serial.println(random_block);
}
}
else{
digitalWrite(Servo_power_pin, LOW);
}
if(time2 > time3 && already_reset == LOW){
already_reset = HIGH;
random_mode = LOW;
Serial.println("random mode reset - low");
random_chosen = LOW;
selected_move = 0;
digitalWrite(Servo_power_pin, LOW);
}
//if random block is on and motion is detected, run randomblock and re-run if random block is not 0
// specifically like this so can still run once random mode is reset to off
if(random_block == 0 && motion_detected_once == HIGH){
random_block = random(0,3); // 25% chance of getting blocked 0,1,2,3 (1/4)
{ BLOCK(); }
Serial.print("random block set to: ");
Serial.println(random_block);
}
}
void Reset(){
AktDoorpos = door_min;
AktHandpos = hand_min;
}
void DoorOpen(int offset,int Speed){
//General routine for opening the door, an offset +/-
//can be specified how far it Moves, in addition to the speed
for(pos = AktDoorpos; pos > door_max + offset; pos -= Speed){
doorServo.write(pos);
delay(8);
}
AktDoorpos = pos;
}
void DoorClose(int offset,int Speed){
//General routine for closing the door, an offset +/-
//can be specified how far it Moves, in addition to the speed
for(pos = AktDoorpos; pos<=door_min+offset; pos+=Speed){
doorServo.write(pos);
delay(8);
}
AktDoorpos = pos;
}
void HandToPin(int offset,int Speed){
//General routine for Move the Hand to the Pin, an offset +/-
//can be specified how far it Moves, in addition to the speed
for(pos = AktHandpos; pos > hand_max + offset; pos -= Speed){
handServo.write(pos);
delay(10);
}
AktHandpos = pos;
}
void HandToHide(int offset,int Speed){
//General routine for Move the Hand to the Hiding Position, an offset +/-
//can be specified how far it Moves, in addition to the speed
for(pos = AktHandpos; pos<=hand_min+offset; pos+=Speed){
handServo.write(pos);
delay(10);
}
AktHandpos = pos;
}
//---------------------
// Library of Moves
//---------------------
void PEEK(){
Serial.println("PEEK");
random_chosen = LOW;
DoorOpen(80,1); //higher offset value in DoorOpen = more closed
delay(random(2000,4000));
DoorClose(0,1);
delay (100);
digitalWrite(Servo_power_pin, LOW);
}
void BLOCK(){
digitalWrite(Servo_power_pin, HIGH);
delay (100);
Serial.println("BLOCK");
DoorOpen(25,4);
delay(100);
HandToPin(-7,5);
delay(random(5000,10000));
HandToHide(0,5);
DoorClose(0,5);
delay (2000);
digitalWrite(Servo_power_pin, LOW);
}
void MOVE1(){
Serial.println("Move 1");
DoorOpen(60,1); //higher offset value in DoorOpen = more closed
HandToPin(-7,2);
delay(100);
HandToHide(0,3);
DoorClose(0,3);
}
void MOVE2(){
Serial.println("Move 2");
DoorOpen(60,3);
delay(random(1000,2000));
DoorClose(0,3);
delay(random(100,2000));
DoorOpen(20,3);
HandToPin(-7,5);
delay(100);
HandToHide(0,5);
DoorClose(0,3);
delay(random(100,2000));
}
void MOVE3(){
Serial.println("Move 3");
DoorOpen(20,3);
delay(random(800,2000));
HandToPin(15,7);
delay(random(500,2000));
HandToPin(-7,5);
HandToHide(0,5);
DoorClose(0,3);
}
void MOVE4(){
Serial.println("Move 4");
DoorOpen(20,4);
delay(100);
HandToPin(-7,5);
delay(100);
HandToHide(0,5);
DoorClose(0,5);
delay(2000);
DoorOpen(20,4);
delay(random(1000,3000));
DoorClose(0,1);
}
void MOVE5(){
Serial.println("Move 5");
delay(random(100,2000));
DoorOpen(20,3);
HandToPin(-7,5);
delay(800);
DoorClose(-90,2);
DoorOpen(20,5);
DoorClose(-90,2);
DoorOpen(20,5);
delay(500);
HandToHide(0,5);
DoorClose(0,2);
}
void MOVE6(){
Serial.println("Move 6");
delay(random(100,2000));
DoorOpen(10,3);
delay(50);
HandToPin(10,5);
delay(1000);
DoorClose(-90,2);
delay(random(100,1000));
HandToPin(-7,5);
delay(200);
HandToHide(0,5);
DoorClose(0,2);
}
void MOVE7(){
Serial.println("Move 7");
delay(random(100,2000));
DoorOpen(10,5);
delay(50);
HandToPin(-7,5);
DoorClose(-90,2);
delay(700);
DoorOpen(0,5);
delay(100);
DoorClose(-90,5);
delay(100);
DoorOpen(0,5);
delay(100);
DoorClose(-90,5);
delay(100);
DoorOpen(10,5);
delay(250);
HandToHide(0,5);
DoorClose(0,2);
}
void MOVE8(){
Serial.println("Move 8");
delay(random(100,2000));
DoorOpen(10,4);
HandToPin(70,10);
delay(1000);
HandToHide(0,10);
delay(1500);
HandToPin(-7,10);
delay(100);
HandToHide(0,10);
DoorClose(0,4);
}
void MOVE9(){
Serial.println("Move 9");
delay(random(100,2000));
digitalWrite(vibration_motor_pin, HIGH);
digitalWrite(angry_light, HIGH);
delay(1000);
digitalWrite(vibration_motor_pin, LOW);
DoorOpen(20,4);
delay(2000);
DoorClose(0,1);
delay(3000);
DoorOpen(20,4);
delay(500);
DoorClose(0,1);
delay(1000);
DoorOpen(0,4);
delay(100);
HandToPin(5,5);
delay(100);
HandToHide(0,10);
delay(100);
HandToPin(5,5);
delay(100);
HandToHide(0,10);
delay(100);
HandToPin(-7,5);
delay(100);
HandToHide(0,5);
DoorClose(0,5);
digitalWrite(angry_light, LOW);
}
void MOVE10(){
Serial.println("Move 10");
delay(random(1000,5000));
DoorOpen(80,random(1,3));
delay(random(100,2000));
DoorClose(0,1);
delay(random(3000,15000));
DoorOpen(0,4);
HandToPin(-7,5);
delay(100);
HandToHide(0,5);
DoorClose(0,3);
delay(random(100,2000));
}
void MOVE11(){
Serial.println("Move 11");
DoorOpen(0,5);
delay(random(100,2000));
HandToPin(-7,5);
delay(100);
HandToHide(0,5);
DoorClose(0,5);
}
2N222 Transistor or similar - cuts power to the servos to save servos and power