Project tutorial

Candy Dispenser © GPL3+

A simple candy dispenser to enjoy upon your request, as well as to practice Arduino and building skills!

  • 3,741 views
  • 4 comments
  • 12 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

The Project

This is an easy way to create a candy dispenser using only: IR sensor, servo, and pushbutton. It can be split up into these steps:

  • Making and using Arduino Uno R3
  • Coding and setting up the Arduino
  • Creating the casing and mechanical parts
  • Installing the electronics
  • Testing and usage

Here is a short demonstration video:

Writing the coding was the hardest part for me because I am still new to writing coding language but I manage to do it after multiple errors and help from Mr Google and friends. Writing codes is the longest time on this part of project.

Credit to the man who INSPIRED me to do this candy dispenser! This is the simplified version of: https://www.hackster.io/gatoninja236/candy-dispenser-with-google-assistant-564724

Creating the Box and Mechanical Parts

This is the fun part of the project to create. It took me at least 1 whole day to fully complete it after multiple errors and parts that don't fit.

Step 1

Lay Out Components

This might look very messy but this is the easiest way to sort out each component and test run each part to see if there's any error. I laid out different colors of wires so that its easier to look at. I laid out all components on a piece of cardboard and sort out which one goes first.

Step 2

Creating the Box

I drew a technical drawing of how the box should look. Most of the parts are made out of Cardboard and some are made out of Acrylic. Then I measure each part.

  • Front & Back = 20cm x 16cm (Back is Acrylic)
  • Side - Side = 12cm x 16cm
  • Top & Bottom = 20cm x 12cm

Step 3

This is how it will look like after attaching them all together (using masking tape first).

Then I cut out places for the components:

  • LCD = 7.2cm x 2.5cm
  • Buttons = 1.2cm x 1.2cm

For the cup:

  • Length x Height = 9cm x 12cm
  • Width x Depth = 9cm x 9cm

Do not to forget to cut out for the sensors too:

I made the dispenser into steps so that the candy doesn't fall right away:

Step 4

I laid out the breadboard and Arduino behind each other. I use acrylics to stick them together. This way it will save a lot of space.

Step 5

Start attaching the components on to the board. I start with LCD and buttons.

Next, after wiring all the cables on the components, I attached them in the middle of the box.

Attaching the servo:

This is how it looks like from the back. I also added a power button to power up the Dispenser using the battery.

Step 6

The finished look.

Schematics

Real Life Schematic
It looks a bit complicated, but follow as how the color wiring is.
 mg 0642 g7ffe4mql9

Code

Candy DispenserArduino
//------LIBRARIES-----
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
//------LIBRARIES-----

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

Servo myservo;
int pos = 90; // servo start position
int buttonCLK = 7; //buttonClockwise pin
int buttonAntiCLK = 6; //buttonAntiClockwise pin
//int sensorPin = 2;             // IR sensor to be attached to digital pin NO.2
//int sensorState = 0;           // variable sensorState for storing statistics about the status of the sensor

int set = 0;
const int avoidPin = 5; //the ir obstacle sensor attach to pin 5

void setup()
{
  lcd.begin(); // initialize the LCD
  lcd.clear();
  
  myservo.attach(9); // Servo Digital 9
  pinMode(buttonCLK, INPUT);
  pinMode(buttonAntiCLK, INPUT);
  pinMode(avoidPin, INPUT); //set the avoidPin as INPUT
  
  Serial.begin(9600);  
  lcd.backlight(); // Turn on the blacklight
  lcd.setCursor(3,0);
  lcd.print("Welcome to");
  lcd.setCursor(3,1);
  lcd.print("Candy Land");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("How much candy?");
  lcd.setCursor(0,1);
  lcd.print("A Little  A Lot");
  delay(5000);

}

