This project is made with the Servo Brick and Sonar Brick. It was built by my son, with some help from me, and with a little bit of trial and error, we were able to create an Automatic Hand Sanitizer Dispenser. The gear mechanism connected to the Servo Brick converts rotary motion into linear motion, which drives a piston and squeezes the dispenser when the Sonar Brick detects the hand below.

Connect:
  • Servo Brick to Port 2A
  • Sonar Brick to Port 4D

Run this code:

var sv = new Servo(PORT_2A);
var ul = new Sonar(PORT_4D);

var e = true;

sv.setPos(150);
while (true) {
  if (ul.getDistance() < 7) {
    e = true;
  }
  else {
    e = false;
  }
  if (e) {
    sv.setPos(0);
    delay(1500);
  }
  else {
    sv.setPos(150);
  }
  delay(20);
}

See the automatic dispenser in action: