//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 VibrationMotorPin = 14;
int AngryLight = 27;
int handMin = 170; //Calibration Value Hand Min
int handMax = 0; //Calibration Value Hand Max
int doorMin = 180; //Calibration Value Door Min
int doorMax = 30; //Calibration Value Door Max
int pos = 0;
int AktHandpos = 0; //current HandPos
int AktDoorpos = 0; //current Doorpos
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)
void setup()
{
Serial.begin(9600);
pinMode(switch_pin, INPUT);
pinMode(VibrationMotorPin, OUTPUT);
pinMode(AngryLight, OUTPUT);
doorServo.attach(12); //set door servo on Pin 9 pwm
handServo.attach(13); //set hand servo on Pin 10 pwm
doorServo.write(handMin); //set door to hiding position
handServo.write(handMin); //set hand to hiding position
Serial.println("UselessBox Hank v0.3");
Serial.println("Testmove: " + String(Testmove));
}
void loop()
{
if (Testmove != 0) {
selectedMove = Testmove;
}
Reset();
//if the switch is on, then move door and hand to switch it off...
if(digitalRead(switch_pin) == HIGH)
{
DisplayPinState();
if (selectedMove > 2) { selectedMove = random(1,10); } //when all moves are played, repeat the moves from beginning
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) { BasicMove5(); }
else if (selectedMove == 6) { BasicMove6(); }
else if (selectedMove == 7) { switchoff(); }
else if (selectedMove == 8) { matrix(); }
else if (selectedMove == 9) { sneak(); }
if (Testmove == 0) {
selectedMove++; //swith to next move if not in test mode
}
}
}
void Reset()
{
AktDoorpos = doorMin;
AktHandpos = handMin;
}
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
Serial.println("door open + offset: " + offset);
//Moving door
for(pos = AktDoorpos; pos > doorMax + offset; pos -= Speed)
{
doorServo.write(pos);
delay(10);
}
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
Serial.println("door close + offset: " + offset);
//Moving door
for(pos = AktDoorpos; pos<=doorMin+offset; pos+=Speed)
{
doorServo.write(pos);
delay(15);
}
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 > handMax + offset; pos -= Speed)
{
handServo.write(pos);
delay(15);
}
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<=handMin+offset; pos+=Speed)
{
handServo.write(pos);
delay(15);
}
AktHandpos = pos;
}
void DisplayPinState()
{
//Display the Current Pin State
if(digitalRead(switch_pin) == LOW)
{
Serial.println("Pin is LOW");
}
else
{
Serial.println("Pin is HIGH");
}
}
void DisplayMove(String Header)
{
//Display the Current Move
if (Serial)
{
Serial.println("-------------");
Serial.println(Header);
Serial.println("-------------");
}
}
//---------------------
// Library of moves
//---------------------
// basic move 1
void switchoff()
{
DisplayMove("Basic Move 1/7");
delay(500);
DisplayPinState();
DoorOpen(0,3);
//Moving hand
HandToPin(0,5);
delay(100);
DisplayPinState();
//hiding hand
HandToHide(0,3);
DoorClose(0,3);
}
// move 2: open and wait, then move hand and wait, then switch of and hide
void switchoffbitaraddod()
{
DisplayMove("Basic Move 2");
//Moving door
DoorOpen(0,3);
//hiding door
DoorClose(0,3);
//Moving door
DoorOpen(0,3);
//hiding door
DoorClose(0,3);
delay(700);
//Moving door
DoorOpen(0,3);
delay(700);
//hiding door
DoorClose(0,1);
delay(1000);
//----of switch of----//
//Moving door
DoorOpen(0,3);
//Moving hand
HandToPin(0,3);
//hiding hand
HandToHide(0,3);
//hiding door
DoorClose(0,3);
}
//move 3: open door then close it many times, wait, then quickly reoprn a nd switch off and hide.
void crazydoor()
{
DisplayMove("Basic Move 3");
//Moving door
DoorOpen(0,3);
delay(800);
//Moving hand
HandToPin(-30,3);
delay(1000);
HandToPin(0,3);
//hiding hand
HandToHide(0,3);
//hiding door
DoorClose(0,3);
}
// move 4: open door, then move hand very slowly forward and back to hiding very slowly, then quickly close door
void crazyslow()
{
DisplayMove("Basic Move 4");
//Moving door
DoorOpen(0,3);
//Moving hand
HandToPin(0,1);
//hiding hand
HandToHide(0,1);
//hiding door
DoorClose(0,5);
delay(2000);
DoorOpen(0,5);
DoorClose(0,1);
}
//move 5: open door, hand to pin, close door, open door and back to hinding hand
void BasicMove5()
{
DisplayMove("Basic Move 5");
//Moving door
DoorOpen(0,3);
//Moving hand
HandToPin(-100,3);
delay(800);
//hiding door
DoorClose(0,2);
//open door
DoorOpen(0,5);
//hiding door
DoorClose(0,2);
//hiding door
DoorOpen(0,5);
delay(500);
//hiding hand
HandToHide(0,3);
DoorClose(0,2);
}
//move 6:
void BasicMove6()
{
DisplayMove("Basic Move 6");
//Moving door
DoorOpen(0,5);
delay(50);
//Moving hand
HandToPin(0,7);
//hiding door
DoorClose(30,2);
delay(700);
DoorOpen(-10,5);
DoorClose(30,5);
DoorOpen(-10,5);
DoorClose(30,5);
DoorOpen(-10,5);
delay(250);
//hiding hand
HandToHide(0,3);
//hiding door
DoorClose(0,2);
}
//move 8:
void matrix()
{
DisplayMove("Basic Move 8");
//Moving door
DoorOpen(0,4);
//Moving hand
HandToPin(70,5);
delay(300);
HandToHide(-10,2);
delay(100);
HandToPin(0,5);
HandToHide(0,1);
//hiding door
DoorClose(0,1);
}
//move 9:
void sneak()
{
DisplayMove("Basic Move 9");
//Moving door
digitalWrite(VibrationMotorPin, HIGH);
digitalWrite(AngryLight, HIGH);
delay(1000);
digitalWrite(VibrationMotorPin, LOW);
DoorOpen(20,4);
delay(2000);
DoorClose(0,1);
delay(3000);
//hiding hand
DoorOpen(30,4);
delay(500);
DoorClose(0,1);
delay(1000);
DoorOpen(0,4);
delay(100);
HandToPin(0,5);
HandToHide(-70,1);
HandToPin(0,5);
HandToHide(-80,1);
HandToPin(0,5);
HandToHide(0,1);
DoorClose(0,1);
digitalWrite(AngryLight, LOW);
}