void loop()
{
  
  //----------THIS IS THE IF FUNCTIONS FOR BUTTONS-----------
  if(digitalRead(buttonCLK) == HIGH) // buttonClockwise is pressed
  {
    //TurnClockwise();
    //SensorDetection();
    lcd.clear();
    lcd.print("Please Place Cup");
    delay(2000);
    lcd.clear();
    set = set + 1;
    
  }

  

  if(digitalRead(buttonAntiCLK) == HIGH) // buttonAntiClockwise is pressed
  {
    //TurnAntiClockwise();
    //SensorDetection();
    lcd.clear();
    lcd.print("Please Place Cup");
    delay(2000);
    lcd.clear();
    set = set + 2;
  }

  if(set == 1)
  {
    IRCLK();
  }

  if(set == 2)
  {
    IRAntiCLK();
  }
  //----------THIS IS THE IF FUNCTIONS FOR BUTTONS-----------

  
}

void TurnClockwise()
{
  Serial.println("Candy 1 Being Dispensed");
  CandyText1(); //display Candy Type Text
  for(pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
    {   
        myservo.write(pos);              // tell servo to go to position in variable 'pos'    
        delay(15);                       // waits 15ms for the servo to reach the position        
    }

    if(pos == 180)
    {
      delay(2000);
      ThankYouText();                  // display Thank You Text
    }
}

void TurnAntiClockwise()
{
  Serial.println("Candy 2 Being Dispensed");
  CandyText2(); //display Candy Type Text
  for(pos = 90; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
    {                              
        myservo.write(pos);              // tell servo to go to position in variable 'pos'  
        delay(20);                       // waits 15ms for the servo to reach the position        
    }
    if(pos == 180)
    {
      delay(3000);
      ThankYouText();                  // display Thank You Text
    }
  
}

void IRCLK()
{
  
  delay(1000); //create a delay of 1s
  boolean avoidVal = digitalRead(avoidPin); //read the value of pin4
  lcd.setCursor(0,0);
  lcd.print("Detecting Cup");
  lcd.setCursor(0,14);
  lcd.print("...");

  if(avoidVal == LOW) //if the value is LOW level
  {
    lcd.clear();
    lcd.print("Cup Placed");
    delay(2000);
    TurnClockwise();
  }
}

void IRAntiCLK()
{
  delay(1000); //create a delay of 1s
  boolean avoidVal = digitalRead(avoidPin); //read the value of pin4
  lcd.setCursor(0,0);
  lcd.print("Detecting Cup");
  lcd.setCursor(0,14);
  lcd.print("...");
  
  if(avoidVal == LOW) //if the value is LOW level
  {
    lcd.clear();
    lcd.print("Cup Placed");
    delay(2000);
    TurnAntiClockwise();
  }
  
}


void CandyText1()
{  
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.print("Pouring");
        lcd.setCursor(1,1);
        lcd.print("Little Candy");
}

void CandyText2()
{  
        lcd.clear();
        lcd.setCursor(4,0);
        lcd.print("Pouring");
        lcd.setCursor(3,1);
        lcd.print("Lot Candy");
}

void ThankYouText()
{
        lcd.clear();
        lcd.setCursor(5,0);
        lcd.print("Thank");
        lcd.setCursor(6,1);
        lcd.print("You");
        delay(15);        
        software_Reset();
}

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
  asm volatile ("jmp 0");  
} 

Comments

Similar projects you might like

Magic Lamp

by Nekhil ravi

  • 683 views
  • 2 comments
  • 8 respects

Arduino MKR GSM 1400 and DTMF

by Arduino_Genuino

  • 3,092 views
  • 0 comments
  • 9 respects

Love You Pillow

Project tutorial by Arduino

  • 1,510 views
  • 0 comments
  • 4 respects

Infrared Replicator

Project tutorial by Gustavo Gonnet

  • 3,418 views
  • 6 comments
  • 24 respects

Rural Hack Kit

by Officine Innesto

  • 3,460 views
  • 0 comments
  • 4 respects

A Model Lighthouse Using an Adafruit NeoPixel Ring

Project tutorial by Jeremy Lindsay

  • 250 views
  • 0 comments
  • 4 respects
Add projectSign up / Login