Introduction: Motion Sensor Candy Dispenser by Lucas LeGage, Henry Morrison, Vladan Napijalo

About: Projects made by students in Ms. Lamm's engineering class at Portland High School.

This product is a candy dispenser that is activated via a motion sensor. It involves an intermediate level C++ code and circuit. This project involves lots of instruction but results in a fun machine that can be used by anyone.

Supplies

  • Bread Board
  • Arduino Uno
  • Laptop
  • Ultrasonic Sensor
  • Servo Motor
  • Foam Board/Cardboard
  • 9V Battery
  • Dowel Rod
  • Hot Glue
  • Masking Tape
  • Male to Male Wires
  • Candy

Step 1: Wiring

Set up the wiring for the ultrasonic sensor and the Servo motor using circuit diagrams.

Step 2: Setting Up Code

  • Write a code that sets up the motion sensor and servo motor
  • Print the values of the motion sensor and set an if-else statement for when the motion sensor reads a specific value. 
  • Once it reads that value, make the servo motor rotate 90 degrees, and rotate back -90 degrees. 

Below is the code written in C++


#include <Servo.h> // includes servo library


Servo myServo;

int const trigPin = 3; // declare variables

const int echoPin = 2;

float duration, distance;

void setup() {

myServo.attach(6); // set up motion sensor and servo motor

pinMode(trigPin, OUTPUT);

pinMode(echoPin,INPUT);

Serial.begin(9600);

}


void loop() {


digitalWrite(trigPin,LOW); // read values of motion sensor

delayMicroseconds(2);

digitalWrite(trigPin,HIGH);

delayMicroseconds(10);

digitalWrite(trigPin,LOW);

duration=pulseIn(echoPin,HIGH); // printing the value numerically

distance=(duration*.0343)/2;

Serial.print("Distance:");

Serial.println(distance);

delay(10);

if (distance<10) {

 myServo.write(180); //declare angle

 delay(1000); //set a delay

 myServo.write(0);//close

}

else{

 myServo.write(0); //closed angle

}

}

Step 3: Box Set Up

Set up a roughly 5inx5inx12in box. Build a wheel that fits in the box without rubbing against the sides. Cut a small semi circle out of the wheel that holds the candy.

Step 4: Assembly

Tape the board in the box and add a battery to the arduino board with the code on it. Tape the ultrasonic sensor so that it faces down with nothing in the way. The sensor should be facing where the candy will lead to the users hand. Glue the servo motor to the wheel so the semi circle faces up when the motor is in its starting position. Add a funnel that leads to the wheel and stops it from falling through the box. Add a funnel below the wheel to direct the candy to the middle.

Step 5: Design

Add an arrow pointing to where the candy dispenses and the sensor is pointing. Write instructions then fill the top funnel with candy.

Step 6: Video