Introduction: Arduino UselessLegoBox

Picture of Arduino UselessLegoBox

Instruction how to create a uselessbox from lego with an Arduino

Step 1: What Do You Need

Picture of What Do You Need

To create a uselessbox from lego I used the following parts

  • Arduino One
  • 2x Servo motors (SG90)
  • Collision switch
  • Breadboard
  • Led
  • Resistor
  • Jumper cables

Step 2: Create the Useless Box

Picture of Create the Useless Box

It pretty hard to get the lego so that it works, and connect the servo's to the lego. I used some screws to screw a cog-wheel to the servo.

I used one servo to open the door of the box and the other for moving the switch back (turn it off again). When you are done with the mechanical part you can close it all up.

Step 3: Connecting It All Together

Picture of Connecting It All Together

Connect the 5v from the Arduino to the + of the breadboard and the GND to the - of the breadboard. Next connect the + and - of the breadboard to the + and - of the servo's and connect the signal wires to the A0 and A2 of the Arduino (I connect everything to the breadboard first)

Connect the collision sensor GND and VCC to the - and + of the breadboard and connect the out/signal to the digital 4 pin of the Ardiono.

Place the Led on the breadboard, connect the + (long leg) to the digital pin 6 of the Ardiono. Connect the - (short leg) to the resistor and the other end of the resistor to the - of the breadboard.

Step 4: The Code

The code is pretty strait forward

Setup some values, lower and upper bound of the servo motors, pins of the led and sensor.

#include

Servo servo1;
Servo servo2;

#define UP 10
#define DOWN 60

#define DOOR_UP 60
#define DOOR_DOWN 10

#define BUTTON 4
#define LED 6

Initialise the motors and set them to the closed position
void setup() {

servo1.attach(16);
servo2.attach(14);
moveDown();
moveDoorDown();
Serial.begin(19200);
pinMode(LED, OUTPUT);

}

Check each loop if the button is pressed to trigger the motors with a random delay

void loop() {

int val = digitalRead(BUTTON);
if (val == LOW) {

digitalWrite (LED, HIGH);
int rnd = random(1000, 4000);
delay(rnd);
moveDoor();
delay(100);

} else {

digitalWrite (LED, LOW);

}

}

Helper function to move the handle and the door

void moveDown(){

Serial.println("Move down"); servo1.write(DOWN);

}

void moveDoorDown() {

servo2.write(DOOR_DOWN);

}

void moveDoor() {

Serial.println("move door");
servo2.write(DOOR_UP);
delay(500);
moveHandle();

}

void moveHandle() {

Serial.println("move up");
servo1.write(UP);
delay(300);
moveDown();
delay(300);
moveDoorDown();

}

Step 5: Finished

Comments

mrsmerwin (author)2017-02-08

cute

About This Instructable

514views

7favorites

License:

More by Aansteekblokje:Arduino UselessLegoBoxArduino Lego Car
Add instructable to: